@logtape/hono 2.1.0-dev.531 → 2.1.0-dev.540

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -99,6 +99,20 @@ app.use(honoLogger({
99
99
  }));
100
100
  ~~~~
101
101
 
102
+ The formatter callback receives the real Hono `Context`, so you can also read
103
+ context variables when your app uses them:
104
+
105
+ ~~~~ typescript
106
+ app.use(honoLogger({
107
+ format: (c, responseTime) => ({
108
+ requestId: c.get("requestId"),
109
+ method: c.req.method,
110
+ path: c.req.path,
111
+ duration: responseTime,
112
+ }),
113
+ }));
114
+ ~~~~
115
+
102
116
 
103
117
  Structured logging output
104
118
  -------------------------
package/dist/mod.d.cts CHANGED
@@ -1,26 +1,17 @@
1
1
  import { LogLevel, LogLevel as LogLevel$1 } from "@logtape/logtape";
2
- import { MiddlewareHandler } from "hono";
2
+ import { Context, MiddlewareHandler } from "hono";
3
3
 
4
4
  //#region src/mod.d.ts
5
5
 
6
6
  /**
7
- * Minimal Hono Context interface for compatibility across Hono versions.
7
+ * Hono context interface exposed to custom formatters and skip callbacks.
8
+ *
9
+ * This matches the actual runtime object passed to the middleware, so custom
10
+ * formatters can access context variables via methods like `c.get()` when
11
+ * needed.
8
12
  * @since 1.3.0
9
13
  */
10
- interface HonoContext {
11
- req: {
12
- method: string;
13
- url: string;
14
- path: string;
15
- header(name: string): string | undefined;
16
- };
17
- res: {
18
- status: number;
19
- headers: {
20
- get(name: string): string | null;
21
- };
22
- };
23
- }
14
+ interface HonoContext extends Context<any, any, any> {}
24
15
  /**
25
16
  * Predefined log format names compatible with Morgan.
26
17
  * @since 1.3.0
@@ -1 +1 @@
1
- {"version":3,"file":"mod.d.cts","names":[],"sources":["../src/mod.ts"],"sourcesContent":[],"mappings":";;;;;;AAUA;AAmBA;AAUA;AAA0B,UA7BT,WAAA,CA6BS;EAAA,GACrB,EAAA;IAES,MAAA,EAAA,MAAA;IAAM,GAAA,EAAA,MAAA;IAMH,IAAA,EAAA,MAAA;IAuBA,MAAA,CAAA,IAAA,EAAA,MAAkB,CAAA,EAAA,MAAA,GAAA,SAAA;EAAA,CAAA;EAAA,GAWhB,EAAA;IAeC,MAAA,EAAA,MAAA;IAAmB,OAAA,EAAA;MAejB,GAAA,CAAA,IAAA,EAAA,MAAA,CAAA,EAAA,MAAA,GAAA,IAAA;IAAW,CAAA;EAmMjB,CAAA;;;;AAEI;;KAxRR,gBAAA;;;;;;;;;KAUA,cAAA,OACP,+CAES;;;;;UAMG,oBAAA;;;;;;;;;;;;;;;;;;;;;;UAuBA,kBAAA;;;;;;;;;;mBAWE;;;;;;;;;;;;;;oBAeC,mBAAmB;;;;;;;;;;;;;;sBAejB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAmMN,UAAA,WACL,qBACR"}
1
+ {"version":3,"file":"mod.d.cts","names":[],"sources":["../src/mod.ts"],"sourcesContent":[],"mappings":";;;;;;AAeA;AAMA;AAUA;;;;AAGoB;AAMH,UAzBA,WAAA,SAAoB,OAyBA,CAAA,GAAA,EAAA,GAAA,EAAA,GAAA,CAAA,CAAA,CAuBrC;;;;;AAyCsB,KAnFV,gBAAA,GAmFU,UAAA,GAAA,QAAA,GAAA,KAAA,GAAA,OAAA,GAAA,MAAA;AAAW;AAmMjC;;;;AAEoB;;;KA9QR,cAAA,OACP,+CAES;;;;;UAMG,oBAAA;;;;;;;;;;;;;;;;;;;;;;UAuBA,kBAAA;;;;;;;;;;mBAWE;;;;;;;;;;;;;;oBAeC,mBAAmB;;;;;;;;;;;;;;sBAejB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAmMN,UAAA,WACL,qBACR"}
package/dist/mod.d.ts CHANGED
@@ -1,26 +1,17 @@
1
1
  import { LogLevel, LogLevel as LogLevel$1 } from "@logtape/logtape";
2
- import { MiddlewareHandler } from "hono";
2
+ import { Context, MiddlewareHandler } from "hono";
3
3
 
4
4
  //#region src/mod.d.ts
5
5
 
6
6
  /**
7
- * Minimal Hono Context interface for compatibility across Hono versions.
7
+ * Hono context interface exposed to custom formatters and skip callbacks.
8
+ *
9
+ * This matches the actual runtime object passed to the middleware, so custom
10
+ * formatters can access context variables via methods like `c.get()` when
11
+ * needed.
8
12
  * @since 1.3.0
9
13
  */
