From 62d89d98a09e4819c90471b043a5db241bb85337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AD=94=E7=8E=8B=E5=B0=91=E5=B9=B4?= Date: Mon, 21 Oct 2024 23:41:46 +0800 Subject: [PATCH] feat: Response message can be overridden (#27) --- README.md | 10 +++++++-- src/logger.test.ts | 10 +++++++++ src/logger.ts | 16 +++++++++++++ src/middleware.test.ts | 51 ++++++++++++++++++++++++++++++++++++++++++ src/middleware.ts | 6 ++++- 5 files changed, 90 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e485fd3..e126f50 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,13 @@ bun add hono-pino pino ## Usage ```ts +import { Hono } from 'hono' +import { pinoLogger } from 'hono-pino' + const app = new Hono() .use( pinoLogger({ - pino: {level: 'debug'} + pino: {level: "debug"} }), ) .get((c) => { @@ -42,6 +45,9 @@ const app = new Hono() const posts = getPosts(); logger.assign({ posts }); + + logger.setResMessage("Get posts success"); // optional + return c.text(""); }); @@ -55,7 +61,7 @@ await app.request("/", { {"level":20, "token":"Bearer token"} { "level": 30, - "msg": "Request completed", + "msg": "Get posts success", "user": { "id": 1, "name": "john" diff --git a/src/logger.test.ts b/src/logger.test.ts index a04813a..601929a 100644 --- a/src/logger.test.ts +++ b/src/logger.test.ts @@ -93,4 +93,14 @@ describe("logger", () => { b: { c: 4, d: 3, e: 5 }, }); }); + + it("set response message", () => { + logger.setResMessage("foo"); + expect(logger.resMessage).toBe("foo"); + }); + + it("set response level", () => { + logger.setResLevel("trace"); + expect(logger.resLevel).toBe("trace"); + }); }); diff --git a/src/logger.ts b/src/logger.ts index 01914c2..bbee25f 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -16,6 +16,8 @@ export class PinoLogger { #rootLogger: pino.Logger; // Use the _ prefix to indicate that this should not be used _logger: pino.Logger; + resMessage?: string | null; + resLevel?: pino.Level | null; constructor(rootLogger: pino.Logger) { this.#rootLogger = rootLogger.child({}); @@ -52,6 +54,20 @@ export class PinoLogger { this._logger = this.#rootLogger.child(newBindings); } + + /** + * Override response log message + */ + setResMessage(message: string | null) { + this.resMessage = message; + } + + /** + * Override response log level + */ + setResLevel(level: pino.Level | null) { + this.resLevel = level; + } } // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/src/middleware.test.ts b/src/middleware.test.ts index 87f8b73..3517e97 100644 --- a/src/middleware.test.ts +++ b/src/middleware.test.ts @@ -4,6 +4,7 @@ import { pinoLogger } from "./middleware"; import type { Options } from "./types"; import { pino } from "pino"; import { PinoLogger } from "./logger"; +import defu from "defu"; const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); @@ -187,6 +188,56 @@ describe("on response", () => { msg: "foo", }); }); + + it("Override response log message", async () => { + const { logs, app } = createMockApp(); + app.get("/override-message", async (c) => { + c.get("logger").setResMessage("foo"); + return c.text("ok"); + }); + + const res = await app.request("/override-message"); + expect(res.status).toBe(200); + expect(res.text()).resolves.toBe("ok"); + + expect(logs).toHaveLength(1); + expect(logs[0]).toMatchObject( + defu( + { + req: { + url: "/override-message", + }, + msg: "foo", + }, + defaultResLog, + ), + ); + }); + + it("Override response log level", async () => { + const { logs, app } = createMockApp(); + app.get("/override-level", async (c) => { + c.get("logger").setResLevel("debug"); + return c.text("ok"); + }); + + const res = await app.request("/override-level"); + expect(res.status).toBe(200); + expect(res.text()).resolves.toBe("ok"); + + expect(logs).toHaveLength(1); + expect(logs[0]).toMatchObject( + defu( + { + req: { + url: "/override-level", + }, + level: 20, + }, + defaultResLog, + ), + ); + }); }); describe("response time", () => { diff --git a/src/middleware.ts b/src/middleware.ts index dc1b027..b83e915 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -70,8 +70,12 @@ export const pinoLogger = ( }; bindings = defu(bindings, onResBindings); - const level = opts?.http?.onResLevel?.(c) ?? (c.error ? "error" : "info"); + const level = + logger.resLevel ?? + opts?.http?.onResLevel?.(c) ?? + (c.error ? "error" : "info"); const msg = + logger.resMessage ?? opts?.http?.onResMessage?.(c) ?? (c.error ? c.error.message : "Request completed"); logger[level](bindings, msg);