@tanngo11/log 0.6.1 → 0.6.3

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.
Files changed (56) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/bullmq.cjs.map +1 -1
  3. package/dist/bullmq.d.cts +1 -1
  4. package/dist/bullmq.d.ts +1 -1
  5. package/dist/bullmq.js +2 -2
  6. package/dist/{chunk-URPHVC4Y.js → chunk-2IIUQFMK.js} +2 -2
  7. package/dist/{chunk-AF4YR4Y4.js → chunk-BFMFXDIJ.js} +5 -9
  8. package/dist/chunk-BFMFXDIJ.js.map +1 -0
  9. package/dist/{chunk-UOIJCSP5.js → chunk-EZCIMEVL.js} +3 -3
  10. package/dist/{chunk-UDS6S22P.js → chunk-JKCR57WJ.js} +25 -9
  11. package/dist/chunk-JKCR57WJ.js.map +1 -0
  12. package/dist/{chunk-2WNGXGP7.js → chunk-MGAEY7UZ.js} +2 -2
  13. package/dist/{chunk-RVETZAK5.js → chunk-RQWNJJIT.js} +3 -3
  14. package/dist/express.cjs +23 -16
  15. package/dist/express.cjs.map +1 -1
  16. package/dist/express.d.cts +2 -2
  17. package/dist/express.d.ts +2 -2
  18. package/dist/express.js +3 -3
  19. package/dist/fastify.cjs +23 -16
  20. package/dist/fastify.cjs.map +1 -1
  21. package/dist/fastify.d.cts +2 -2
  22. package/dist/fastify.d.ts +2 -2
  23. package/dist/fastify.js +3 -3
  24. package/dist/{http-44ztBSXk.d.ts → http-Dp6Qtr9M.d.ts} +1 -1
  25. package/dist/{http-BQJkHBwQ.d.cts → http-eeog5tR8.d.cts} +1 -1
  26. package/dist/index.cjs +27 -14
  27. package/dist/index.cjs.map +1 -1
  28. package/dist/index.d.cts +11 -4
  29. package/dist/index.d.ts +11 -4
  30. package/dist/index.js +4 -4
  31. package/dist/{logger-3Co9eVJf.d.cts → logger-CtlC9SUE.d.cts} +3 -2
  32. package/dist/{logger-3Co9eVJf.d.ts → logger-CtlC9SUE.d.ts} +3 -2
  33. package/dist/nest.cjs +23 -16
  34. package/dist/nest.cjs.map +1 -1
  35. package/dist/nest.d.cts +2 -2
  36. package/dist/nest.d.ts +2 -2
  37. package/dist/nest.js +4 -4
  38. package/dist/next.cjs +23 -16
  39. package/dist/next.cjs.map +1 -1
  40. package/dist/next.d.cts +2 -2
  41. package/dist/next.d.ts +2 -2
  42. package/dist/next.js +3 -3
  43. package/dist/prisma.d.cts +1 -1
  44. package/dist/prisma.d.ts +1 -1
  45. package/dist/strapi.cjs +23 -11
  46. package/dist/strapi.cjs.map +1 -1
  47. package/dist/strapi.d.cts +2 -2
  48. package/dist/strapi.d.ts +2 -2
  49. package/dist/strapi.js +2 -2
  50. package/package.json +1 -1
  51. package/dist/chunk-AF4YR4Y4.js.map +0 -1
  52. package/dist/chunk-UDS6S22P.js.map +0 -1
  53. /package/dist/{chunk-URPHVC4Y.js.map → chunk-2IIUQFMK.js.map} +0 -0
  54. /package/dist/{chunk-UOIJCSP5.js.map → chunk-EZCIMEVL.js.map} +0 -0
  55. /package/dist/{chunk-2WNGXGP7.js.map → chunk-MGAEY7UZ.js.map} +0 -0
  56. /package/dist/{chunk-RVETZAK5.js.map → chunk-RQWNJJIT.js.map} +0 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.6.3