10
- interface HonoContext {
11
- req: {
12
- method: string;
13
- url: string;
14
- path: string;
15
- header(name: string): string | undefined;
16
- };
17
- res: {
18
- status: number;
19
- headers: {
20
- get(name: string): string | null;
21
- };
22
- };
23
- }
14
+ interface HonoContext extends Context<any, any, any> {}
24
15
  /**
25
16
  * Predefined log format names compatible with Morgan.
26
17
  * @since 1.3.0
package/dist/mod.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mod.d.ts","names":[],"sources":["../src/mod.ts"],"sourcesContent":[],"mappings":";;;;;;AAUA;AAmBA;AAUA;AAA0B,UA7BT,WAAA,CA6BS;EAAA,GACrB,EAAA;IAES,MAAA,EAAA,MAAA;IAAM,GAAA,EAAA,MAAA;IAMH,IAAA,EAAA,MAAA;IAuBA,MAAA,CAAA,IAAA,EAAA,MAAkB,CAAA,EAAA,MAAA,GAAA,SAAA;EAAA,CAAA;EAAA,GAWhB,EAAA;IAeC,MAAA,EAAA,MAAA;IAAmB,OAAA,EAAA;MAejB,GAAA,CAAA,IAAA,EAAA,MAAA,CAAA,EAAA,MAAA,GAAA,IAAA;IAAW,CAAA;EAmMjB,CAAA;;;;AAEI;;KAxRR,gBAAA;;;;;;;;;KAUA,cAAA,OACP,+CAES;;;;;UAMG,oBAAA;;;;;;;;;;;;;;;;;;;;;;UAuBA,kBAAA;;;;;;;;;;mBAWE;;;;;;;;;;;;;;oBAeC,mBAAmB;;;;;;;;;;;;;;sBAejB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAmMN,UAAA,WACL,qBACR"}
1
+ {"version":3,"file":"mod.d.ts","names":[],"sources":["../src/mod.ts"],"sourcesContent":[],"mappings":";;;;;;AAeA;AAMA;AAUA;;;;AAGoB;AAMH,UAzBA,WAAA,SAAoB,OAyBA,CAAA,GAAA,EAAA,GAAA,EAAA,GAAA,CAAA,CAAA,CAuBrC;;;;;AAyCsB,KAnFV,gBAAA,GAmFU,UAAA,GAAA,QAAA,GAAA,KAAA,GAAA,OAAA,GAAA,MAAA;AAAW;AAmMjC;;;;AAEoB;;;KA9QR,cAAA,OACP,+CAES;;;;;UAMG,oBAAA;;;;;;;;;;;;;;;;;;;;;;UAuBA,kBAAA;;;;;;;;;;mBAWE;;;;;;;;;;;;;;oBAeC,mBAAmB;;;;;;;;;;;;;;sBAejB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAmMN,UAAA,WACL,qBACR"}
package/dist/mod.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mod.js","names":["c: HonoContext","responseTime: number","predefinedFormats: Record<PredefinedFormat, FormatFunction>","category: string | readonly string[]","options: HonoLogTapeOptions","formatFn: FormatFunction","result"],"sources":["../src/mod.ts"],"sourcesContent":["import { getLogger, type LogLevel } from \"@logtape/logtape\";\nimport { createMiddleware } from \"hono/factory\";\nimport type { MiddlewareHandler } from \"hono\";\n\nexport type { LogLevel } from \"@logtape/logtape\";\n\n/**\n * Minimal Hono Context interface for compatibility across Hono versions.\n * @since 1.3.0\n */\nexport interface HonoContext {\n req: {\n method: string;\n url: string;\n path: string;\n header(name: string): string | undefined;\n };\n res: {\n status: number;\n headers: {\n get(name: string): string | null;\n };\n };\n}\n\n/**\n * Predefined log format names compatible with Morgan.\n * @since 1.3.0\n */\nexport type PredefinedFormat = \"combined\" | \"common\" | \"dev\" | \"short\" | \"tiny\";\n\n/**\n * Custom format function for request logging.\n *\n * @param c The Hono context object.\n * @param responseTime The response time in milliseconds.\n * @returns A string message or an object with structured properties.\n * @since 1.3.0\n */\nexport type FormatFunction = (\n c: HonoContext,\n responseTime: number,\n) => string | Record<string, unknown>;\n\n/**\n * Structured log properties for HTTP requests.\n * @since 1.3.0\n */\nexport interface RequestLogProperties {\n /** HTTP request method */\n method: string;\n /** Request URL */\n url: string;\n /** Request path */\n path: string;\n /** HTTP response status code */\n status: number;\n /** Response time in milliseconds */\n responseTime: number;\n /** Response content-length header value */\n contentLength: string | undefined;\n /** User-Agent header value */\n userAgent: string | undefined;\n /** Referrer header value */\n referrer: string | undefined;\n}\n\n/**\n * Options for configuring the Hono LogTape middleware.\n * @since 1.3.0\n */\nexport interface HonoLogTapeOptions {\n /**\n * The LogTape category to use for logging.\n * @default [\"hono\"]\n */\n readonly category?: string | readonly string[];\n\n /**\n * The log level to use for request logging.\n * @default \"info\"\n */\n readonly level?: LogLevel;\n\n /**\n * The format for log output.\n * Can be a predefined format name or a custom format function.\n *\n * Predefined formats:\n * - `\"combined\"` - Apache Combined Log Format (structured, default)\n * - `\"common\"` - Apache Common Log Format (structured, no referrer/userAgent)\n * - `\"dev\"` - Concise colored output for development (string)\n * - `\"short\"` - Shorter than common (string)\n * - `\"tiny\"` - Minimal output (string)\n *\n * @default \"combined\"\n */\n readonly format?: PredefinedFormat | FormatFunction;\n\n /**\n * Function to determine whether logging should be skipped.\n * Return `true` to skip logging for a request.\n *\n * @example Skip logging for health check endpoint\n * ```typescript\n * app.use(honoLogger({\n * skip: (c) => c.req.path === \"/health\",\n * }));\n * ```\n *\n * @default () => false\n */\n readonly skip?: (c: HonoContext) => boolean;\n\n /**\n * If `true`, logs are written immediately when the request is received.\n * If `false` (default), logs are written after the response is sent.\n *\n * Note: When `logRequest` is `true`, response-related properties\n * (status, responseTime, contentLength) will not be available.\n *\n * @default false\n */\n readonly logRequest?: boolean;\n}\n\n/**\n * Get referrer from request headers.\n */\nfunction getReferrer(c: HonoContext): string | undefined {\n return c.req.header(\"referrer\") || c.req.header(\"referer\");\n}\n\n/**\n * Get user agent from request headers.\n */\nfunction getUserAgent(c: HonoContext): string | undefined {\n return c.req.header(\"user-agent\");\n}\n\n/**\n * Get content length from response headers.\n */\nfunction getContentLength(c: HonoContext): string | undefined {\n const contentLength = c.res.headers.get(\"content-length\");\n if (contentLength === null) return undefined;\n return contentLength;\n}\n\n/**\n * Build structured log properties from context.\n */\nfunction buildProperties(\n c: HonoContext,\n responseTime: number,\n): RequestLogProperties {\n return {\n method: c.req.method,\n url: c.req.url,\n path: c.req.path,\n status: c.res.status,\n responseTime,\n contentLength: getContentLength(c),\n userAgent: getUserAgent(c),\n referrer: getReferrer(c),\n };\n}\n\n/**\n * Combined format (Apache Combined Log Format).\n * Returns all structured properties.\n */\nfunction formatCombined(\n c: HonoContext,\n responseTime: number,\n): Record<string, unknown> {\n return { ...buildProperties(c, responseTime) };\n}\n\n/**\n * Common format (Apache Common Log Format).\n * Like combined but without referrer and userAgent.\n */\nfunction formatCommon(\n c: HonoContext,\n responseTime: number,\n): Record<string, unknown> {\n const props = buildProperties(c, responseTime);\n const { referrer: _referrer, userAgent: _userAgent, ...rest } = props;\n return rest;\n}\n\n/**\n * Dev format (colored output for development).\n * :method :path :status :response-time ms - :res[content-length]\n */\nfunction formatDev(\n c: HonoContext,\n responseTime: number,\n): string {\n const contentLength = getContentLength(c) ?? \"-\";\n return `${c.req.method} ${c.req.path} ${c.res.status} ${\n responseTime.toFixed(3)\n } ms - ${contentLength}`;\n}\n\n/**\n * Short format.\n * :method :url :status :res[content-length] - :response-time ms\n */\nfunction formatShort(\n c: HonoContext,\n responseTime: number,\n): string {\n const contentLength = getContentLength(c) ?? \"-\";\n return `${c.req.method} ${c.req.url} ${c.res.status} ${contentLength} - ${\n responseTime.toFixed(3)\n } ms`;\n}\n\n/**\n * Tiny format (minimal output).\n * :method :path :status :res[content-length] - :response-time ms\n */\nfunction formatTiny(\n c: HonoContext,\n responseTime: number,\n): string {\n const contentLength = getContentLength(c) ?? \"-\";\n return `${c.req.method} ${c.req.path} ${c.res.status} ${contentLength} - ${\n responseTime.toFixed(3)\n } ms`;\n}\n\n/**\n * Map of predefined format functions.\n */\nconst predefinedFormats: Record<PredefinedFormat, FormatFunction> = {\n combined: formatCombined,\n common: formatCommon,\n dev: formatDev,\n short: formatShort,\n tiny: formatTiny,\n};\n\n/**\n * Normalize category to array format.\n */\nfunction normalizeCategory(\n category: string | readonly string[],\n): readonly string[] {\n return typeof category === \"string\" ? [category] : category;\n}\n\n/**\n * Creates Hono middleware for HTTP request logging using LogTape.\n *\n * This middleware provides Morgan-compatible request logging with LogTape\n * as the backend, supporting structured logging and customizable formats.\n *\n * @example Basic usage\n * ```typescript\n * import { Hono } from \"hono\";\n * import { configure, getConsoleSink } from \"@logtape/logtape\";\n * import { honoLogger } from \"@logtape/hono\";\n *\n * await configure({\n * sinks: { console: getConsoleSink() },\n * loggers: [\n * { category: [\"hono\"], sinks: [\"console\"], lowestLevel: \"info\" }\n * ],\n * });\n *\n * const app = new Hono();\n * app.use(honoLogger());\n *\n * app.get(\"/\", (c) => c.json({ hello: \"world\" }));\n *\n * export default app;\n * ```\n *\n * @example With custom options\n * ```typescript\n * app.use(honoLogger({\n * category: [\"myapp\", \"http\"],\n * level: \"debug\",\n * format: \"dev\",\n * skip: (c) => c.req.path === \"/health\",\n * }));\n * ```\n *\n * @example With custom format function\n * ```typescript\n * app.use(honoLogger({\n * format: (c, responseTime) => ({\n * method: c.req.method,\n * path: c.req.path,\n * status: c.res.status,\n * duration: responseTime,\n * }),\n * }));\n * ```\n *\n * @param options Configuration options for the middleware.\n * @returns Hono middleware function.\n * @since 1.3.0\n */\nexport function honoLogger(\n options: HonoLogTapeOptions = {},\n): MiddlewareHandler {\n const category = normalizeCategory(options.category ?? [\"hono\"]);\n const logger = getLogger(category);\n const level = options.level ?? \"info\";\n const formatOption = options.format ?? \"combined\";\n const skip = options.skip ?? (() => false);\n const logRequest = options.logRequest ?? false;\n\n // Resolve format function\n const formatFn: FormatFunction = typeof formatOption === \"string\"\n ? predefinedFormats[formatOption]\n : formatOption;\n\n const logMethod = logger[level].bind(logger);\n\n return createMiddleware(async (c, next) => {\n const startTime = Date.now();\n\n // For immediate logging, log when request arrives\n if (logRequest) {\n if (!skip(c as unknown as HonoContext)) {\n const result = formatFn(c as unknown as HonoContext, 0);\n if (typeof result === \"string\") {\n logMethod(result);\n } else {\n logMethod(\"{method} {url}\", result);\n }\n }\n await next();\n return;\n }\n\n // Log after response is sent\n await next();\n\n if (skip(c as unknown as HonoContext)) return;\n\n const responseTime = Date.now() - startTime;\n const result = formatFn(c as unknown as HonoContext, responseTime);\n\n if (typeof result === \"string\") {\n logMethod(result);\n } else {\n logMethod(\"{method} {url} {status} - {responseTime} ms\", result);\n }\n });\n}\n"],"mappings":";;;;;;;AAiIA,SAAS,YAAYA,GAAoC;AACvD,QAAO,EAAE,IAAI,OAAO,WAAW,IAAI,EAAE,IAAI,OAAO,UAAU;AAC3D;;;;AAKD,SAAS,aAAaA,GAAoC;AACxD,QAAO,EAAE,IAAI,OAAO,aAAa;AAClC;;;;AAKD,SAAS,iBAAiBA,GAAoC;CAC5D,MAAM,gBAAgB,EAAE,IAAI,QAAQ,IAAI,iBAAiB;AACzD,KAAI,kBAAkB,KAAM;AAC5B,QAAO;AACR;;;;AAKD,SAAS,gBACPA,GACAC,cACsB;AACtB,QAAO;EACL,QAAQ,EAAE,IAAI;EACd,KAAK,EAAE,IAAI;EACX,MAAM,EAAE,IAAI;EACZ,QAAQ,EAAE,IAAI;EACd;EACA,eAAe,iBAAiB,EAAE;EAClC,WAAW,aAAa,EAAE;EAC1B,UAAU,YAAY,EAAE;CACzB;AACF;;;;;AAMD,SAAS,eACPD,GACAC,cACyB;AACzB,QAAO,EAAE,GAAG,gBAAgB,GAAG,aAAa,CAAE;AAC/C;;;;;AAMD,SAAS,aACPD,GACAC,cACyB;CACzB,MAAM,QAAQ,gBAAgB,GAAG,aAAa;CAC9C,MAAM,EAAE,UAAU,WAAW,WAAW,WAA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,OAAO,KAAK,OAAO;AAE5C,QAAO,iBAAiB,OAAO,GAAG,SAAS;EACzC,MAAM,YAAY,KAAK,KAAK;AAG5B,MAAI,YAAY;AACd,QAAK,KAAK,EAA4B,EAAE;IACtC,MAAMC,WAAS,SAAS,GAA6B,EAAE;AACvD,eAAWA,aAAW,SACpB,WAAUA,SAAO;QAEjB,WAAU,kBAAkBA,SAAO;GAEtC;AACD,SAAM,MAAM;AACZ;EACD;AAGD,QAAM,MAAM;AAEZ,MAAI,KAAK,EAA4B,CAAE;EAEvC,MAAM,eAAe,KAAK,KAAK,GAAG;EAClC,MAAM,SAAS,SAAS,GAA6B,aAAa;AAElE,aAAW,WAAW,SACpB,WAAU,OAAO;MAEjB,WAAU,+CAA+C,OAAO;CAEnE,EAAC;AACH"}
1
+ {"version":3,"file":"mod.js","names":["c: HonoContext","responseTime: number","predefinedFormats: Record<PredefinedFormat, FormatFunction>","category: string | readonly string[]","options: HonoLogTapeOptions","formatFn: FormatFunction","result"],"sources":["../src/mod.ts"],"sourcesContent":["import { getLogger, type LogLevel } from \"@logtape/logtape\";\nimport { createMiddleware } from \"hono/factory\";\nimport type { Context, MiddlewareHandler } from \"hono\";\n\nexport type { LogLevel } from \"@logtape/logtape\";\n\n/**\n * Hono context interface exposed to custom formatters and skip callbacks.\n *\n * This matches the actual runtime object passed to the middleware, so custom\n * formatters can access context variables via methods like `c.get()` when\n * needed.\n * @since 1.3.0\n */\n// deno-lint-ignore no-explicit-any\nexport interface HonoContext extends Context<any, any, any> {}\n\n/**\n * Predefined log format names compatible with Morgan.\n * @since 1.3.0\n */\nexport type PredefinedFormat = \"combined\" | \"common\" | \"dev\" | \"short\" | \"tiny\";\n\n/**\n * Custom format function for request logging.\n *\n * @param c The Hono context object.\n * @param responseTime The response time in milliseconds.\n * @returns A string message or an object with structured properties.\n * @since 1.3.0\n */\nexport type FormatFunction = (\n c: HonoContext,\n responseTime: number,\n) => string | Record<string, unknown>;\n\n/**\n * Structured log properties for HTTP requests.\n * @since 1.3.0\n */\nexport interface RequestLogProperties {\n /** HTTP request method */\n method: string;\n /** Request URL */\n url: string;\n /** Request path */\n path: string;\n /** HTTP response status code */\n status: number;\n /** Response time in milliseconds */\n responseTime: number;\n /** Response content-length header value */\n contentLength: string | undefined;\n /** User-Agent header value */\n userAgent: string | undefined;\n /** Referrer header value */\n referrer: string | undefined;\n}\n\n/**\n * Options for configuring the Hono LogTape middleware.\n * @since 1.3.0\n */\nexport interface HonoLogTapeOptions {\n /**\n * The LogTape category to use for logging.\n * @default [\"hono\"]\n */\n readonly category?: string | readonly string[];\n\n /**\n * The log level to use for request logging.\n * @default \"info\"\n */\n readonly level?: LogLevel;\n\n /**\n * The format for log output.\n * Can be a predefined format name or a custom format function.\n *\n * Predefined formats:\n * - `\"combined\"` - Apache Combined Log Format (structured, default)\n * - `\"common\"` - Apache Common Log Format (structured, no referrer/userAgent)\n * - `\"dev\"` - Concise colored output for development (string)\n * - `\"short\"` - Shorter than common (string)\n * - `\"tiny\"` - Minimal output (string)\n *\n * @default \"combined\"\n */\n readonly format?: PredefinedFormat | FormatFunction;\n\n /**\n * Function to determine whether logging should be skipped.\n * Return `true` to skip logging for a request.\n *\n * @example Skip logging for health check endpoint\n * ```typescript\n * app.use(honoLogger({\n * skip: (c) => c.req.path === \"/health\",\n * }));\n * ```\n *\n * @default () => false\n */\n readonly skip?: (c: HonoContext) => boolean;\n\n /**\n * If `true`, logs are written immediately when the request is received.\n * If `false` (default), logs are written after the response is sent.\n *\n * Note: When `logRequest` is `true`, response-related properties\n * (status, responseTime, contentLength) will not be available.\n *\n * @default false\n */\n readonly logRequest?: boolean;\n}\n\n/**\n * Get referrer from request headers.\n */\nfunction getReferrer(c: HonoContext): string | undefined {\n return c.req.header(\"referrer\") || c.req.header(\"referer\");\n}\n\n/**\n * Get user agent from request headers.\n */\nfunction getUserAgent(c: HonoContext): string | undefined {\n return c.req.header(\"user-agent\");\n}\n\n/**\n * Get content length from response headers.\n */\nfunction getContentLength(c: HonoContext): string | undefined {\n const contentLength = c.res.headers.get(\"content-length\");\n if (contentLength === null) return undefined;\n return contentLength;\n}\n\n/**\n * Build structured log properties from context.\n */\nfunction buildProperties(\n c: HonoContext,\n responseTime: number,\n): RequestLogProperties {\n return {\n method: c.req.method,\n url: c.req.url,\n path: c.req.path,\n status: c.res.status,\n responseTime,\n contentLength: getContentLength(c),\n userAgent: getUserAgent(c),\n referrer: getReferrer(c),\n };\n}\n\n/**\n * Combined format (Apache Combined Log Format).\n * Returns all structured properties.\n */\nfunction formatCombined(\n c: HonoContext,\n responseTime: number,\n): Record<string, unknown> {\n return { ...buildProperties(c, responseTime) };\n}\n\n/**\n * Common format (Apache Common Log Format).\n * Like combined but without referrer and userAgent.\n */\nfunction formatCommon(\n c: HonoContext,\n responseTime: number,\n): Record<string, unknown> {\n const props = buildProperties(c, responseTime);\n const { referrer: _referrer, userAgent: _userAgent, ...rest } = props;\n return rest;\n}\n\n/**\n * Dev format (colored output for development).\n * :method :path :status :response-time ms - :res[content-length]\n */\nfunction formatDev(\n c: HonoContext,\n responseTime: number,\n): string {\n const contentLength = getContentLength(c) ?? \"-\";\n return `${c.req.method} ${c.req.path} ${c.res.status} ${\n responseTime.toFixed(3)\n } ms - ${contentLength}`;\n}\n\n/**\n * Short format.\n * :method :url :status :res[content-length] - :response-time ms\n */\nfunction formatShort(\n c: HonoContext,\n responseTime: number,\n): string {\n const contentLength = getContentLength(c) ?? \"-\";\n return `${c.req.method} ${c.req.url} ${c.res.status} ${contentLength} - ${\n responseTime.toFixed(3)\n } ms`;\n}\n\n/**\n * Tiny format (minimal output).\n * :method :path :status :res[content-length] - :response-time ms\n */\nfunction formatTiny(\n c: HonoContext,\n responseTime: number,\n): string {\n const contentLength = getContentLength(c) ?? \"-\";\n return `${c.req.method} ${c.req.path} ${c.res.status} ${contentLength} - ${\n responseTime.toFixed(3)\n } ms`;\n}\n\n/**\n * Map of predefined format functions.\n */\nconst predefinedFormats: Record<PredefinedFormat, FormatFunction> = {\n combined: formatCombined,\n common: formatCommon,\n dev: formatDev,\n short: formatShort,\n tiny: formatTiny,\n};\n\n/**\n * Normalize category to array format.\n */\nfunction normalizeCategory(\n category: string | readonly string[],\n): readonly string[] {\n return typeof category === \"string\" ? [category] : category;\n}\n\n/**\n * Creates Hono middleware for HTTP request logging using LogTape.\n *\n * This middleware provides Morgan-compatible request logging with LogTape\n * as the backend, supporting structured logging and customizable formats.\n *\n * @example Basic usage\n * ```typescript\n * import { Hono } from \"hono\";\n * import { configure, getConsoleSink } from \"@logtape/logtape\";\n * import { honoLogger } from \"@logtape/hono\";\n *\n * await configure({\n * sinks: { console: getConsoleSink() },\n * loggers: [\n * { category: [\"hono\"], sinks: [\"console\"], lowestLevel: \"info\" }\n * ],\n * });\n *\n * const app = new Hono();\n * app.use(honoLogger());\n *\n * app.get(\"/\", (c) => c.json({ hello: \"world\" }));\n *\n * export default app;\n * ```\n *\n * @example With custom options\n * ```typescript\n * app.use(honoLogger({\n * category: [\"myapp\", \"http\"],\n * level: \"debug\",\n * format: \"dev\",\n * skip: (c) => c.req.path === \"/health\",\n * }));\n * ```\n *\n * @example With custom format function\n * ```typescript\n * app.use(honoLogger({\n * format: (c, responseTime) => ({\n * method: c.req.method,\n * path: c.req.path,\n * status: c.res.status,\n * duration: responseTime,\n * }),\n * }));\n * ```\n *\n * @param options Configuration options for the middleware.\n * @returns Hono middleware function.\n * @since 1.3.0\n */\nexport function honoLogger(\n options: HonoLogTapeOptions = {},\n): MiddlewareHandler {\n const category = normalizeCategory(options.category ?? [\"hono\"]);\n const logger = getLogger(category);\n const level = options.level ?? \"info\";\n const formatOption = options.format ?? \"combined\";\n const skip = options.skip ?? (() => false);\n const logRequest = options.logRequest ?? false;\n\n // Resolve format function\n const formatFn: FormatFunction = typeof formatOption === \"string\"\n ? predefinedFormats[formatOption]\n : formatOption;\n\n const logMethod = logger[level].bind(logger);\n\n return createMiddleware(async (c, next) => {\n const startTime = Date.now();\n\n // For immediate logging, log when request arrives\n if (logRequest) {\n if (!skip(c as unknown as HonoContext)) {\n const result = formatFn(c as unknown as HonoContext, 0);\n if (typeof result === \"string\") {\n logMethod(result);\n } else {\n logMethod(\"{method} {url}\", result);\n }\n }\n await next();\n return;\n }\n\n // Log after response is sent\n await next();\n\n if (skip(c as unknown as HonoContext)) return;\n\n const responseTime = Date.now() - startTime;\n const result = formatFn(c as unknown as HonoContext, responseTime);\n\n if (typeof result === \"string\") {\n logMethod(result);\n } else {\n logMethod(\"{method} {url} {status} - {responseTime} ms\", result);\n }\n });\n}\n"],"mappings":";;;;;;;AAyHA,SAAS,YAAYA,GAAoC;AACvD,QAAO,EAAE,IAAI,OAAO,WAAW,IAAI,EAAE,IAAI,OAAO,UAAU;AAC3D;;;;AAKD,SAAS,aAAaA,GAAoC;AACxD,QAAO,EAAE,IAAI,OAAO,aAAa;AAClC;;;;AAKD,SAAS,iBAAiBA,GAAoC;CAC5D,MAAM,gBAAgB,EAAE,IAAI,QAAQ,IAAI,iBAAiB;AACzD,KAAI,kBAAkB,KAAM;AAC5B,QAAO;AACR;;;;AAKD,SAAS,gBACPA,GACAC,cACsB;AACtB,QAAO;EACL,QAAQ,EAAE,IAAI;EACd,KAAK,EAAE,IAAI;EACX,MAAM,EAAE,IAAI;EACZ,QAAQ,EAAE,IAAI;EACd;EACA,eAAe,iBAAiB,EAAE;EAClC,WAAW,aAAa,EAAE;EAC1B,UAAU,YAAY,EAAE;CACzB;AACF;;;;;AAMD,SAAS,eACPD,GACAC,cACyB;AACzB,QAAO,EAAE,GAAG,gBAAgB,GAAG,aAAa,CAAE;AAC/C;;;;;AAMD,SAAS,aACPD,GACAC,cACyB;CACzB,MAAM,QAAQ,gBAAgB,GAAG,aAAa;CAC9C,MAAM,EAAE,UAAU,WAAW,WAAW,WAAY,GAAG,MAAM,GAAG;AAChE,QAAO;AACR;;;;;AAMD,SAAS,UACPD,GACAC,cACQ;CACR,MAAM,gBAAgB,iBAAiB,EAAE,IAAI;AAC7C,SAAQ,EAAE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,EAAE;IACtC,MAAMC,WAAS,SAAS,GAA6B,EAAE;AACvD,eAAWA,aAAW,SACpB,WAAUA,SAAO;QAEjB,WAAU,kBAAkBA,SAAO;GAEtC;AACD,SAAM,MAAM;AACZ;EACD;AAGD,QAAM,MAAM;AAEZ,MAAI,KAAK,EAA4B,CAAE;EAEvC,MAAM,eAAe,KAAK,KAAK,GAAG;EAClC,MAAM,SAAS,SAAS,GAA6B,aAAa;AAElE,aAAW,WAAW,SACpB,WAAU,OAAO;MAEjB,WAAU,+CAA+C,OAAO;CAEnE,EAAC;AACH"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logtape/hono",
3
- "version": "2.1.0-dev.531+0f09947c",
3
+ "version": "2.1.0-dev.540+76fbcbe2",
4
4
  "description": "Hono adapter for LogTape logging library",
5
5
  "keywords": [
6
6
  "logging",
@@ -55,7 +55,7 @@
55
55
  ],
56
56
  "peerDependencies": {
57
57
  "hono": "^4.0.0",
58
- "@logtape/logtape": "^2.1.0-dev.531+0f09947c"
58
+ "@logtape/logtape": "^2.1.0-dev.540+76fbcbe2"
59
59
  },
60
60
  "devDependencies": {
61
61
  "@alinea/suite": "^0.6.3",