Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change internal logger to _logger #20

Merged
merged 2 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quick-lobsters-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hono-pino": patch
---

Change internal `logger` to `_logger`
23 changes: 12 additions & 11 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { pino } from "pino";

export interface PinoLogger {
logger: pino.Logger;
_logger: pino.Logger;
trace: pino.LogFn;
debug: pino.LogFn;
info: pino.LogFn;
Expand All @@ -13,19 +13,20 @@ export interface PinoLogger {

export class PinoLogger {
#rootLogger: pino.Logger;
logger: pino.Logger;
// Use the _ prefix to indicate that this should not be used
_logger: pino.Logger;

constructor(rootLogger: pino.Logger) {
this.#rootLogger = rootLogger.child({});
this.logger = rootLogger;
this._logger = rootLogger;
}

/**
* assign bindings to http log context
*/
assign(bindings: pino.Bindings) {
this.logger = this.#rootLogger.child({
...this.logger.bindings(),
this._logger = this.#rootLogger.child({
...this._logger.bindings(),
...bindings,
});
}
Expand All @@ -34,35 +35,35 @@ export class PinoLogger {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
PinoLogger.prototype.trace = function (this, ...args: [any, ...any]) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
this.logger.trace(...args);
this._logger.trace(...args);
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
PinoLogger.prototype.debug = function (this, ...args: [any, ...any[]]) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
this.logger.debug(...args);
this._logger.debug(...args);
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
PinoLogger.prototype.info = function (this, ...args: [any, ...any]) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
this.logger.info(...args);
this._logger.info(...args);
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
PinoLogger.prototype.warn = function (this, ...args: [any, ...any]) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
this.logger.warn(...args);
this._logger.warn(...args);
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
PinoLogger.prototype.error = function (this, ...args: [any, ...any]) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
this.logger.error(...args);
this._logger.error(...args);
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
PinoLogger.prototype.fatal = function (this, ...args: [any, ...any]) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
this.logger.fatal(...args);
this._logger.fatal(...args);
};
4 changes: 2 additions & 2 deletions src/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ describe("contextKey option", () => {
.use(pinoLogger({ contextKey: "logger2" as const, pino: pino2 }))
.get("/", async (c) =>
c.text(
c.get("logger1").logger.bindings().name === "pino1" &&
c.get("logger2").logger.bindings().name === "pino2"
c.get("logger1")._logger.bindings().name === "pino1" &&
c.get("logger2")._logger.bindings().name === "pino2"
? "ok"
: "fail",
200,
Expand Down