4
+
5
+ Fixed:
6
+ - `err_stack` of a failed knex query repeated the statement: knex rewrites the message before the
7
+ stack is first read, so V8 renders the statement into the stack's first line. The statement is
8
+ now removed from `err_stack` and `err_cause` as well as `err_message`.
9
+ - The statement is removed whatever the error code: pg errors without a SQLSTATE ("Connection
10
+ terminated unexpectedly"), MySQL `ER_*` and SQLite errors, and statements behind a comment or an
11
+ opening parenthesis. Without a SQLSTATE the prefix must read like SQL, so "Update failed - retry"
12
+ stays whole.
13
+ - `REDACT_PATTERNS.phoneVN` again masks a number glued to a label such as `SDT0912345678`; only a
14
+ hex letter (a-f) next to the number rules a match out (0.6.0 ruled out any letter).
15
+
16
+ ## 0.6.2
17
+
18
+ Fixed:
19
+ - Reverts the 0.6.0 change that made an `http.request` line `warn` with `client_disconnect` when
20
+ its recorded error looked like a disconnect. The error's shape cannot tell: knex's pool (tarn)
21
+ rejects in-flight queries with `Error("aborted")` when it is destroyed on shutdown, and pg or
22
+ outbound socket resets are `ECONNRESET` too. Those are server failures and stay `error`.
23
+ `strapiServerErrors`, which only sees inbound socket errors, still marks client disconnects.
24
+
3
25
  ## 0.6.1
4
26
 
5
27
  Changed:
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/bullmq.ts","../src/context.ts","../src/global.ts","../src/error.ts","../src/job.ts"],"sourcesContent":["import { runJob } from \"./job\";\nimport type { Logger } from \"./logger\";\n\n/** Minimal shape of a BullMQ Job; avoids depending on bullmq. */\nexport interface BullJobLike {\n id?: string;\n attemptsMade: number;\n opts?: { attempts?: number };\n data?: unknown;\n}\n\nexport function loggedProcessor<J extends BullJobLike, R, A extends unknown[] = [token?: string, signal?: AbortSignal]>(\n log: Logger,\n name: string,\n processor: (job: J, ...rest: A) => Promise<R>,\n): (job: J, ...rest: A) => Promise<R> {\n return (job, ...rest) => {\n const attempts = job.opts?.attempts ?? 1;\n const attempt = job.attemptsMade + 1;\n const requestId = (job.data as { request_id?: unknown } | undefined)?.request_id;\n return runJob(\n log,\n {\n job_name: name,\n job_id: job.id,\n job_attempt: attempt,\n // UnrecoverableError skips the remaining retries.\n final_attempt: (err) => attempt >= attempts || (err as { name?: unknown } | null)?.name === \"UnrecoverableError\",\n request_id: typeof requestId === \"string\" ? requestId : undefined,\n },\n () => processor(job, ...rest),\n );\n };\n}\n","import { AsyncLocalStorage } from \"node:async_hooks\";\nimport { globalSingleton } from \"./global\";\nimport type { Fields } from \"./normalize\";\n\nexport interface ContextStore {\n fields: Fields;\n error?: unknown;\n /** Route template captured while the router still knew it (Express error path). */\n route?: string;\n parent?: ContextStore;\n}\n\nconst als = globalSingleton(\"als\", () => new AsyncLocalStorage<ContextStore>());\n\nexport const STORE_KEY: symbol = Symbol.for(\"@tanngo11/log.store\");\n\nexport function runWithStore<T>(fields: Fields, fn: (store: ContextStore) => T): T {\n const parent = als.getStore();\n const store: ContextStore = { fields: { ...parent?.fields, ...fields }, parent };\n return als.run(store, () => fn(store));\n}\n\nexport function withContext<T>(fields: Fields, fn: () => T): T {\n return runWithStore(fields, () => fn());\n}\n\nexport function currentStore(): ContextStore | undefined {\n return als.getStore();\n}\n\nexport function getContext(): Fields {\n return als.getStore()?.fields ?? {};\n}\n\n// Applies to the current context and every enclosing one, so a field added inside a nested\n// withContext/runJob still reaches the request's http.request line.\nexport function addContext(fields: Fields): void {\n for (let s = als.getStore(); s; s = s.parent) Object.assign(s.fields, fields);\n}\n\n// Sets the error on the store and on enclosing stores that have none yet.\nexport function recordErrorIn(store: ContextStore, err: unknown): void {\n store.error = err;\n for (let s = store.parent; s; s = s.parent) if (s.error === undefined) s.error = err;\n}\n\n// Marks the error that the current request/job summary line should carry. Does not write a line.\nexport function recordError(err: unknown): void {\n const store = als.getStore();\n if (store) recordErrorIn(store, err);\n}\n\nexport function attachStore(target: object, store: ContextStore): void {\n (target as Record<symbol, ContextStore>)[STORE_KEY] = store;\n}\n\nexport function storeOf(target: unknown): ContextStore | undefined {\n if (typeof target !== \"object\" || target === null) return undefined;\n return (target as Record<symbol, ContextStore | undefined>)[STORE_KEY];\n}\n","// Singletons shared by every copy of this package loaded in the process (ESM and CJS builds,\n// or duplicated installs), so context and dedupe state never split.\nexport function globalSingleton<T>(name: string, make: () => T): T {\n const key = Symbol.for(`@tanngo11/log.${name}`);\n const g = globalThis as unknown as Record<symbol, T | undefined>;\n let value = g[key];\n if (value === undefined) {\n value = make();\n g[key] = value;\n }\n return value;\n}\n","import { globalSingleton } from \"./global\";\nimport { defaultRedactor } from \"./redact\";\n\nexport interface ErrFields {\n err_type?: string;\n err_message?: string;\n err_code?: string;\n err_stack?: string;\n err_cause?: string;\n /** Database errors (pg): the table and constraint the server reported. */\n db_table?: string;\n db_constraint?: string;\n}\n\nconst MAX_CAUSE_DEPTH = 5;\n\nexport function isErrorLike(v: unknown): v is Error {\n if (v instanceof Error) return true;\n return (\n typeof v === \"object\" &&\n v !== null &&\n typeof (v as { message?: unknown }).message === \"string\" &&\n (\"stack\" in v || \"name\" in v)\n );\n}\n\nfunction errorType(e: Error): string {\n // pg's DatabaseError keeps the lowercase name \"error\"; its class name says more.\n if (e.name && e.name !== \"Error\" && e.name !== \"error\") return e.name;\n const ctor = (e as { constructor?: { name?: string } }).constructor?.name;\n return ctor && ctor !== \"Object\" ? ctor : \"Error\";\n}\n\nexport function serializeError(err: unknown): ErrFields {\n if (!isErrorLike(err)) {\n if (typeof err === \"string\") return { err_type: \"string\", err_message: err };\n return { err_type: typeof err, err_message: defaultRedactor.json(err) };\n }\n const e = err as Error & { code?: unknown; cause?: unknown; table?: unknown; constraint?: unknown };\n const out: ErrFields = { err_type: errorType(e), err_message: String(e.message ?? \"\") };\n if (e.code !== undefined && e.code !== null) out.err_code = String(e.code);\n if (isSqlState(out.err_code)) {\n out.err_message = stripSql(out.err_message ?? \"\");\n if (typeof e.table === \"string\" && e.table) out.db_table = e.table;\n if (typeof e.constraint === \"string\" && e.constraint) out.db_constraint = e.constraint;\n }\n if (typeof e.stack === \"string\") out.err_stack = e.stack;\n\n const causes: string[] = [];\n let cause = e.cause;\n while (cause !== undefined && cause !== null && causes.length < MAX_CAUSE_DEPTH) {\n if (isErrorLike(cause)) {\n causes.push(`${errorType(cause)}: ${cause.message}`);\n cause = (cause as { cause?: unknown }).cause;\n } else {\n causes.push(defaultRedactor.json(cause));\n cause = undefined;\n }\n }\n if (causes.length > 0) out.err_cause = causes.join(\" <- \");\n return out;\n}\n\n// Postgres SQLSTATE codes: five digits or uppercase letters (23505, 40P01, 25P02).\nfunction isSqlState(code: string | undefined): boolean {\n return code !== undefined && /^[0-9A-Z]{5}$/.test(code);\n}\n\nconst SQL_START_RE = /^\\s*(?:select|insert|update|delete|with|create|alter|drop|truncate|lock)\\b/i;\n\n/**\n * knex prefixes a failed query's message with the whole statement (\"update ... - deadlock\n * detected\"). A long statement filled err_message and the truncation cut off the server's reason,\n * the only part worth reading. Keeps the reason; the statement never reaches the log.\n */\nexport function stripSql(message: string): string {\n if (!SQL_START_RE.test(message)) return message;\n const at = message.lastIndexOf(\" - \");\n return at === -1 ? message : message.slice(at + 3);\n}\n\n// The client hung up or sent a malformed request: worth a warn, not an error on our side.\nexport function isClientDisconnect(err: unknown): boolean {\n const e = err as { code?: unknown; message?: unknown } | null;\n const code = typeof e?.code === \"string\" ? e.code : \"\";\n return (\n code === \"ECONNRESET\" ||\n code === \"ECONNABORTED\" ||\n code === \"EPIPE\" ||\n code === \"ERR_STREAM_PREMATURE_CLOSE\" ||\n code.startsWith(\"HPE_\") ||\n e?.message === \"aborted\"\n );\n}\n\nconst logged = globalSingleton(\"logged\", () => new WeakSet<object>());\n\n// Marks an error as already written, so a later boundary (e.g. Next's onRequestError)\n// does not log it a second time.\nexport function markLogged(err: unknown): void {\n if (typeof err === \"object\" && err !== null) logged.add(err);\n}\n\nexport function isLogged(err: unknown): boolean {\n return typeof err === \"object\" && err !== null && logged.has(err);\n}\n","import { getContext, withContext } from \"./context\";\nimport { markLogged } from \"./error\";\nimport type { Logger } from \"./logger\";\nimport type { Fields } from \"./normalize\";\n\nexport interface JobInfo {\n job_name: string;\n job_id?: string | number;\n job_attempt?: number;\n /** No retries left after this attempt (or decided per error). Defaults to true when unknown. */\n final_attempt?: boolean | ((err: unknown) => boolean);\n request_id?: string;\n fields?: Fields;\n}\n\nexport async function runJob<T>(log: Logger, info: JobInfo, fn: () => T | Promise<T>): Promise<T> {\n const ctx: Fields = { ...info.fields, job_name: info.job_name };\n if (info.job_id !== undefined) ctx.job_id = String(info.job_id);\n if (info.job_attempt !== undefined) ctx.job_attempt = info.job_attempt;\n if (info.request_id !== undefined) ctx.request_id = info.request_id;\n\n return withContext(ctx, async () => {\n const start = performance.now();\n try {\n const result = await fn();\n log.info(\"job completed\", { event: \"job.completed\", duration_ms: Math.round(performance.now() - start) });\n return result;\n } catch (err) {\n const final = typeof info.final_attempt === \"function\" ? info.final_attempt(err) : (info.final_attempt ?? true);\n log[final ? \"error\" : \"warn\"](\"job failed\", {\n event: \"job.failed\",\n duration_ms: Math.round(performance.now() - start),\n err,\n });\n markLogged(err);\n throw err;\n }\n });\n}\n\n/** Adds the current request_id to a job payload, so the job's logs link back to the request. */\nexport function jobData<T extends Record<string, unknown>>(data: T): T & { request_id?: string } {\n const requestId = getContext().request_id;\n if (typeof requestId !== \"string\" || data.request_id !== undefined) return data;\n return { ...data, request_id: requestId };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,8BAAkC;;;ACE3B,SAAS,gBAAmB,MAAc,MAAkB;AACjE,QAAM,MAAM,uBAAO,IAAI,iBAAiB,IAAI,EAAE;AAC9C,QAAM,IAAI;AACV,MAAI,QAAQ,EAAE,GAAG;AACjB,MAAI,UAAU,QAAW;AACvB,YAAQ,KAAK;AACb,MAAE,GAAG,IAAI;AAAA,EACX;AACA,SAAO;AACT;;;ADCA,IAAM,MAAM,gBAAgB,OAAO,MAAM,IAAI,0CAAgC,CAAC;AAIvE,SAAS,aAAgB,QAAgB,IAAmC;AACjF,QAAM,SAAS,IAAI,SAAS;AAC5B,QAAM,QAAsB,EAAE,QAAQ,EAAE,GAAG,QAAQ,QAAQ,GAAG,OAAO,GAAG,OAAO;AAC/E,SAAO,IAAI,IAAI,OAAO,MAAM,GAAG,KAAK,CAAC;AACvC;AAEO,SAAS,YAAe,QAAgB,IAAgB;AAC7D,SAAO,aAAa,QAAQ,MAAM,GAAG,CAAC;AACxC;;;AEuEA,IAAM,SAAS,gBAAgB,UAAU,MAAM,oBAAI,QAAgB,CAAC;AAI7D,SAAS,WAAW,KAAoB;AAC7C,MAAI,OAAO,QAAQ,YAAY,QAAQ,KAAM,QAAO,IAAI,GAAG;AAC7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;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/bullmq.ts","../src/context.ts","../src/global.ts","../src/error.ts","../src/job.ts"],"sourcesContent":["import { runJob } from \"./job\";\nimport type { Logger } from \"./logger\";\n\n/** Minimal shape of a BullMQ Job; avoids depending on bullmq. */\nexport interface BullJobLike {\n id?: string;\n attemptsMade: number;\n opts?: { attempts?: number };\n data?: unknown;\n}\n\nexport function loggedProcessor<J extends BullJobLike, R, A extends unknown[] = [token?: string, signal?: AbortSignal]>(\n log: Logger,\n name: string,\n processor: (job: J, ...rest: A) => Promise<R>,\n): (job: J, ...rest: A) => Promise<R> {\n return (job, ...rest) => {\n const attempts = job.opts?.attempts ?? 1;\n const attempt = job.attemptsMade + 1;\n const requestId = (job.data as { request_id?: unknown } | undefined)?.request_id;\n return runJob(\n log,\n {\n job_name: name,\n job_id: job.id,\n job_attempt: attempt,\n // UnrecoverableError skips the remaining retries.\n final_attempt: (err) => attempt >= attempts || (err as { name?: unknown } | null)?.name === \"UnrecoverableError\",\n request_id: typeof requestId === \"string\" ? requestId : undefined,\n },\n () => processor(job, ...rest),\n );\n };\n}\n","import { AsyncLocalStorage } from \"node:async_hooks\";\nimport { globalSingleton } from \"./global\";\nimport type { Fields } from \"./normalize\";\n\nexport interface ContextStore {\n fields: Fields;\n error?: unknown;\n /** Route template captured while the router still knew it (Express error path). */\n route?: string;\n parent?: ContextStore;\n}\n\nconst als = globalSingleton(\"als\", () => new AsyncLocalStorage<ContextStore>());\n\nexport const STORE_KEY: symbol = Symbol.for(\"@tanngo11/log.store\");\n\nexport function runWithStore<T>(fields: Fields, fn: (store: ContextStore) => T): T {\n const parent = als.getStore();\n const store: ContextStore = { fields: { ...parent?.fields, ...fields }, parent };\n return als.run(store, () => fn(store));\n}\n\nexport function withContext<T>(fields: Fields, fn: () => T): T {\n return runWithStore(fields, () => fn());\n}\n\nexport function currentStore(): ContextStore | undefined {\n return als.getStore();\n}\n\nexport function getContext(): Fields {\n return als.getStore()?.fields ?? {};\n}\n\n// Applies to the current context and every enclosing one, so a field added inside a nested\n// withContext/runJob still reaches the request's http.request line.\nexport function addContext(fields: Fields): void {\n for (let s = als.getStore(); s; s = s.parent) Object.assign(s.fields, fields);\n}\n\n// Sets the error on the store and on enclosing stores that have none yet.\nexport function recordErrorIn(store: ContextStore, err: unknown): void {\n store.error = err;\n for (let s = store.parent; s; s = s.parent) if (s.error === undefined) s.error = err;\n}\n\n// Marks the error that the current request/job summary line should carry. Does not write a line.\nexport function recordError(err: unknown): void {\n const store = als.getStore();\n if (store) recordErrorIn(store, err);\n}\n\nexport function attachStore(target: object, store: ContextStore): void {\n (target as Record<symbol, ContextStore>)[STORE_KEY] = store;\n}\n\nexport function storeOf(target: unknown): ContextStore | undefined {\n if (typeof target !== \"object\" || target === null) return undefined;\n return (target as Record<symbol, ContextStore | undefined>)[STORE_KEY];\n}\n","// Singletons shared by every copy of this package loaded in the process (ESM and CJS builds,\n// or duplicated installs), so context and dedupe state never split.\nexport function globalSingleton<T>(name: string, make: () => T): T {\n const key = Symbol.for(`@tanngo11/log.${name}`);\n const g = globalThis as unknown as Record<symbol, T | undefined>;\n let value = g[key];\n if (value === undefined) {\n value = make();\n g[key] = value;\n }\n return value;\n}\n","import { globalSingleton } from \"./global\";\nimport { defaultRedactor } from \"./redact\";\n\nexport interface ErrFields {\n err_type?: string;\n err_message?: string;\n err_code?: string;\n err_stack?: string;\n err_cause?: string;\n /** Database errors (pg): the table and constraint the server reported. */\n db_table?: string;\n db_constraint?: string;\n}\n\nconst MAX_CAUSE_DEPTH = 5;\n\nexport function isErrorLike(v: unknown): v is Error {\n if (v instanceof Error) return true;\n return (\n typeof v === \"object\" &&\n v !== null &&\n typeof (v as { message?: unknown }).message === \"string\" &&\n (\"stack\" in v || \"name\" in v)\n );\n}\n\nfunction errorType(e: Error): string {\n // pg's DatabaseError keeps the lowercase name \"error\"; its class name says more.\n if (e.name && e.name !== \"Error\" && e.name !== \"error\") return e.name;\n const ctor = (e as { constructor?: { name?: string } }).constructor?.name;\n return ctor && ctor !== \"Object\" ? ctor : \"Error\";\n}\n\nexport function serializeError(err: unknown): ErrFields {\n if (!isErrorLike(err)) {\n if (typeof err === \"string\") return { err_type: \"string\", err_message: err };\n return { err_type: typeof err, err_message: defaultRedactor.json(err) };\n }\n const e = err as Error & { code?: unknown; cause?: unknown; table?: unknown; constraint?: unknown };\n const message = String(e.message ?? \"\");\n const out: ErrFields = { err_type: errorType(e), err_message: message };\n if (e.code !== undefined && e.code !== null) out.err_code = String(e.code);\n out.err_message = cleanMessage(message, out.err_code);\n if (isSqlState(out.err_code)) {\n if (typeof e.table === \"string\" && e.table) out.db_table = e.table;\n if (typeof e.constraint === \"string\" && e.constraint) out.db_constraint = e.constraint;\n }\n if (typeof e.stack === \"string\") out.err_stack = replaceOnce(e.stack, message, out.err_message);\n\n const causes: string[] = [];\n let cause = e.cause;\n while (cause !== undefined && cause !== null && causes.length < MAX_CAUSE_DEPTH) {\n if (isErrorLike(cause)) {\n const code = (cause as { code?: unknown }).code;\n const causeCode = code === undefined || code === null ? undefined : String(code);\n causes.push(`${errorType(cause)}: ${cleanMessage(String(cause.message ?? \"\"), causeCode)}`);\n cause = (cause as { cause?: unknown }).cause;\n } else {\n causes.push(defaultRedactor.json(cause));\n cause = undefined;\n }\n }\n if (causes.length > 0) out.err_cause = causes.join(\" <- \");\n return out;\n}\n\n// Postgres SQLSTATE codes: five digits or uppercase letters (23505, 40P01, 25P02).\nfunction isSqlState(code: string | undefined): boolean {\n return code !== undefined && /^[0-9A-Z]{5}$/.test(code);\n}\n\n// A statement, optionally behind comments or opening parentheses (\"(select ...) union ...\").\nconst SQL_START_RE = /^\\s*(?:\\/\\*[\\s\\S]*?\\*\\/\\s*)*\\(*\\s*(?:select|insert|update|delete|with|create|alter|drop|truncate|lock)\\b/i;\n// Without a SQLSTATE code, the part before \" - \" must also read like SQL, so a message such as\n// \"Update failed - retry later\" stays whole.\nconst SQL_BODY_RE = /[\"`]|\\b(?:from|into|set|values|where|table|returning)\\b/i;\n\n/**\n * knex prefixes every failed query's message with the statement, whatever the driver or error\n * (pg with or without a SQLSTATE, MySQL ER_*, SQLite, \"Connection terminated\"). Drops it.\n */\nfunction cleanMessage(message: string, code: string | undefined): string {\n const at = message.lastIndexOf(\" - \");\n if (at === -1 || !SQL_START_RE.test(message)) return message;\n if (!isSqlState(code) && !SQL_BODY_RE.test(message.slice(0, at))) return message;\n return stripSql(message);\n}\n\n// V8 renders the stack's first line from the message when .stack is first read. For knex errors\n// that is after the statement was prefixed, so the stack repeats it.\nfunction replaceOnce(text: string, find: string, replacement: string | undefined): string {\n if (replacement === undefined || find === replacement || find === \"\") return text;\n const at = text.indexOf(find);\n return at === -1 ? text : text.slice(0, at) + replacement + text.slice(at + find.length);\n}\n\n/**\n * knex prefixes a failed query's message with the whole statement (\"update ... - deadlock\n * detected\"). A long statement filled err_message and the truncation cut off the server's reason,\n * the only part worth reading. Keeps the reason; the statement never reaches the log. Cuts at the\n * last \" - \": a reason that contains \" - \" loses its start, which is safer than keeping part of a\n * statement that contains one (arithmetic, string literals).\n */\nexport function stripSql(message: string): string {\n if (!SQL_START_RE.test(message)) return message;\n const at = message.lastIndexOf(\" - \");\n return at === -1 ? message : message.slice(at + 3);\n}\n\n/**\n * For errors Koa emits on the inbound socket (strapiServerErrors): the client hung up or sent a\n * malformed request. Not valid for errors in general: knex's pool rejects with Error(\"aborted\")\n * when it is destroyed, and pg or outbound sockets reset with ECONNRESET.\n */\nexport function isClientDisconnect(err: unknown): boolean {\n const e = err as { code?: unknown; message?: unknown } | null;\n const code = typeof e?.code === \"string\" ? e.code : \"\";\n return (\n code === \"ECONNRESET\" ||\n code === \"ECONNABORTED\" ||\n code === \"EPIPE\" ||\n code === \"ERR_STREAM_PREMATURE_CLOSE\" ||\n code.startsWith(\"HPE_\") ||\n e?.message === \"aborted\"\n );\n}\n\nconst logged = globalSingleton(\"logged\", () => new WeakSet<object>());\n\n// Marks an error as already written, so a later boundary (e.g. Next's onRequestError)\n// does not log it a second time.\nexport function markLogged(err: unknown): void {\n if (typeof err === \"object\" && err !== null) logged.add(err);\n}\n\nexport function isLogged(err: unknown): boolean {\n return typeof err === \"object\" && err !== null && logged.has(err);\n}\n","import { getContext, withContext } from \"./context\";\nimport { markLogged } from \"./error\";\nimport type { Logger } from \"./logger\";\nimport type { Fields } from \"./normalize\";\n\nexport interface JobInfo {\n job_name: string;\n job_id?: string | number;\n job_attempt?: number;\n /** No retries left after this attempt (or decided per error). Defaults to true when unknown. */\n final_attempt?: boolean | ((err: unknown) => boolean);\n request_id?: string;\n fields?: Fields;\n}\n\nexport async function runJob<T>(log: Logger, info: JobInfo, fn: () => T | Promise<T>): Promise<T> {\n const ctx: Fields = { ...info.fields, job_name: info.job_name };\n if (info.job_id !== undefined) ctx.job_id = String(info.job_id);\n if (info.job_attempt !== undefined) ctx.job_attempt = info.job_attempt;\n if (info.request_id !== undefined) ctx.request_id = info.request_id;\n\n return withContext(ctx, async () => {\n const start = performance.now();\n try {\n const result = await fn();\n log.info(\"job completed\", { event: \"job.completed\", duration_ms: Math.round(performance.now() - start) });\n return result;\n } catch (err) {\n const final = typeof info.final_attempt === \"function\" ? info.final_attempt(err) : (info.final_attempt ?? true);\n log[final ? \"error\" : \"warn\"](\"job failed\", {\n event: \"job.failed\",\n duration_ms: Math.round(performance.now() - start),\n err,\n });\n markLogged(err);\n throw err;\n }\n });\n}\n\n/** Adds the current request_id to a job payload, so the job's logs link back to the request. */\nexport function jobData<T extends Record<string, unknown>>(data: T): T & { request_id?: string } {\n const requestId = getContext().request_id;\n if (typeof requestId !== \"string\" || data.request_id !== undefined) return data;\n return { ...data, request_id: requestId };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,8BAAkC;;;ACE3B,SAAS,gBAAmB,MAAc,MAAkB;AACjE,QAAM,MAAM,uBAAO,IAAI,iBAAiB,IAAI,EAAE;AAC9C,QAAM,IAAI;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1G,UAAI,QAAQ,UAAU,MAAM,EAAE,cAAc;AAAA,QAC1C,OAAO;AAAA,QACP,aAAa,KAAK,MAAM,YAAY,IAAI,IAAI,KAAK;AAAA,QACjD;AAAA,MACF,CAAC;AACD,iBAAW,GAAG;AACd,YAAM;AAAA,IACR;AAAA,EACF,CAAC;AACH;;;AJ3BO,SAAS,gBACd,KACA,MACA,WACoC;AACpC,SAAO,CAAC,QAAQ,SAAS;AACvB,UAAM,WAAW,IAAI,MAAM,YAAY;AACvC,UAAM,UAAU,IAAI,eAAe;AACnC,UAAM,YAAa,IAAI,MAA+C;AACtE,WAAO;AAAA,MACL;AAAA,MACA;AAAA,QACE,UAAU;AAAA,QACV,QAAQ,IAAI;AAAA,QACZ,aAAa;AAAA;AAAA,QAEb,eAAe,CAAC,QAAQ,WAAW,YAAa,KAAmC,SAAS;AAAA,QAC5F,YAAY,OAAO,cAAc,WAAW,YAAY;AAAA,MAC1D;AAAA,MACA,MAAM,UAAU,KAAK,GAAG,IAAI;AAAA,IAC9B;AAAA,EACF;AACF;","names":[]}
package/dist/bullmq.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { L as Logger } from './logger-3Co9eVJf.cjs';
1
+ import { L as Logger } from './logger-CtlC9SUE.cjs';
2
2
 
3
3
  /** Minimal shape of a BullMQ Job; avoids depending on bullmq. */
4
4
  interface BullJobLike {
package/dist/bullmq.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { L as Logger } from './logger-3Co9eVJf.js';
1
+ import { L as Logger } from './logger-CtlC9SUE.js';
2
2
 
3
3
  /** Minimal shape of a BullMQ Job; avoids depending on bullmq. */
4
4
  interface BullJobLike {
package/dist/bullmq.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  runJob
3
- } from "./chunk-2WNGXGP7.js";
4
- import "./chunk-UDS6S22P.js";
3
+ } from "./chunk-MGAEY7UZ.js";
4
+ import "./chunk-JKCR57WJ.js";
5
5
 
6
6
  // src/bullmq.ts
7
7
  function loggedProcessor(log, name, processor) {
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  globalSingleton
3
- } from "./chunk-UDS6S22P.js";
3
+ } from "./chunk-JKCR57WJ.js";
4
4
 
5
5
  // src/process.ts
6
6
  function logStartup(log, fields = {}) {
@@ -83,4 +83,4 @@ export {
83
83
  installProcessHandlers,
84
84
  logProcessWarnings
85
85
  };
86
- //# sourceMappingURL=chunk-URPHVC4Y.js.map
86
+ //# sourceMappingURL=chunk-2IIUQFMK.js.map
@@ -5,12 +5,11 @@ import {
5
5
  import {
6
6
  REDACTED,
7
7
  getContext,
8
- isClientDisconnect,
9
8
  isErrorLike,
10
9
  markLogged,
11
10
  serializeError,
12
11
  toSnakeCase
13
- } from "./chunk-UDS6S22P.js";
12
+ } from "./chunk-JKCR57WJ.js";
14
13
 
15
14
  // src/normalize.ts
16
15
  var HOOK_EXEMPT = /* @__PURE__ */ new Set(["level", "message", "event"]);
@@ -184,15 +183,12 @@ function shouldSkip(method, path, skipPaths = DEFAULT_SKIP_PATHS) {
184
183
  }
185
184
  function logHttpRequest(log, info, opts = {}) {
186
185
  if (info.status < 500 && shouldSkip(info.method, info.path, opts.skipPaths)) return;
187
- const hasErr = info.err !== void 0 && info.err !== null;
188
- const clientGone = hasErr && isClientDisconnect(info.err);
189
- const level = info.status >= 500 && !clientGone ? "error" : info.status >= 400 ? "warn" : "info";
186
+ const level = info.status >= 500 ? "error" : info.status >= 400 ? "warn" : "info";
190
187
  const path = stripUrl(info.path);
191
188
  const errFields = {};
192
- if (hasErr) {
189
+ if (info.err !== void 0 && info.err !== null) {
193
190
  Object.assign(errFields, serializeError(info.err));
194
- if (info.status < 500 || clientGone) delete errFields.err_stack;
195
- if (clientGone) errFields.client_disconnect = true;
191
+ if (info.status < 500) delete errFields.err_stack;
196
192
  markLogged(info.err);
197
193
  }
198
194
  const message = info.route ? `${info.method} ${info.route} ${info.status}` : `${info.method} ${info.status}`;
@@ -216,4 +212,4 @@ export {
216
212
  DEFAULT_SKIP_PATHS,
217
213
  logHttpRequest
218
214
  };
219
- //# sourceMappingURL=chunk-AF4YR4Y4.js.map
215
+ //# sourceMappingURL=chunk-BFMFXDIJ.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/normalize.ts","../src/http.ts"],"sourcesContent":["import { isErrorLike, serializeError } from \"./error\";\nimport type { Level } from \"./levels\";\nimport { REDACTED, toSnakeCase, type Redactor } from \"./redact\";\n\nexport { DEFAULT_REDACT_KEYS, toSnakeCase } from \"./redact\";\n\n/** Final per-field hook: return a replacement, or undefined to drop the field. */\nexport type RedactHook = (key: string, value: Primitive) => unknown;\n\nexport type Fields = Record<string, unknown>;\nexport type Primitive = string | number | boolean | null;\nexport type LogRecord = Record<string, Primitive>;\n\nexport interface NormalizeOptions {\n snakeCase: boolean;\n redactor: Redactor;\n hook?: RedactHook;\n}\n\n// Fields the redact hook never sees, so every line keeps its level, message and event.\nconst HOOK_EXEMPT = new Set([\"level\", \"message\", \"event\"]);\n\n// Keys owned by Vector (overwritten or deleted on ingest) or by the logger itself.\nexport const RESERVED_KEYS = new Set([\n \"project\", \"service\", \"vps\", \"version\", \"container_name\", \"container_id\", \"image\", \"stream\",\n \"_timestamp\", \"label\", \"host\", \"source_type\", \"msg\", \"lvl\", \"severity\", \"level\", \"message\",\n]);\n\nexport const MAX_LINE_BYTES = 16000;\nconst MAX_MESSAGE = 500;\nconst MAX_STACK = 4000;\nconst MAX_STRING = 1000;\nconst MAX_DEPTH = 3;\nconst TRUNCATED = \"…[truncated]\";\n\n// Fields kept when a line is too large.\nconst KEEP_ON_SHRINK = new Set([\n \"level\", \"message\", \"event\", \"request_id\", \"trace_id\", \"span_id\", \"job_name\", \"job_id\", \"job_attempt\",\n \"user_id\", \"tenant_id\", \"err_type\", \"err_message\", \"err_code\",\n]);\n\nexport function stripUrl(url: string): string {\n const noQuery = url.split(/[?#]/, 1)[0] ?? \"\";\n return noQuery.replace(/^([a-z][a-z0-9+.-]*:\\/\\/)[^/@\\s]*@/i, \"$1\");\n}\n\nexport function truncate(s: string, max: number): string {\n return s.length > max ? s.slice(0, max - TRUNCATED.length) + TRUNCATED : s;\n}\n\nfunction isPlainObject(v: unknown): v is Fields {\n if (typeof v !== \"object\" || v === null) return false;\n const proto = Object.getPrototypeOf(v);\n return proto === Object.prototype || proto === null;\n}\n\nfunction isUrlKey(key: string): boolean {\n return key === \"http_path\" || key === \"http_url\" || key.endsWith(\"_url\");\n}\n\nfunction flattenInto(out: LogRecord, prefix: string, obj: Fields, depth: number, opts: NormalizeOptions): void {\n for (const [rawKey, value] of Object.entries(obj)) {\n const key = opts.snakeCase ? toSnakeCase(rawKey) : rawKey;\n const full = prefix ? `${prefix}_${key}` : key;\n if (value === undefined || typeof value === \"function\" || typeof value === \"symbol\") continue;\n if (value !== null && (opts.redactor.isSensitive(rawKey) || opts.redactor.isSensitive(full))) {\n out[full] = REDACTED;\n continue;\n }\n if (value === null || typeof value === \"string\" || typeof value === \"boolean\") {\n out[full] = value;\n } else if (typeof value === \"number\") {\n out[full] = Number.isFinite(value) ? value : String(value);\n } else if (typeof value === \"bigint\") {\n out[full] = value.toString();\n } else if (value instanceof Date) {\n out[full] = Number.isNaN(value.getTime()) ? \"Invalid Date\" : value.toISOString();\n } else if (isErrorLike(value)) {\n out[full] = `${value.name}: ${value.message}`;\n } else if (isPlainObject(value) && depth < MAX_DEPTH - 1) {\n flattenInto(out, full, value, depth + 1, opts);\n } else {\n // Arrays, deep objects, class instances: stringified with sensitive keys redacted.\n out[full] = opts.redactor.json(value);\n }\n }\n}\n\n// Correlation ids are opaque tokens: value patterns such as phoneVN can match inside a hex\n// segment and break the join between services, so a well-formed id is never scrubbed.\nconst ID_VALUE_RE = /^[A-Za-z0-9._:-]{1,128}$/;\n\nfunction isIdKey(key: string): boolean {\n return key === \"request_id\" || key === \"trace_id\" || key === \"span_id\" || key.endsWith(\"_request_id\");\n}\n\nfunction finalize(key: string, value: Primitive, opts: NormalizeOptions): Primitive {\n if (typeof value !== \"string\") return value;\n if (isIdKey(key) && ID_VALUE_RE.test(value)) return value;\n const v = opts.redactor.scrub(isUrlKey(key) ? stripUrl(value) : value);\n return truncate(v, key === \"err_stack\" ? MAX_STACK : MAX_STRING);\n}\n\nexport function buildRecord(\n level: Level,\n message: unknown,\n sources: (Fields | undefined)[],\n opts: NormalizeOptions,\n): LogRecord {\n const merged: Fields = {};\n for (const source of sources) if (source) Object.assign(merged, source);\n\n let err: unknown;\n if (merged.err !== undefined && merged.err !== null) {\n err = merged.err;\n delete merged.err;\n } else if (isErrorLike(merged.error)) {\n err = merged.error;\n delete merged.error;\n }\n\n const flat: LogRecord = {};\n flattenInto(flat, \"\", merged, 0, opts);\n if (err !== undefined) Object.assign(flat, serializeError(err));\n\n const text = typeof message === \"string\" ? message : opts.redactor.json(message);\n const out: LogRecord = { level, message: truncate(opts.redactor.scrub(text), MAX_MESSAGE) };\n for (const [k, v] of Object.entries(flat)) {\n const key = RESERVED_KEYS.has(k) ? `app_${k}` : k;\n let value = finalize(key, v, opts);\n if (opts.hook && !HOOK_EXEMPT.has(key)) {\n const replaced = opts.hook(key, value);\n if (replaced === undefined) continue;\n value = typeof replaced === \"object\" && replaced !== null ? opts.redactor.json(replaced) : (replaced as Primitive);\n }\n out[key] = value;\n }\n return out;\n}\n\nfunction byteLength(s: string): number {\n // UTF-8 uses at most 3 bytes per UTF-16 code unit: skip encoding short lines.\n if (s.length * 3 <= MAX_LINE_BYTES) return s.length;\n return new TextEncoder().encode(s).length;\n}\n\nexport function serializeRecord(record: LogRecord): string {\n const line = JSON.stringify(record);\n if (byteLength(line) <= MAX_LINE_BYTES) return line;\n const small: LogRecord = {};\n for (const [k, v] of Object.entries(record)) {\n if (KEEP_ON_SHRINK.has(k) || k.startsWith(\"http_\")) small[k] = v;\n }\n small.log_truncated = true;\n return JSON.stringify(small);\n}\n","import { getContext } from \"./context\";\nimport { markLogged, serializeError } from \"./error\";\nimport type { Logger } from \"./logger\";\nimport { stripUrl, type Fields } from \"./normalize\";\nimport { parseTraceparent, resolveRequestId } from \"./request-id\";\n\nexport { newRequestId, parseTraceparent, REQUEST_ID_RE, resolveRequestId } from \"./request-id\";\n\nexport type HeaderSource = Headers | Record<string, string | string[] | undefined>;\n\nfunction header(headers: HeaderSource, name: string): string | string[] | undefined {\n if (typeof (headers as Headers).get === \"function\") return (headers as Headers).get(name) ?? undefined;\n return (headers as Record<string, string | string[] | undefined>)[name];\n}\n\nexport function requestContextFields(headers: HeaderSource): { request_id: string; trace_id?: string; span_id?: string } {\n const out: { request_id: string; trace_id?: string; span_id?: string } = {\n request_id: resolveRequestId(header(headers, \"x-request-id\")),\n };\n const tp = parseTraceparent(header(headers, \"traceparent\"));\n if (tp) Object.assign(out, tp);\n return out;\n}\n\n// Headers to forward to services we own, so their logs share request_id / trace_id.\nexport function propagationHeaders(): Record<string, string> {\n const ctx = getContext();\n const out: Record<string, string> = {};\n if (typeof ctx.request_id === \"string\") out[\"x-request-id\"] = ctx.request_id;\n if (typeof ctx.trace_id === \"string\" && typeof ctx.span_id === \"string\") {\n out.traceparent = `00-${ctx.trace_id}-${ctx.span_id}-01`;\n }\n return out;\n}\n\nexport const DEFAULT_SKIP_PATHS: (string | RegExp)[] = [\n // Also under a prefix such as /api/v1/health.\n /(^|\\/)(health|healthz|ready|readyz|livez)\\/?$/,\n \"/favicon.ico\",\n /^\\/_next\\/static\\//,\n];\n\nexport interface HttpLogOptions {\n skipPaths?: (string | RegExp)[];\n}\n\nexport interface HttpRequestInfo {\n method: string;\n route?: string;\n path: string;\n status: number;\n duration_ms?: number;\n err?: unknown;\n fields?: Fields;\n}\n\nexport function shouldSkip(method: string, path: string, skipPaths: (string | RegExp)[] = DEFAULT_SKIP_PATHS): boolean {\n if (method.toUpperCase() === \"OPTIONS\") return true;\n const p = stripUrl(path);\n return skipPaths.some((s) => (typeof s === \"string\" ? p === s : s.test(p)));\n}\n\n// Writes the one `http.request` summary line for an inbound request. Skipped paths are still\n// logged when they fail with a 5xx, so a failing health check stays visible.\nexport function logHttpRequest(log: Logger, info: HttpRequestInfo, opts: HttpLogOptions = {}): void {\n if (info.status < 500 && shouldSkip(info.method, info.path, opts.skipPaths)) return;\n // The level follows the status only. An error's shape cannot tell a client hang-up apart:\n // knex's pool (tarn) rejects in-flight queries with Error(\"aborted\") when it is destroyed on\n // shutdown, and a pg or outbound socket reset is ECONNRESET too. Both are server-side failures.\n const level = info.status >= 500 ? \"error\" : info.status >= 400 ? \"warn\" : \"info\";\n const path = stripUrl(info.path);\n const errFields: Fields = {};\n if (info.err !== undefined && info.err !== null) {\n Object.assign(errFields, serializeError(info.err));\n if (info.status < 500) delete errFields.err_stack;\n markLogged(info.err);\n }\n // Route templates keep the message bounded; the raw path would make one message per URL.\n const message = info.route ? `${info.method} ${info.route} ${info.status}` : `${info.method} ${info.status}`;\n log[level](message, {\n ...info.fields,\n event: \"http.request\",\n http_method: info.method,\n http_route: info.route,\n http_path: path,\n http_status: info.status,\n duration_ms: info.duration_ms === undefined ? undefined : Math.round(info.duration_ms),\n ...errFields,\n });\n}\n"],"mappings":";;;;;;;;;;;;;;AAoBA,IAAM,cAAc,oBAAI,IAAI,CAAC,SAAS,WAAW,OAAO,CAAC;AAGlD,IAAM,gBAAgB,oBAAI,IAAI;AAAA,EACnC;AAAA,EAAW;AAAA,EAAW;AAAA,EAAO;AAAA,EAAW;AAAA,EAAkB;AAAA,EAAgB;AAAA,EAAS;AAAA,EACnF;AAAA,EAAc;AAAA,EAAS;AAAA,EAAQ;AAAA,EAAe;AAAA,EAAO;AAAA,EAAO;AAAA,EAAY;AAAA,EAAS;AACnF,CAAC;AAEM,IAAM,iBAAiB;AAC9B,IAAM,cAAc;AACpB,IAAM,YAAY;AAClB,IAAM,aAAa;AACnB,IAAM,YAAY;AAClB,IAAM,YAAY;AAGlB,IAAM,iBAAiB,oBAAI,IAAI;AAAA,EAC7B;AAAA,EAAS;AAAA,EAAW;AAAA,EAAS;AAAA,EAAc;AAAA,EAAY;AAAA,EAAW;AAAA,E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UAAU,KAAK,QAAQ,GAAG,KAAK,MAAM,IAAI,KAAK,KAAK,IAAI,KAAK,MAAM,KAAK,GAAG,KAAK,MAAM,IAAI,KAAK,MAAM;AAC1G,MAAI,KAAK,EAAE,SAAS;AAAA,IAClB,GAAG,KAAK;AAAA,IACR,OAAO;AAAA,IACP,aAAa,KAAK;AAAA,IAClB,YAAY,KAAK;AAAA,IACjB,WAAW;AAAA,IACX,aAAa,KAAK;AAAA,IAClB,aAAa,KAAK,gBAAgB,SAAY,SAAY,KAAK,MAAM,KAAK,WAAW;AAAA,IACrF,GAAG;AAAA,EACL,CAAC;AACH;","names":[]}
@@ -1,13 +1,13 @@
1
1
  import {
2
2
  logHttpRequest,
3
3
  requestContextFields
4
- } from "./chunk-AF4YR4Y4.js";
4
+ } from "./chunk-BFMFXDIJ.js";
5
5
  import {
6
6
  attachStore,
7
7
  recordErrorIn,
8
8
  runWithStore,
9
9
  storeOf
10
- } from "./chunk-UDS6S22P.js";
10
+ } from "./chunk-JKCR57WJ.js";
11
11
 
12
12
  // src/express.ts
13
13
  function joinRoute(baseUrl, path) {
@@ -69,4 +69,4 @@ export {
69
69
  requestLogger,
70
70
  errorRecorder
71
71
  };
72
- //# sourceMappingURL=chunk-UOIJCSP5.js.map
72
+ //# sourceMappingURL=chunk-EZCIMEVL.js.map
@@ -82,10 +82,11 @@ var DEFAULT_REDACT_KEYS = [
82
82
  ];
83
83
  var REDACT_PATTERNS = {
84
84
  /**
85
- * Vietnamese mobile numbers: 0912345678, +84912345678, 84912345678. Letters on either side
86
- * rule a match out, so hex ids (a UUID segment such as 0912345678e0) are left alone.
85
+ * Vietnamese mobile numbers: 0912345678, +84912345678, 84912345678, also "SDT0912345678". A
86
+ * hex letter (a-f) on either side rules a match out, so hex ids (a UUID segment such as
87
+ * 0912345678e0) are left alone.
87
88
  */
88
- phoneVN: /(?<![\dA-Za-z+])(?:\+?84|0)[35789]\d{8}(?![\dA-Za-z])/g,
89
+ phoneVN: /(?<![\dA-Fa-f+])(?:\+?84|0)[35789]\d{8}(?![\dA-Fa-f])/g,
89
90
  /** Visa, Mastercard, Amex, Discover-like card numbers, with optional spaces or dashes. */
90
91
  paymentCard: /(?<!\d)(?:4\d{3}|5[1-5]\d{2}|2[2-7]\d{2}|3[47]\d{2}|6(?:011|5\d{2}))(?:[ -]?\d{4}){2}[ -]?\d{1,4}(?!\d)/g,
91
92
  /** IPv4 addresses. */
@@ -157,19 +158,22 @@ function serializeError(err) {
157
158
  return { err_type: typeof err, err_message: defaultRedactor.json(err) };
158
159
  }
159
160
  const e = err;
160
- const out = { err_type: errorType(e), err_message: String(e.message ?? "") };
161
+ const message = String(e.message ?? "");
162
+ const out = { err_type: errorType(e), err_message: message };
161
163
  if (e.code !== void 0 && e.code !== null) out.err_code = String(e.code);
164
+ out.err_message = cleanMessage(message, out.err_code);
162
165
  if (isSqlState(out.err_code)) {
163
- out.err_message = stripSql(out.err_message ?? "");
164
166
  if (typeof e.table === "string" && e.table) out.db_table = e.table;
165
167
  if (typeof e.constraint === "string" && e.constraint) out.db_constraint = e.constraint;
166
168
  }
167
- if (typeof e.stack === "string") out.err_stack = e.stack;
169
+ if (typeof e.stack === "string") out.err_stack = replaceOnce(e.stack, message, out.err_message);
168
170
  const causes = [];
169
171
  let cause = e.cause;
170
172
  while (cause !== void 0 && cause !== null && causes.length < MAX_CAUSE_DEPTH) {
171
173
  if (isErrorLike(cause)) {
172
- causes.push(`${errorType(cause)}: ${cause.message}`);
174
+ const code = cause.code;
175
+ const causeCode = code === void 0 || code === null ? void 0 : String(code);
176
+ causes.push(`${errorType(cause)}: ${cleanMessage(String(cause.message ?? ""), causeCode)}`);
173
177
  cause = cause.cause;
174
178
  } else {
175
179
  causes.push(defaultRedactor.json(cause));
@@ -182,7 +186,19 @@ function serializeError(err) {
182
186
  function isSqlState(code) {
183
187
  return code !== void 0 && /^[0-9A-Z]{5}$/.test(code);
184
188
  }
185
- var SQL_START_RE = /^\s*(?:select|insert|update|delete|with|create|alter|drop|truncate|lock)\b/i;
189
+ var SQL_START_RE = /^\s*(?:\/\*[\s\S]*?\*\/\s*)*\(*\s*(?:select|insert|update|delete|with|create|alter|drop|truncate|lock)\b/i;
190
+ var SQL_BODY_RE = /["`]|\b(?:from|into|set|values|where|table|returning)\b/i;
191
+ function cleanMessage(message, code) {
192
+ const at = message.lastIndexOf(" - ");
193
+ if (at === -1 || !SQL_START_RE.test(message)) return message;
194
+ if (!isSqlState(code) && !SQL_BODY_RE.test(message.slice(0, at))) return message;
195
+ return stripSql(message);
196
+ }
197
+ function replaceOnce(text, find, replacement) {
198
+ if (replacement === void 0 || find === replacement || find === "") return text;
199
+ const at = text.indexOf(find);
200
+ return at === -1 ? text : text.slice(0, at) + replacement + text.slice(at + find.length);
201
+ }
186
202
  function stripSql(message) {
187
203
  if (!SQL_START_RE.test(message)) return message;
188
204
  const at = message.lastIndexOf(" - ");
@@ -224,4 +240,4 @@ export {
224
240
  markLogged,
225
241
  isLogged
226
242
  };
227
- //# sourceMappingURL=chunk-UDS6S22P.js.map
243
+ //# sourceMappingURL=chunk-JKCR57WJ.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/context.ts","../src/global.ts","../src/redact.ts","../src/error.ts"],"sourcesContent":["import { AsyncLocalStorage } from \"node:async_hooks\";\nimport { globalSingleton } from \"./global\";\nimport type { Fields } from \"./normalize\";\n\nexport interface ContextStore {\n fields: Fields;\n error?: unknown;\n /** Route template captured while the router still knew it (Express error path). */\n route?: string;\n parent?: ContextStore;\n}\n\nconst als = globalSingleton(\"als\", () => new AsyncLocalStorage<ContextStore>());\n\nexport const STORE_KEY: symbol = Symbol.for(\"@tanngo11/log.store\");\n\nexport function runWithStore<T>(fields: Fields, fn: (store: ContextStore) => T): T {\n const parent = als.getStore();\n const store: ContextStore = { fields: { ...parent?.fields, ...fields }, parent };\n return als.run(store, () => fn(store));\n}\n\nexport function withContext<T>(fields: Fields, fn: () => T): T {\n return runWithStore(fields, () => fn());\n}\n\nexport function currentStore(): ContextStore | undefined {\n return als.getStore();\n}\n\nexport function getContext(): Fields {\n return als.getStore()?.fields ?? {};\n}\n\n// Applies to the current context and every enclosing one, so a field added inside a nested\n// withContext/runJob still reaches the request's http.request line.\nexport function addContext(fields: Fields): void {\n for (let s = als.getStore(); s; s = s.parent) Object.assign(s.fields, fields);\n}\n\n// Sets the error on the store and on enclosing stores that have none yet.\nexport function recordErrorIn(store: ContextStore, err: unknown): void {\n store.error = err;\n for (let s = store.parent; s; s = s.parent) if (s.error === undefined) s.error = err;\n}\n\n// Marks the error that the current request/job summary line should carry. Does not write a line.\nexport function recordError(err: unknown): void {\n const store = als.getStore();\n if (store) recordErrorIn(store, err);\n}\n\nexport function attachStore(target: object, store: ContextStore): void {\n (target as Record<symbol, ContextStore>)[STORE_KEY] = store;\n}\n\nexport function storeOf(target: unknown): ContextStore | undefined {\n if (typeof target !== \"object\" || target === null) return undefined;\n return (target as Record<symbol, ContextStore | undefined>)[STORE_KEY];\n}\n","// Singletons shared by every copy of this package loaded in the process (ESM and CJS builds,\n// or duplicated installs), so context and dedupe state never split.\nexport function globalSingleton<T>(name: string, make: () => T): T {\n const key = Symbol.for(`@tanngo11/log.${name}`);\n const g = globalThis as unknown as Record<symbol, T | undefined>;\n let value = g[key];\n if (value === undefined) {\n value = make();\n g[key] = value;\n }\n return value;\n}\n","// Redaction shared by the logger and the error serializer. The collector redacts again,\n// but it cannot see secrets inside JSON-encoded strings, so the app side must catch them.\n\nexport const DEFAULT_REDACT_KEYS = [\n \"password\", \"passwd\", \"pwd\", \"passphrase\", \"secret\", \"client_secret\",\n \"token\", \"access_token\", \"refresh_token\", \"id_token\", \"auth_token\",\n \"api_key\", \"apikey\", \"access_key\", \"secret_key\", \"private_key\",\n \"authorization\", \"cookie\", \"set_cookie\", \"session_id\", \"sessionid\",\n \"credential\", \"credentials\", \"dsn\", \"connection_string\", \"conn_string\", \"jwt\", \"bearer\",\n];\n\n/** Opt-in value patterns for `redactValues`. Not on by default: they can hit ordinary numbers. */\nexport const REDACT_PATTERNS = {\n /**\n * Vietnamese mobile numbers: 0912345678, +84912345678, 84912345678, also \"SDT0912345678\". A\n * hex letter (a-f) on either side rules a match out, so hex ids (a UUID segment such as\n * 0912345678e0) are left alone.\n */\n phoneVN: /(?<![\\dA-Fa-f+])(?:\\+?84|0)[35789]\\d{8}(?![\\dA-Fa-f])/g,\n /** Visa, Mastercard, Amex, Discover-like card numbers, with optional spaces or dashes. */\n paymentCard: /(?<!\\d)(?:4\\d{3}|5[1-5]\\d{2}|2[2-7]\\d{2}|3[47]\\d{2}|6(?:011|5\\d{2}))(?:[ -]?\\d{4}){2}[ -]?\\d{1,4}(?!\\d)/g,\n /** IPv4 addresses. */\n ipv4: /(?<![\\d.])(?:\\d{1,3}\\.){3}\\d{1,3}(?![\\d.])/g,\n} as const;\n\n// Keys that only describe a secret (count, type, timestamps, flags) stay readable.\nconst SAFE_SUFFIXES = [\"_count\", \"_type\", \"_length\", \"_expires_at\", \"_expires_in\", \"_ttl\", \"_at\", \"_ms\", \"_enabled\"];\n\nexport const REDACTED = \"[REDACTED]\";\n\nexport function toSnakeCase(key: string): string {\n return key\n .replace(/([a-z0-9])([A-Z])/g, \"$1_$2\")\n .replace(/([A-Z]+)([A-Z][a-z])/g, \"$1_$2\")\n .replace(/[^A-Za-z0-9_]+/g, \"_\")\n .toLowerCase();\n}\n\nconst URL_CREDENTIALS_RE = /([a-z][a-z0-9+.-]*:\\/\\/)[^\\s/@:\"']+:[^\\s/@\"']+@/gi;\nconst BEARER_RE = /\\bBearer\\s+[A-Za-z0-9._~+/=-]+/gi;\nconst JWT_RE = /\\beyJ[A-Za-z0-9_-]+\\.[A-Za-z0-9_-]+\\.[A-Za-z0-9_-]+/g;\nconst EMAIL_RE = /[A-Za-z0-9._%+-]+@([A-Za-z0-9-]+(?:\\.[A-Za-z0-9-]+)*\\.[A-Za-z]{2,})/g;\n\nexport type KeyPattern = string | RegExp;\n\nexport interface RedactorOptions {\n /** Extra sensitive keys: names (matched like the defaults) or patterns tested on the snake_case key. */\n keys?: readonly KeyPattern[];\n /** Extra value patterns; every match inside any string becomes [REDACTED]. */\n values?: readonly RegExp[];\n}\n\nexport interface Redactor {\n isSensitive(key: string): boolean;\n /** Masks secrets and emails that appear inside free text. */\n scrub(s: string): string;\n /** JSON.stringify that redacts sensitive keys at any depth and never throws. */\n json(value: unknown): string;\n}\n\nfunction globalize(re: RegExp): RegExp {\n return re.flags.includes(\"g\") ? new RegExp(re.source, re.flags) : new RegExp(re.source, `${re.flags}g`);\n}\n\nexport function createRedactor(opts: RedactorOptions = {}): Redactor {\n const words = [...DEFAULT_REDACT_KEYS];\n const keyPatterns: RegExp[] = [];\n for (const k of opts.keys ?? []) {\n if (typeof k === \"string\") words.push(toSnakeCase(k));\n else keyPatterns.push(new RegExp(k.source, k.flags.replace(\"g\", \"\")));\n }\n const valuePatterns = (opts.values ?? []).map(globalize);\n\n const isSensitive = (key: string): boolean => {\n const k = toSnakeCase(key);\n if (keyPatterns.some((re) => re.test(k))) return true;\n if (!words.includes(k) && SAFE_SUFFIXES.some((s) => k.endsWith(s))) return false;\n return words.some((w) => k === w || k.startsWith(`${w}_`) || k.endsWith(`_${w}`) || k.includes(`_${w}_`));\n };\n\n const scrub = (s: string): string => {\n let out = s\n .replace(URL_CREDENTIALS_RE, `$1${REDACTED}@`)\n .replace(BEARER_RE, `Bearer ${REDACTED}`)\n .replace(JWT_RE, \"[REDACTED_JWT]\")\n .replace(EMAIL_RE, \"***@$1\");\n for (const re of valuePatterns) out = out.replace(re, REDACTED);\n return out;\n };\n\n const json = (value: unknown): string => {\n if (typeof value === \"string\") return value;\n try {\n const text = JSON.stringify(value, (key, val: unknown) => {\n if (key && val !== null && val !== undefined && isSensitive(key)) return REDACTED;\n if (typeof val === \"bigint\") return val.toString();\n if (val instanceof Error) return { name: val.name, message: val.message };\n return val;\n });\n return text ?? String(value);\n } catch {\n return String(value);\n }\n };\n\n return { isSensitive, scrub, json };\n}\n\nexport const defaultRedactor: Redactor = createRedactor();\n","import { globalSingleton } from \"./global\";\nimport { defaultRedactor } from \"./redact\";\n\nexport interface ErrFields {\n err_type?: string;\n err_message?: string;\n err_code?: string;\n err_stack?: string;\n err_cause?: string;\n /** Database errors (pg): the table and constraint the server reported. */\n db_table?: string;\n db_constraint?: string;\n}\n\nconst MAX_CAUSE_DEPTH = 5;\n\nexport function isErrorLike(v: unknown): v is Error {\n if (v instanceof Error) return true;\n return (\n typeof v === \"object\" &&\n v !== null &&\n typeof (v as { message?: unknown }).message === \"string\" &&\n (\"stack\" in v || \"name\" in v)\n );\n}\n\nfunction errorType(e: Error): string {\n // pg's DatabaseError keeps the lowercase name \"error\"; its class name says more.\n if (e.name && e.name !== \"Error\" && e.name !== \"error\") return e.name;\n const ctor = (e as { constructor?: { name?: string } }).constructor?.name;\n return ctor && ctor !== \"Object\" ? ctor : \"Error\";\n}\n\nexport function serializeError(err: unknown): ErrFields {\n if (!isErrorLike(err)) {\n if (typeof err === \"string\") return { err_type: \"string\", err_message: err };\n return { err_type: typeof err, err_message: defaultRedactor.json(err) };\n }\n const e = err as Error & { code?: unknown; cause?: unknown; table?: unknown; constraint?: unknown };\n const message = String(e.message ?? \"\");\n const out: ErrFields = { err_type: errorType(e), err_message: message };\n if (e.code !== undefined && e.code !== null) out.err_code = String(e.code);\n out.err_message = cleanMessage(message, out.err_code);\n if (isSqlState(out.err_code)) {\n if (typeof e.table === \"string\" && e.table) out.db_table = e.table;\n if (typeof e.constraint === \"string\" && e.constraint) out.db_constraint = e.constraint;\n }\n if (typeof e.stack === \"string\") out.err_stack = replaceOnce(e.stack, message, out.err_message);\n\n const causes: string[] = [];\n let cause = e.cause;\n while (cause !== undefined && cause !== null && causes.length < MAX_CAUSE_DEPTH) {\n if (isErrorLike(cause)) {\n const code = (cause as { code?: unknown }).code;\n const causeCode = code === undefined || code === null ? undefined : String(code);\n causes.push(`${errorType(cause)}: ${cleanMessage(String(cause.message ?? \"\"), causeCode)}`);\n cause = (cause as { cause?: unknown }).cause;\n } else {\n causes.push(defaultRedactor.json(cause));\n cause = undefined;\n }\n }\n if (causes.length > 0) out.err_cause = causes.join(\" <- \");\n return out;\n}\n\n// Postgres SQLSTATE codes: five digits or uppercase letters (23505, 40P01, 25P02).\nfunction isSqlState(code: string | undefined): boolean {\n return code !== undefined && /^[0-9A-Z]{5}$/.test(code);\n}\n\n// A statement, optionally behind comments or opening parentheses (\"(select ...) union ...\").\nconst SQL_START_RE = /^\\s*(?:\\/\\*[\\s\\S]*?\\*\\/\\s*)*\\(*\\s*(?:select|insert|update|delete|with|create|alter|drop|truncate|lock)\\b/i;\n// Without a SQLSTATE code, the part before \" - \" must also read like SQL, so a message such as\n// \"Update failed - retry later\" stays whole.\nconst SQL_BODY_RE = /[\"`]|\\b(?:from|into|set|values|where|table|returning)\\b/i;\n\n/**\n * knex prefixes every failed query's message with the statement, whatever the driver or error\n * (pg with or without a SQLSTATE, MySQL ER_*, SQLite, \"Connection terminated\"). Drops it.\n */\nfunction cleanMessage(message: string, code: string | undefined): string {\n const at = message.lastIndexOf(\" - \");\n if (at === -1 || !SQL_START_RE.test(message)) return message;\n if (!isSqlState(code) && !SQL_BODY_RE.test(message.slice(0, at))) return message;\n return stripSql(message);\n}\n\n// V8 renders the stack's first line from the message when .stack is first read. For knex errors\n// that is after the statement was prefixed, so the stack repeats it.\nfunction replaceOnce(text: string, find: string, replacement: string | undefined): string {\n if (replacement === undefined || find === replacement || find === \"\") return text;\n const at = text.indexOf(find);\n return at === -1 ? text : text.slice(0, at) + replacement + text.slice(at + find.length);\n}\n\n/**\n * knex prefixes a failed query's message with the whole statement (\"update ... - deadlock\n * detected\"). A long statement filled err_message and the truncation cut off the server's reason,\n * the only part worth reading. Keeps the reason; the statement never reaches the log. Cuts at the\n * last \" - \": a reason that contains \" - \" loses its start, which is safer than keeping part of a\n * statement that contains one (arithmetic, string literals).\n */\nexport function stripSql(message: string): string {\n if (!SQL_START_RE.test(message)) return message;\n const at = message.lastIndexOf(\" - \");\n return at === -1 ? message : message.slice(at + 3);\n}\n\n/**\n * For errors Koa emits on the inbound socket (strapiServerErrors): the client hung up or sent a\n * malformed request. Not valid for errors in general: knex's pool rejects with Error(\"aborted\")\n * when it is destroyed, and pg or outbound sockets reset with ECONNRESET.\n */\nexport function isClientDisconnect(err: unknown): boolean {\n const e = err as { code?: unknown; message?: unknown } | null;\n const code = typeof e?.code === \"string\" ? e.code : \"\";\n return (\n code === \"ECONNRESET\" ||\n code === \"ECONNABORTED\" ||\n code === \"EPIPE\" ||\n code === \"ERR_STREAM_PREMATURE_CLOSE\" ||\n code.startsWith(\"HPE_\") ||\n e?.message === \"aborted\"\n );\n}\n\nconst logged = globalSingleton(\"logged\", () => new WeakSet<object>());\n\n// Marks an error as already written, so a later boundary (e.g. Next's onRequestError)\n// does not log it a second time.\nexport function markLogged(err: unknown): void {\n if (typeof err === \"object\" && err !== null) logged.add(err);\n}\n\nexport function isLogged(err: unknown): boolean {\n return typeof err === \"object\" && err !== null && logged.has(err);\n}\n"],"mappings":";AAAA,SAAS,yBAAyB;;;ACE3B,SAAS,gBAAmB,MAAc,MAAkB;AACjE,QAAM,MAAM,uBAAO,IAAI,iBAAiB,IAAI,EAAE;AAC9C,QAAM,IAAI;AACV,MAAI,QAAQ,EAAE,GAAG;AACjB,MAAI,UAAU,QAAW;AACvB,YAAQ,KAAK;AACb,MAAE,GAAG,IAAI;AAAA,EACX;AACA,SAAO;AACT;;;ADCA,IAAM,MAAM,gBAAgB,OAAO,MAAM,IAAI,kBAAgC,CAAC;AAEvE,IAAM,YAAoB,uBAAO,IAAI,qBAAqB;AAE1D,SAAS,aAAgB,QAAgB,IAAmC;AACjF,QAAM,SAAS,IAAI,SAAS;AAC5B,QAAM,QAAsB,EAAE,QAAQ,EAAE,GAAG,QAAQ,QAAQ,GAAG,OAAO,GAAG,OAAO;AAC/E,SAAO,IAAI,IAAI,OAAO,MAAM,GAAG,KAAK,CAAC;AACvC;AAEO,SAAS,YAAe,QAA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ged,
4
4
  withContext
5
- } from "./chunk-UDS6S22P.js";
5
+ } from "./chunk-JKCR57WJ.js";
6
6
 
7
7
  // src/job.ts
8
8
  async function runJob(log, info, fn) {
@@ -38,4 +38,4 @@ export {
38
38
  runJob,
39
39
  jobData
40
40
  };
41
- //# sourceMappingURL=chunk-2WNGXGP7.js.map
41
+ //# sourceMappingURL=chunk-MGAEY7UZ.js.map
@@ -1,13 +1,13 @@
1
1
  import {
2
2
  logHttpRequest,
3
3
  requestContextFields
4
- } from "./chunk-AF4YR4Y4.js";
4
+ } from "./chunk-BFMFXDIJ.js";
5
5
  import {
6
6
  attachStore,
7
7
  recordErrorIn,
8
8
  runWithStore,
9
9
  storeOf
10
- } from "./chunk-UDS6S22P.js";
10
+ } from "./chunk-JKCR57WJ.js";
11
11
 
12
12
  // src/fastify.ts
13
13
  var starts = /* @__PURE__ */ new WeakMap();
@@ -63,4 +63,4 @@ var fastifyLogger = plugin;
63
63
  export {
64
64
  fastifyLogger
65
65
  };
66
- //# sourceMappingURL=chunk-RVETZAK5.js.map
66
+ //# sourceMappingURL=chunk-RQWNJJIT.js.map
package/dist/express.cjs CHANGED
@@ -157,19 +157,22 @@ function serializeError(err) {
157
157
  return { err_type: typeof err, err_message: defaultRedactor.json(err) };
158
158
  }
159
159
  const e = err;
160
- const out = { err_type: errorType(e), err_message: String(e.message ?? "") };
160
+ const message = String(e.message ?? "");
161
+ const out = { err_type: errorType(e), err_message: message };
161
162
  if (e.code !== void 0 && e.code !== null) out.err_code = String(e.code);
163
+ out.err_message = cleanMessage(message, out.err_code);
162
164
  if (isSqlState(out.err_code)) {
163
- out.err_message = stripSql(out.err_message ?? "");
164
165
  if (typeof e.table === "string" && e.table) out.db_table = e.table;
165
166
  if (typeof e.constraint === "string" && e.constraint) out.db_constraint = e.constraint;
166
167
  }
167
- if (typeof e.stack === "string") out.err_stack = e.stack;
168
+ if (typeof e.stack === "string") out.err_stack = replaceOnce(e.stack, message, out.err_message);
168
169
  const causes = [];
169
170
  let cause = e.cause;
170
171
  while (cause !== void 0 && cause !== null && causes.length < MAX_CAUSE_DEPTH) {
171
172
  if (isErrorLike(cause)) {
172
- causes.push(`${errorType(cause)}: ${cause.message}`);
173
+ const code = cause.code;
174
+ const causeCode = code === void 0 || code === null ? void 0 : String(code);
175
+ causes.push(`${errorType(cause)}: ${cleanMessage(String(cause.message ?? ""), causeCode)}`);
173
176
  cause = cause.cause;
174
177
  } else {
175
178
  causes.push(defaultRedactor.json(cause));
@@ -182,17 +185,24 @@ function serializeError(err) {
182
185
  function isSqlState(code) {
183
186
  return code !== void 0 && /^[0-9A-Z]{5}$/.test(code);
184
187
  }
185
- var SQL_START_RE = /^\s*(?:select|insert|update|delete|with|create|alter|drop|truncate|lock)\b/i;
188
+ var SQL_START_RE = /^\s*(?:\/\*[\s\S]*?\*\/\s*)*\(*\s*(?:select|insert|update|delete|with|create|alter|drop|truncate|lock)\b/i;
189
+ var SQL_BODY_RE = /["`]|\b(?:from|into|set|values|where|table|returning)\b/i;
190
+ function cleanMessage(message, code) {
191
+ const at = message.lastIndexOf(" - ");
192
+ if (at === -1 || !SQL_START_RE.test(message)) return message;
193
+ if (!isSqlState(code) && !SQL_BODY_RE.test(message.slice(0, at))) return message;
194
+ return stripSql(message);
195
+ }
196
+ function replaceOnce(text, find, replacement) {
197
+ if (replacement === void 0 || find === replacement || find === "") return text;
198
+ const at = text.indexOf(find);
199
+ return at === -1 ? text : text.slice(0, at) + replacement + text.slice(at + find.length);
200
+ }
186
201
  function stripSql(message) {
187
202
  if (!SQL_START_RE.test(message)) return message;
188
203
  const at = message.lastIndexOf(" - ");
189
204
  return at === -1 ? message : message.slice(at + 3);
190
205
  }
191
- function isClientDisconnect(err) {
192
- const e = err;
193
- const code = typeof e?.code === "string" ? e.code : "";
194
- return code === "ECONNRESET" || code === "ECONNABORTED" || code === "EPIPE" || code === "ERR_STREAM_PREMATURE_CLOSE" || code.startsWith("HPE_") || e?.message === "aborted";
195
- }
196
206
  var logged = globalSingleton("logged", () => /* @__PURE__ */ new WeakSet());
197
207
  function markLogged(err) {
198
208
  if (typeof err === "object" && err !== null) logged.add(err);
@@ -252,15 +262,12 @@ function shouldSkip(method, path, skipPaths = DEFAULT_SKIP_PATHS) {
252
262
  }
253
263
  function logHttpRequest(log, info, opts = {}) {
254
264
  if (info.status < 500 && shouldSkip(info.method, info.path, opts.skipPaths)) return;
255
- const hasErr = info.err !== void 0 && info.err !== null;
256
- const clientGone = hasErr && isClientDisconnect(info.err);
257
- const level = info.status >= 500 && !clientGone ? "error" : info.status >= 400 ? "warn" : "info";
265
+ const level = info.status >= 500 ? "error" : info.status >= 400 ? "warn" : "info";
258
266
  const path = stripUrl(info.path);
259
267
  const errFields = {};
260
- if (hasErr) {
268
+ if (info.err !== void 0 && info.err !== null) {
261
269
  Object.assign(errFields, serializeError(info.err));
262
- if (info.status < 500 || clientGone) delete errFields.err_stack;
263
- if (clientGone) errFields.client_disconnect = true;
270
+ if (info.status < 500) delete errFields.err_stack;
264
271
  markLogged(info.err);
265
272
  }
266
273
  const message = info.route ? `${info.method} ${info.route} ${info.status}` : `${info.method} ${info.status}`;