@autofleet/logger 4.2.34 → 4.2.36
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/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
Object.defineProperty(exports,`__esModule`,{value:!0});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`pino`);c=s(c);const l={trace:`DEBUG`,debug:`DEBUG`,info:`INFO`,warn:`WARNING`,error:`ERROR`,fatal:`CRITICAL`};function u(e,t={}){let{serviceName:n,version:r}=t;return{base:n&&r?{serviceContext:{service:n,version:r}}:null,formatters:{level:e=>({severity:l[e]??l.info,...[`error`,`fatal`].includes(e)&&{"@type":`type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent`}}),log(e){let t=e,n=t.err?.stack??t.error?.stack??t.e?.stack;return{...e,...n&&{stack_trace:n}}}},messageKey:`message`,...e}}let d=function(e){return e.trace=`trace`,e.debug=`debug`,e.info=`info`,e.warn=`warn`,e.error=`error`,e.fatal=`fatal`,e}({});const f=e=>e||(process.env.LOG_LEVEL?process.env.LOG_LEVEL:process.env.NODE_ENV&&[`production`,`staging`,`test`].includes(process.env.NODE_ENV)?d.info:(process.env.NODE_ENV,d.debug)),p=(e,t,n)=>{let r;return r=process.env.NODE_ENV===`production`?(0,c.default)(u({level:process.env.PINO_LOG_LEVEL||`info`,...t},n)):(0,c.default)({level:process.env.PINO_LOG_LEVEL||`info`,transport:{target:`pino-pretty`,options:{colorize:!0}},...t}),r.level=e,r};var m=class{#e=[];#t;constructor(e,t){this.addContextMiddleware=e=>this.#e.push(e),this.trace=(e,t)=>this.winstonLikeLoggerCall(d.trace,e,t),this.debug=(e,t)=>this.winstonLikeLoggerCall(d.debug,e,t),this.info=(e,t)=>this.winstonLikeLoggerCall(d.info,e,t),this.warn=(e,t)=>this.winstonLikeLoggerCall(d.warn,e,t),this.error=(e,t)=>this.winstonLikeLoggerCall(d.error,e,t),this.fatal=(e,t)=>this.winstonLikeLoggerCall(d.fatal,e,t),this.child=e=>this.#t.child(e),this.#t=p(f(e),{mixin:()=>this.addMetadata(),serializers:{...c.stdSerializers,e:c.stdSerializers.errWithCause,err:c.stdSerializers.errWithCause,error:c.stdSerializers.errWithCause,errors(e){return Array.isArray(e)?e.map(c.stdSerializers.errWithCause):c.stdSerializers.errWithCause(e)}}},t)}winstonLikeLoggerCall(e,t,n){n?this.#t[e](n,t):this.#t[e](t)}addMetadata(){let e={};return this.#e.forEach(t=>{let n=t();Object.assign(e,n)}),e}get level(){return this.#t.level}},h=e=>new m(e);exports.LogLevel=d,exports.LoggerInstanceManager=m,exports.default=h;
|
|
1
|
+
Object.defineProperty(exports,`__esModule`,{value:!0});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`pino`);c=s(c);const l={trace:`DEBUG`,debug:`DEBUG`,info:`INFO`,warn:`WARNING`,error:`ERROR`,fatal:`CRITICAL`};function u(e,t={}){let{serviceName:n,version:r}=t;return{base:n&&r?{serviceContext:{service:n,version:r}}:null,formatters:{level:e=>({severity:l[e]??l.info,...[`error`,`fatal`].includes(e)&&{"@type":`type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent`}}),log(e){let t=e,n=t.err?.stack??t.error?.stack??t.e?.stack;return{...e,...n&&{stack_trace:n}}}},messageKey:`message`,...e}}let d=function(e){return e.trace=`trace`,e.debug=`debug`,e.info=`info`,e.warn=`warn`,e.error=`error`,e.fatal=`fatal`,e}({});const f=e=>e||(process.env.LOG_LEVEL?process.env.LOG_LEVEL:process.env.NODE_ENV&&[`production`,`staging`,`test`].includes(process.env.NODE_ENV)?d.info:(process.env.NODE_ENV,d.debug)),p=(e,t,n)=>{let r;return r=process.env.NODE_ENV===`production`?(0,c.default)(u({level:process.env.PINO_LOG_LEVEL||`info`,...t},n)):(0,c.default)({level:process.env.PINO_LOG_LEVEL||`info`,transport:{target:`pino-pretty`,options:{colorize:!0}},...t}),r.level=e,r};var m=class{#e=[];#t;constructor(e,t){this.addContextMiddleware=e=>this.#e.push(e),this.trace=(e,t)=>this.winstonLikeLoggerCall(d.trace,e,t),this.traceLazy=e=>this.lazyLoggerCall(d.trace,e),this.debug=(e,t)=>this.winstonLikeLoggerCall(d.debug,e,t),this.debugLazy=e=>this.lazyLoggerCall(d.debug,e),this.info=(e,t)=>this.winstonLikeLoggerCall(d.info,e,t),this.infoLazy=e=>this.lazyLoggerCall(d.info,e),this.warn=(e,t)=>this.winstonLikeLoggerCall(d.warn,e,t),this.warnLazy=e=>this.lazyLoggerCall(d.warn,e),this.error=(e,t)=>this.winstonLikeLoggerCall(d.error,e,t),this.errorLazy=e=>this.lazyLoggerCall(d.error,e),this.fatal=(e,t)=>this.winstonLikeLoggerCall(d.fatal,e,t),this.fatalLazy=e=>this.lazyLoggerCall(d.fatal,e),this.child=e=>this.#t.child(e),this.#t=p(f(e),{mixin:()=>this.addMetadata(),serializers:{...c.stdSerializers,e:c.stdSerializers.errWithCause,err:c.stdSerializers.errWithCause,error:c.stdSerializers.errWithCause,errors(e){return Array.isArray(e)?e.map(c.stdSerializers.errWithCause):c.stdSerializers.errWithCause(e)}}},t)}winstonLikeLoggerCall(e,t,n){n?this.#t[e](n,t):this.#t[e](t)}lazyLoggerCall(e,t){if(!this.#t.isLevelEnabled(e))return;let[n,r]=t();this.winstonLikeLoggerCall(e,n,r)}addMetadata(){let e={};return this.#e.forEach(t=>{let n=t();Object.assign(e,n)}),e}get level(){return this.#t.level}},h=e=>new m(e);exports.LogLevel=d,exports.LoggerInstanceManager=m,exports.default=h;
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["levelToSeverity: Record<LevelOrString, string>","#contextMiddlewares","#logger","stdSerializers"],"sources":["../src/gcpLogOptions.ts","../src/index.ts"],"sourcesContent":["// Based on https://github.com/simenandre/pino-cloud-logging/blob/7816b2e4dec3dd18ef34c0f3f241d88a4cab0b90/src/main.ts\n// The repo is licensed under the Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0\n// Extracted since the repo no longer receives updates\n\nimport type { LevelOrString, LoggerOptions } from 'pino';\n\n// Map Pino levels to Google Cloud Logging severity levels\n// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity\nconst levelToSeverity: Record<LevelOrString, string> = {\n trace: 'DEBUG',\n debug: 'DEBUG',\n info: 'INFO',\n warn: 'WARNING',\n error: 'ERROR',\n fatal: 'CRITICAL',\n};\n\nexport interface ServiceContext {\n serviceName?: string;\n version?: string;\n}\n\nexport function gcpLogOptions(\n options?: LoggerOptions,\n context: ServiceContext = {},\n): LoggerOptions {\n const { serviceName, version } = context;\n\n const base = serviceName && version ? { serviceContext: { service: serviceName, version } } : null;\n\n return {\n // https://cloud.google.com/error-reporting/docs/formatting-error-messages#json_representation\n base,\n formatters: {\n level: (label: string) => ({\n severity: levelToSeverity[label] ?? levelToSeverity.info,\n // `@type` property tells Error Reporting to track even if there is no `stack_trace`\n ...(['error', 'fatal'].includes(label) && { '@type': 'type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent' }),\n }),\n\n log(object) {\n const logObject = object as { err?: Error; error?: Error; e?: Error; };\n const stackTrace = logObject.err?.stack ?? logObject.error?.stack ?? logObject.e?.stack;\n // eslint-disable-next-line camelcase\n return { ...object, ...(stackTrace && { stack_trace: stackTrace }) };\n },\n },\n messageKey: 'message',\n ...options,\n };\n}\n","import Pino, { type LoggerOptions, stdSerializers } from 'pino';\nimport { gcpLogOptions, type ServiceContext } from './gcpLogOptions';\n\nexport enum LogLevel {\n trace = 'trace',\n debug = 'debug',\n info = 'info',\n warn = 'warn',\n error = 'error',\n fatal = 'fatal',\n}\n\nconst getLevel = (logLevel?: LogLevel): LogLevel => {\n if (logLevel) return logLevel;\n if (process.env.LOG_LEVEL) return process.env.LOG_LEVEL as LogLevel;\n if (process.env.NODE_ENV && ['production', 'staging', 'test'].includes(process.env.NODE_ENV)) return LogLevel.info;\n if (process.env.NODE_ENV === 'development') return LogLevel.debug;\n return LogLevel.debug;\n};\n\nconst createLoggerInstance = (level: LogLevel, options: Omit<LoggerOptions, 'transport'>, context?: ServiceContext): Pino.Logger => {\n let loggerInstance;\n if (process.env.NODE_ENV === 'production') {\n loggerInstance = Pino(gcpLogOptions({\n level: process.env.PINO_LOG_LEVEL || 'info',\n ...options,\n } as object, context));\n } else {\n loggerInstance = Pino({\n level: process.env.PINO_LOG_LEVEL || 'info',\n transport: {\n target: 'pino-pretty',\n options: {\n colorize: true,\n },\n },\n ...options,\n });\n }\n loggerInstance.level = level;\n return loggerInstance;\n};\n\ntype MiddlewareFunction = () => Record<string, unknown>;\n\nexport class LoggerInstanceManager {\n #contextMiddlewares: MiddlewareFunction[] = [];\n\n #logger: Pino.Logger;\n\n constructor(logLevel?: LogLevel, context?: ServiceContext) {\n this.#logger = createLoggerInstance(getLevel(logLevel), {\n mixin: () => this.addMetadata(),\n serializers: {\n ...stdSerializers,\n e: stdSerializers.errWithCause,\n err: stdSerializers.errWithCause,\n error: stdSerializers.errWithCause,\n errors(val) {\n if (Array.isArray(val)) {\n return val.map(stdSerializers.errWithCause);\n }\n return stdSerializers.errWithCause(val);\n },\n },\n }, context);\n }\n\n // To support winston like logging in pino\n private winstonLikeLoggerCall(logLevel: LogLevel, message: string, meta: unknown): void {\n if (meta) {\n this.#logger[logLevel](meta, message);\n } else {\n this.#logger[logLevel](message);\n }\n }\n\n private addMetadata(): Record<string, unknown> {\n const newMetadata = {};\n this.#contextMiddlewares.forEach((middleware) => {\n const metadata = middleware();\n Object.assign(newMetadata, metadata);\n });\n return newMetadata;\n }\n\n addContextMiddleware = (middleware: MiddlewareFunction): number => this.#contextMiddlewares.push(middleware);\n\n trace = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.trace, message, meta);\n\n debug = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.debug, message, meta);\n\n info = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.info, message, meta);\n\n warn = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.warn, message, meta);\n\n error = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.error, message, meta);\n\n fatal = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.fatal, message, meta);\n\n child = (metadata: Record<string, unknown>): Pino.Logger => this.#logger.child(metadata);\n\n get level(): Pino.LevelWithSilentOrString {\n return this.#logger.level;\n }\n}\n\nexport default (loglevel?: `${LogLevel}`): LoggerInstanceManager => new LoggerInstanceManager(loglevel as LogLevel);\n"],"mappings":"kjBAQA,MAAMA,EAAiD,CACrD,MAAO,QACP,MAAO,QACP,KAAM,OACN,KAAM,UACN,MAAO,QACP,MAAO,WACR,CAOD,SAAgB,EACd,EACA,EAA0B,EAAE,CACb,CACf,GAAM,CAAE,cAAa,WAAY,EAIjC,MAAO,CAEL,KAJW,GAAe,EAAU,CAAE,eAAgB,CAAE,QAAS,EAAa,UAAS,CAAE,CAAG,KAK5F,WAAY,CACV,MAAQ,IAAmB,CACzB,SAAU,EAAgB,IAAU,EAAgB,KAEpD,GAAI,CAAC,QAAS,QAAQ,CAAC,SAAS,EAAM,EAAI,CAAE,QAAS,qFAAsF,CAC5I,EAED,IAAI,EAAQ,CACV,IAAM,EAAY,EACZ,EAAa,EAAU,KAAK,OAAS,EAAU,OAAO,OAAS,EAAU,GAAG,MAElF,MAAO,CAAE,GAAG,EAAQ,GAAI,GAAc,CAAE,YAAa,EAAY,CAAG,EAEvE,CACD,WAAY,UACZ,GAAG,EACJ,CC9CH,IAAY,EAAA,SAAA,EAAL,OACL,GAAA,MAAA,QACA,EAAA,MAAA,QACA,EAAA,KAAA,OACA,EAAA,KAAA,OACA,EAAA,MAAA,QACA,EAAA,MAAA,
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["levelToSeverity: Record<LevelOrString, string>","#contextMiddlewares","#logger","stdSerializers"],"sources":["../src/gcpLogOptions.ts","../src/index.ts"],"sourcesContent":["// Based on https://github.com/simenandre/pino-cloud-logging/blob/7816b2e4dec3dd18ef34c0f3f241d88a4cab0b90/src/main.ts\n// The repo is licensed under the Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0\n// Extracted since the repo no longer receives updates\n\nimport type { LevelOrString, LoggerOptions } from 'pino';\n\n// Map Pino levels to Google Cloud Logging severity levels\n// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity\nconst levelToSeverity: Record<LevelOrString, string> = {\n trace: 'DEBUG',\n debug: 'DEBUG',\n info: 'INFO',\n warn: 'WARNING',\n error: 'ERROR',\n fatal: 'CRITICAL',\n};\n\nexport interface ServiceContext {\n serviceName?: string;\n version?: string;\n}\n\nexport function gcpLogOptions(\n options?: LoggerOptions,\n context: ServiceContext = {},\n): LoggerOptions {\n const { serviceName, version } = context;\n\n const base = serviceName && version ? { serviceContext: { service: serviceName, version } } : null;\n\n return {\n // https://cloud.google.com/error-reporting/docs/formatting-error-messages#json_representation\n base,\n formatters: {\n level: (label: string) => ({\n severity: levelToSeverity[label] ?? levelToSeverity.info,\n // `@type` property tells Error Reporting to track even if there is no `stack_trace`\n ...(['error', 'fatal'].includes(label) && { '@type': 'type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent' }),\n }),\n\n log(object) {\n const logObject = object as { err?: Error; error?: Error; e?: Error; };\n const stackTrace = logObject.err?.stack ?? logObject.error?.stack ?? logObject.e?.stack;\n // eslint-disable-next-line camelcase\n return { ...object, ...(stackTrace && { stack_trace: stackTrace }) };\n },\n },\n messageKey: 'message',\n ...options,\n };\n}\n","import Pino, { type LoggerOptions, stdSerializers } from 'pino';\nimport { gcpLogOptions, type ServiceContext } from './gcpLogOptions';\n\nexport enum LogLevel {\n trace = 'trace',\n debug = 'debug',\n info = 'info',\n warn = 'warn',\n error = 'error',\n fatal = 'fatal',\n}\ntype LazyLogFn = () => [string] | [string, unknown];\n\nconst getLevel = (logLevel?: LogLevel): LogLevel => {\n if (logLevel) return logLevel;\n if (process.env.LOG_LEVEL) return process.env.LOG_LEVEL as LogLevel;\n if (process.env.NODE_ENV && ['production', 'staging', 'test'].includes(process.env.NODE_ENV)) return LogLevel.info;\n if (process.env.NODE_ENV === 'development') return LogLevel.debug;\n return LogLevel.debug;\n};\n\nconst createLoggerInstance = (level: LogLevel, options: Omit<LoggerOptions, 'transport'>, context?: ServiceContext): Pino.Logger => {\n let loggerInstance;\n if (process.env.NODE_ENV === 'production') {\n loggerInstance = Pino(gcpLogOptions({\n level: process.env.PINO_LOG_LEVEL || 'info',\n ...options,\n } as object, context));\n } else {\n loggerInstance = Pino({\n level: process.env.PINO_LOG_LEVEL || 'info',\n transport: {\n target: 'pino-pretty',\n options: {\n colorize: true,\n },\n },\n ...options,\n });\n }\n loggerInstance.level = level;\n return loggerInstance;\n};\n\ntype MiddlewareFunction = () => Record<string, unknown>;\n\nexport class LoggerInstanceManager {\n #contextMiddlewares: MiddlewareFunction[] = [];\n\n #logger: Pino.Logger;\n\n constructor(logLevel?: LogLevel, context?: ServiceContext) {\n this.#logger = createLoggerInstance(getLevel(logLevel), {\n mixin: () => this.addMetadata(),\n serializers: {\n ...stdSerializers,\n e: stdSerializers.errWithCause,\n err: stdSerializers.errWithCause,\n error: stdSerializers.errWithCause,\n errors(val) {\n if (Array.isArray(val)) {\n return val.map(stdSerializers.errWithCause);\n }\n return stdSerializers.errWithCause(val);\n },\n },\n }, context);\n }\n\n // To support winston like logging in pino\n private winstonLikeLoggerCall(logLevel: LogLevel, message: string, meta: unknown): void {\n if (meta) {\n this.#logger[logLevel](meta, message);\n } else {\n this.#logger[logLevel](message);\n }\n }\n\n private lazyLoggerCall(logLevel: LogLevel, fn: LazyLogFn): void {\n if (!this.#logger.isLevelEnabled(logLevel)) {\n return;\n }\n const [message, meta] = fn();\n this.winstonLikeLoggerCall(logLevel, message, meta);\n }\n\n private addMetadata(): Record<string, unknown> {\n const newMetadata = {};\n this.#contextMiddlewares.forEach((middleware) => {\n const metadata = middleware();\n Object.assign(newMetadata, metadata);\n });\n return newMetadata;\n }\n\n addContextMiddleware = (middleware: MiddlewareFunction): number => this.#contextMiddlewares.push(middleware);\n\n trace = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.trace, message, meta);\n /** A lazy version of trace that only evaluates the message and metadata only if the trace level is enabled */\n traceLazy = (fn: LazyLogFn): void => this.lazyLoggerCall(LogLevel.trace, fn);\n\n debug = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.debug, message, meta);\n /** A lazy version of trace that only evaluates the message and metadata only if the debug level is enabled */\n debugLazy = (fn: LazyLogFn): void => this.lazyLoggerCall(LogLevel.debug, fn);\n\n info = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.info, message, meta);\n /** A lazy version of trace that only evaluates the message and metadata only if the info level is enabled */\n infoLazy = (fn: LazyLogFn): void => this.lazyLoggerCall(LogLevel.info, fn);\n\n warn = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.warn, message, meta);\n /** A lazy version of trace that only evaluates the message and metadata only if the warn level is enabled */\n warnLazy = (fn: LazyLogFn): void => this.lazyLoggerCall(LogLevel.warn, fn);\n\n error = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.error, message, meta);\n /** A lazy version of trace that only evaluates the message and metadata only if the error level is enabled */\n errorLazy = (fn: LazyLogFn): void => this.lazyLoggerCall(LogLevel.error, fn);\n\n fatal = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.fatal, message, meta);\n /** A lazy version of trace that only evaluates the message and metadata only if the fatal level is enabled */\n fatalLazy = (fn: LazyLogFn): void => this.lazyLoggerCall(LogLevel.fatal, fn);\n\n child = (metadata: Record<string, unknown>): Pino.Logger => this.#logger.child(metadata);\n\n get level(): Pino.LevelWithSilentOrString {\n return this.#logger.level;\n }\n}\n\nexport default (loglevel?: `${LogLevel}`): LoggerInstanceManager => new LoggerInstanceManager(loglevel as LogLevel);\n"],"mappings":"kjBAQA,MAAMA,EAAiD,CACrD,MAAO,QACP,MAAO,QACP,KAAM,OACN,KAAM,UACN,MAAO,QACP,MAAO,WACR,CAOD,SAAgB,EACd,EACA,EAA0B,EAAE,CACb,CACf,GAAM,CAAE,cAAa,WAAY,EAIjC,MAAO,CAEL,KAJW,GAAe,EAAU,CAAE,eAAgB,CAAE,QAAS,EAAa,UAAS,CAAE,CAAG,KAK5F,WAAY,CACV,MAAQ,IAAmB,CACzB,SAAU,EAAgB,IAAU,EAAgB,KAEpD,GAAI,CAAC,QAAS,QAAQ,CAAC,SAAS,EAAM,EAAI,CAAE,QAA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AAa,QAIxB,EAAgB,GAAoD,IAAI,EAAsB,EAAqB"}
|
package/dist/index.d.cts
CHANGED
|
@@ -15,19 +15,33 @@ declare enum LogLevel {
|
|
|
15
15
|
error = "error",
|
|
16
16
|
fatal = "fatal",
|
|
17
17
|
}
|
|
18
|
+
type LazyLogFn = () => [string] | [string, unknown];
|
|
18
19
|
type MiddlewareFunction = () => Record<string, unknown>;
|
|
19
20
|
declare class LoggerInstanceManager {
|
|
20
21
|
#private;
|
|
21
22
|
constructor(logLevel?: LogLevel, context?: ServiceContext);
|
|
22
23
|
private winstonLikeLoggerCall;
|
|
24
|
+
private lazyLoggerCall;
|
|
23
25
|
private addMetadata;
|
|
24
26
|
addContextMiddleware: (middleware: MiddlewareFunction) => number;
|
|
25
27
|
trace: (message: string, meta?: unknown) => void;
|
|
28
|
+
/** A lazy version of trace that only evaluates the message and metadata only if the trace level is enabled */
|
|
29
|
+
traceLazy: (fn: LazyLogFn) => void;
|
|
26
30
|
debug: (message: string, meta?: unknown) => void;
|
|
31
|
+
/** A lazy version of trace that only evaluates the message and metadata only if the debug level is enabled */
|
|
32
|
+
debugLazy: (fn: LazyLogFn) => void;
|
|
27
33
|
info: (message: string, meta?: unknown) => void;
|
|
34
|
+
/** A lazy version of trace that only evaluates the message and metadata only if the info level is enabled */
|
|
35
|
+
infoLazy: (fn: LazyLogFn) => void;
|
|
28
36
|
warn: (message: string, meta?: unknown) => void;
|
|
37
|
+
/** A lazy version of trace that only evaluates the message and metadata only if the warn level is enabled */
|
|
38
|
+
warnLazy: (fn: LazyLogFn) => void;
|
|
29
39
|
error: (message: string, meta?: unknown) => void;
|
|
40
|
+
/** A lazy version of trace that only evaluates the message and metadata only if the error level is enabled */
|
|
41
|
+
errorLazy: (fn: LazyLogFn) => void;
|
|
30
42
|
fatal: (message: string, meta?: unknown) => void;
|
|
43
|
+
/** A lazy version of trace that only evaluates the message and metadata only if the fatal level is enabled */
|
|
44
|
+
fatalLazy: (fn: LazyLogFn) => void;
|
|
31
45
|
child: (metadata: Record<string, unknown>) => Pino.Logger;
|
|
32
46
|
get level(): Pino.LevelWithSilentOrString;
|
|
33
47
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -15,19 +15,33 @@ declare enum LogLevel {
|
|
|
15
15
|
error = "error",
|
|
16
16
|
fatal = "fatal",
|
|
17
17
|
}
|
|
18
|
+
type LazyLogFn = () => [string] | [string, unknown];
|
|
18
19
|
type MiddlewareFunction = () => Record<string, unknown>;
|
|
19
20
|
declare class LoggerInstanceManager {
|
|
20
21
|
#private;
|
|
21
22
|
constructor(logLevel?: LogLevel, context?: ServiceContext);
|
|
22
23
|
private winstonLikeLoggerCall;
|
|
24
|
+
private lazyLoggerCall;
|
|
23
25
|
private addMetadata;
|
|
24
26
|
addContextMiddleware: (middleware: MiddlewareFunction) => number;
|
|
25
27
|
trace: (message: string, meta?: unknown) => void;
|
|
28
|
+
/** A lazy version of trace that only evaluates the message and metadata only if the trace level is enabled */
|
|
29
|
+
traceLazy: (fn: LazyLogFn) => void;
|
|
26
30
|
debug: (message: string, meta?: unknown) => void;
|
|
31
|
+
/** A lazy version of trace that only evaluates the message and metadata only if the debug level is enabled */
|
|
32
|
+
debugLazy: (fn: LazyLogFn) => void;
|
|
27
33
|
info: (message: string, meta?: unknown) => void;
|
|
34
|
+
/** A lazy version of trace that only evaluates the message and metadata only if the info level is enabled */
|
|
35
|
+
infoLazy: (fn: LazyLogFn) => void;
|
|
28
36
|
warn: (message: string, meta?: unknown) => void;
|
|
37
|
+
/** A lazy version of trace that only evaluates the message and metadata only if the warn level is enabled */
|
|
38
|
+
warnLazy: (fn: LazyLogFn) => void;
|
|
29
39
|
error: (message: string, meta?: unknown) => void;
|
|
40
|
+
/** A lazy version of trace that only evaluates the message and metadata only if the error level is enabled */
|
|
41
|
+
errorLazy: (fn: LazyLogFn) => void;
|
|
30
42
|
fatal: (message: string, meta?: unknown) => void;
|
|
43
|
+
/** A lazy version of trace that only evaluates the message and metadata only if the fatal level is enabled */
|
|
44
|
+
fatalLazy: (fn: LazyLogFn) => void;
|
|
31
45
|
child: (metadata: Record<string, unknown>) => Pino.Logger;
|
|
32
46
|
get level(): Pino.LevelWithSilentOrString;
|
|
33
47
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import e,{stdSerializers as t}from"pino";const n={trace:`DEBUG`,debug:`DEBUG`,info:`INFO`,warn:`WARNING`,error:`ERROR`,fatal:`CRITICAL`};function r(e,t={}){let{serviceName:r,version:i}=t;return{base:r&&i?{serviceContext:{service:r,version:i}}:null,formatters:{level:e=>({severity:n[e]??n.info,...[`error`,`fatal`].includes(e)&&{"@type":`type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent`}}),log(e){let t=e,n=t.err?.stack??t.error?.stack??t.e?.stack;return{...e,...n&&{stack_trace:n}}}},messageKey:`message`,...e}}let i=function(e){return e.trace=`trace`,e.debug=`debug`,e.info=`info`,e.warn=`warn`,e.error=`error`,e.fatal=`fatal`,e}({});const a=e=>e||(process.env.LOG_LEVEL?process.env.LOG_LEVEL:process.env.NODE_ENV&&[`production`,`staging`,`test`].includes(process.env.NODE_ENV)?i.info:(process.env.NODE_ENV,i.debug)),o=(t,n,i)=>{let a;return a=process.env.NODE_ENV===`production`?e(r({level:process.env.PINO_LOG_LEVEL||`info`,...n},i)):e({level:process.env.PINO_LOG_LEVEL||`info`,transport:{target:`pino-pretty`,options:{colorize:!0}},...n}),a.level=t,a};var s=class{#e=[];#t;constructor(e,n){this.addContextMiddleware=e=>this.#e.push(e),this.trace=(e,t)=>this.winstonLikeLoggerCall(i.trace,e,t),this.debug=(e,t)=>this.winstonLikeLoggerCall(i.debug,e,t),this.info=(e,t)=>this.winstonLikeLoggerCall(i.info,e,t),this.warn=(e,t)=>this.winstonLikeLoggerCall(i.warn,e,t),this.error=(e,t)=>this.winstonLikeLoggerCall(i.error,e,t),this.fatal=(e,t)=>this.winstonLikeLoggerCall(i.fatal,e,t),this.child=e=>this.#t.child(e),this.#t=o(a(e),{mixin:()=>this.addMetadata(),serializers:{...t,e:t.errWithCause,err:t.errWithCause,error:t.errWithCause,errors(e){return Array.isArray(e)?e.map(t.errWithCause):t.errWithCause(e)}}},n)}winstonLikeLoggerCall(e,t,n){n?this.#t[e](n,t):this.#t[e](t)}addMetadata(){let e={};return this.#e.forEach(t=>{let n=t();Object.assign(e,n)}),e}get level(){return this.#t.level}},c=e=>new s(e);export{i as LogLevel,s as LoggerInstanceManager,c as default};
|
|
1
|
+
import e,{stdSerializers as t}from"pino";const n={trace:`DEBUG`,debug:`DEBUG`,info:`INFO`,warn:`WARNING`,error:`ERROR`,fatal:`CRITICAL`};function r(e,t={}){let{serviceName:r,version:i}=t;return{base:r&&i?{serviceContext:{service:r,version:i}}:null,formatters:{level:e=>({severity:n[e]??n.info,...[`error`,`fatal`].includes(e)&&{"@type":`type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent`}}),log(e){let t=e,n=t.err?.stack??t.error?.stack??t.e?.stack;return{...e,...n&&{stack_trace:n}}}},messageKey:`message`,...e}}let i=function(e){return e.trace=`trace`,e.debug=`debug`,e.info=`info`,e.warn=`warn`,e.error=`error`,e.fatal=`fatal`,e}({});const a=e=>e||(process.env.LOG_LEVEL?process.env.LOG_LEVEL:process.env.NODE_ENV&&[`production`,`staging`,`test`].includes(process.env.NODE_ENV)?i.info:(process.env.NODE_ENV,i.debug)),o=(t,n,i)=>{let a;return a=process.env.NODE_ENV===`production`?e(r({level:process.env.PINO_LOG_LEVEL||`info`,...n},i)):e({level:process.env.PINO_LOG_LEVEL||`info`,transport:{target:`pino-pretty`,options:{colorize:!0}},...n}),a.level=t,a};var s=class{#e=[];#t;constructor(e,n){this.addContextMiddleware=e=>this.#e.push(e),this.trace=(e,t)=>this.winstonLikeLoggerCall(i.trace,e,t),this.traceLazy=e=>this.lazyLoggerCall(i.trace,e),this.debug=(e,t)=>this.winstonLikeLoggerCall(i.debug,e,t),this.debugLazy=e=>this.lazyLoggerCall(i.debug,e),this.info=(e,t)=>this.winstonLikeLoggerCall(i.info,e,t),this.infoLazy=e=>this.lazyLoggerCall(i.info,e),this.warn=(e,t)=>this.winstonLikeLoggerCall(i.warn,e,t),this.warnLazy=e=>this.lazyLoggerCall(i.warn,e),this.error=(e,t)=>this.winstonLikeLoggerCall(i.error,e,t),this.errorLazy=e=>this.lazyLoggerCall(i.error,e),this.fatal=(e,t)=>this.winstonLikeLoggerCall(i.fatal,e,t),this.fatalLazy=e=>this.lazyLoggerCall(i.fatal,e),this.child=e=>this.#t.child(e),this.#t=o(a(e),{mixin:()=>this.addMetadata(),serializers:{...t,e:t.errWithCause,err:t.errWithCause,error:t.errWithCause,errors(e){return Array.isArray(e)?e.map(t.errWithCause):t.errWithCause(e)}}},n)}winstonLikeLoggerCall(e,t,n){n?this.#t[e](n,t):this.#t[e](t)}lazyLoggerCall(e,t){if(!this.#t.isLevelEnabled(e))return;let[n,r]=t();this.winstonLikeLoggerCall(e,n,r)}addMetadata(){let e={};return this.#e.forEach(t=>{let n=t();Object.assign(e,n)}),e}get level(){return this.#t.level}},c=e=>new s(e);export{i as LogLevel,s as LoggerInstanceManager,c as default};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["levelToSeverity: Record<LevelOrString, string>","#contextMiddlewares","#logger"],"sources":["../src/gcpLogOptions.ts","../src/index.ts"],"sourcesContent":["// Based on https://github.com/simenandre/pino-cloud-logging/blob/7816b2e4dec3dd18ef34c0f3f241d88a4cab0b90/src/main.ts\n// The repo is licensed under the Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0\n// Extracted since the repo no longer receives updates\n\nimport type { LevelOrString, LoggerOptions } from 'pino';\n\n// Map Pino levels to Google Cloud Logging severity levels\n// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity\nconst levelToSeverity: Record<LevelOrString, string> = {\n trace: 'DEBUG',\n debug: 'DEBUG',\n info: 'INFO',\n warn: 'WARNING',\n error: 'ERROR',\n fatal: 'CRITICAL',\n};\n\nexport interface ServiceContext {\n serviceName?: string;\n version?: string;\n}\n\nexport function gcpLogOptions(\n options?: LoggerOptions,\n context: ServiceContext = {},\n): LoggerOptions {\n const { serviceName, version } = context;\n\n const base = serviceName && version ? { serviceContext: { service: serviceName, version } } : null;\n\n return {\n // https://cloud.google.com/error-reporting/docs/formatting-error-messages#json_representation\n base,\n formatters: {\n level: (label: string) => ({\n severity: levelToSeverity[label] ?? levelToSeverity.info,\n // `@type` property tells Error Reporting to track even if there is no `stack_trace`\n ...(['error', 'fatal'].includes(label) && { '@type': 'type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent' }),\n }),\n\n log(object) {\n const logObject = object as { err?: Error; error?: Error; e?: Error; };\n const stackTrace = logObject.err?.stack ?? logObject.error?.stack ?? logObject.e?.stack;\n // eslint-disable-next-line camelcase\n return { ...object, ...(stackTrace && { stack_trace: stackTrace }) };\n },\n },\n messageKey: 'message',\n ...options,\n };\n}\n","import Pino, { type LoggerOptions, stdSerializers } from 'pino';\nimport { gcpLogOptions, type ServiceContext } from './gcpLogOptions';\n\nexport enum LogLevel {\n trace = 'trace',\n debug = 'debug',\n info = 'info',\n warn = 'warn',\n error = 'error',\n fatal = 'fatal',\n}\n\nconst getLevel = (logLevel?: LogLevel): LogLevel => {\n if (logLevel) return logLevel;\n if (process.env.LOG_LEVEL) return process.env.LOG_LEVEL as LogLevel;\n if (process.env.NODE_ENV && ['production', 'staging', 'test'].includes(process.env.NODE_ENV)) return LogLevel.info;\n if (process.env.NODE_ENV === 'development') return LogLevel.debug;\n return LogLevel.debug;\n};\n\nconst createLoggerInstance = (level: LogLevel, options: Omit<LoggerOptions, 'transport'>, context?: ServiceContext): Pino.Logger => {\n let loggerInstance;\n if (process.env.NODE_ENV === 'production') {\n loggerInstance = Pino(gcpLogOptions({\n level: process.env.PINO_LOG_LEVEL || 'info',\n ...options,\n } as object, context));\n } else {\n loggerInstance = Pino({\n level: process.env.PINO_LOG_LEVEL || 'info',\n transport: {\n target: 'pino-pretty',\n options: {\n colorize: true,\n },\n },\n ...options,\n });\n }\n loggerInstance.level = level;\n return loggerInstance;\n};\n\ntype MiddlewareFunction = () => Record<string, unknown>;\n\nexport class LoggerInstanceManager {\n #contextMiddlewares: MiddlewareFunction[] = [];\n\n #logger: Pino.Logger;\n\n constructor(logLevel?: LogLevel, context?: ServiceContext) {\n this.#logger = createLoggerInstance(getLevel(logLevel), {\n mixin: () => this.addMetadata(),\n serializers: {\n ...stdSerializers,\n e: stdSerializers.errWithCause,\n err: stdSerializers.errWithCause,\n error: stdSerializers.errWithCause,\n errors(val) {\n if (Array.isArray(val)) {\n return val.map(stdSerializers.errWithCause);\n }\n return stdSerializers.errWithCause(val);\n },\n },\n }, context);\n }\n\n // To support winston like logging in pino\n private winstonLikeLoggerCall(logLevel: LogLevel, message: string, meta: unknown): void {\n if (meta) {\n this.#logger[logLevel](meta, message);\n } else {\n this.#logger[logLevel](message);\n }\n }\n\n private addMetadata(): Record<string, unknown> {\n const newMetadata = {};\n this.#contextMiddlewares.forEach((middleware) => {\n const metadata = middleware();\n Object.assign(newMetadata, metadata);\n });\n return newMetadata;\n }\n\n addContextMiddleware = (middleware: MiddlewareFunction): number => this.#contextMiddlewares.push(middleware);\n\n trace = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.trace, message, meta);\n\n debug = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.debug, message, meta);\n\n info = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.info, message, meta);\n\n warn = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.warn, message, meta);\n\n error = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.error, message, meta);\n\n fatal = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.fatal, message, meta);\n\n child = (metadata: Record<string, unknown>): Pino.Logger => this.#logger.child(metadata);\n\n get level(): Pino.LevelWithSilentOrString {\n return this.#logger.level;\n }\n}\n\nexport default (loglevel?: `${LogLevel}`): LoggerInstanceManager => new LoggerInstanceManager(loglevel as LogLevel);\n"],"mappings":"yCAQA,MAAMA,EAAiD,CACrD,MAAO,QACP,MAAO,QACP,KAAM,OACN,KAAM,UACN,MAAO,QACP,MAAO,WACR,CAOD,SAAgB,EACd,EACA,EAA0B,EAAE,CACb,CACf,GAAM,CAAE,cAAa,WAAY,EAIjC,MAAO,CAEL,KAJW,GAAe,EAAU,CAAE,eAAgB,CAAE,QAAS,EAAa,UAAS,CAAE,CAAG,KAK5F,WAAY,CACV,MAAQ,IAAmB,CACzB,SAAU,EAAgB,IAAU,EAAgB,KAEpD,GAAI,CAAC,QAAS,QAAQ,CAAC,SAAS,EAAM,EAAI,CAAE,QAAS,qFAAsF,CAC5I,EAED,IAAI,EAAQ,CACV,IAAM,EAAY,EACZ,EAAa,EAAU,KAAK,OAAS,EAAU,OAAO,OAAS,EAAU,GAAG,MAElF,MAAO,CAAE,GAAG,EAAQ,GAAI,GAAc,CAAE,YAAa,EAAY,CAAG,EAEvE,CACD,WAAY,UACZ,GAAG,EACJ,CC9CH,IAAY,EAAA,SAAA,EAAL,OACL,GAAA,MAAA,QACA,EAAA,MAAA,QACA,EAAA,KAAA,OACA,EAAA,KAAA,OACA,EAAA,MAAA,QACA,EAAA,MAAA,
|
|
1
|
+
{"version":3,"file":"index.js","names":["levelToSeverity: Record<LevelOrString, string>","#contextMiddlewares","#logger"],"sources":["../src/gcpLogOptions.ts","../src/index.ts"],"sourcesContent":["// Based on https://github.com/simenandre/pino-cloud-logging/blob/7816b2e4dec3dd18ef34c0f3f241d88a4cab0b90/src/main.ts\n// The repo is licensed under the Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0\n// Extracted since the repo no longer receives updates\n\nimport type { LevelOrString, LoggerOptions } from 'pino';\n\n// Map Pino levels to Google Cloud Logging severity levels\n// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity\nconst levelToSeverity: Record<LevelOrString, string> = {\n trace: 'DEBUG',\n debug: 'DEBUG',\n info: 'INFO',\n warn: 'WARNING',\n error: 'ERROR',\n fatal: 'CRITICAL',\n};\n\nexport interface ServiceContext {\n serviceName?: string;\n version?: string;\n}\n\nexport function gcpLogOptions(\n options?: LoggerOptions,\n context: ServiceContext = {},\n): LoggerOptions {\n const { serviceName, version } = context;\n\n const base = serviceName && version ? { serviceContext: { service: serviceName, version } } : null;\n\n return {\n // https://cloud.google.com/error-reporting/docs/formatting-error-messages#json_representation\n base,\n formatters: {\n level: (label: string) => ({\n severity: levelToSeverity[label] ?? levelToSeverity.info,\n // `@type` property tells Error Reporting to track even if there is no `stack_trace`\n ...(['error', 'fatal'].includes(label) && { '@type': 'type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent' }),\n }),\n\n log(object) {\n const logObject = object as { err?: Error; error?: Error; e?: Error; };\n const stackTrace = logObject.err?.stack ?? logObject.error?.stack ?? logObject.e?.stack;\n // eslint-disable-next-line camelcase\n return { ...object, ...(stackTrace && { stack_trace: stackTrace }) };\n },\n },\n messageKey: 'message',\n ...options,\n };\n}\n","import Pino, { type LoggerOptions, stdSerializers } from 'pino';\nimport { gcpLogOptions, type ServiceContext } from './gcpLogOptions';\n\nexport enum LogLevel {\n trace = 'trace',\n debug = 'debug',\n info = 'info',\n warn = 'warn',\n error = 'error',\n fatal = 'fatal',\n}\ntype LazyLogFn = () => [string] | [string, unknown];\n\nconst getLevel = (logLevel?: LogLevel): LogLevel => {\n if (logLevel) return logLevel;\n if (process.env.LOG_LEVEL) return process.env.LOG_LEVEL as LogLevel;\n if (process.env.NODE_ENV && ['production', 'staging', 'test'].includes(process.env.NODE_ENV)) return LogLevel.info;\n if (process.env.NODE_ENV === 'development') return LogLevel.debug;\n return LogLevel.debug;\n};\n\nconst createLoggerInstance = (level: LogLevel, options: Omit<LoggerOptions, 'transport'>, context?: ServiceContext): Pino.Logger => {\n let loggerInstance;\n if (process.env.NODE_ENV === 'production') {\n loggerInstance = Pino(gcpLogOptions({\n level: process.env.PINO_LOG_LEVEL || 'info',\n ...options,\n } as object, context));\n } else {\n loggerInstance = Pino({\n level: process.env.PINO_LOG_LEVEL || 'info',\n transport: {\n target: 'pino-pretty',\n options: {\n colorize: true,\n },\n },\n ...options,\n });\n }\n loggerInstance.level = level;\n return loggerInstance;\n};\n\ntype MiddlewareFunction = () => Record<string, unknown>;\n\nexport class LoggerInstanceManager {\n #contextMiddlewares: MiddlewareFunction[] = [];\n\n #logger: Pino.Logger;\n\n constructor(logLevel?: LogLevel, context?: ServiceContext) {\n this.#logger = createLoggerInstance(getLevel(logLevel), {\n mixin: () => this.addMetadata(),\n serializers: {\n ...stdSerializers,\n e: stdSerializers.errWithCause,\n err: stdSerializers.errWithCause,\n error: stdSerializers.errWithCause,\n errors(val) {\n if (Array.isArray(val)) {\n return val.map(stdSerializers.errWithCause);\n }\n return stdSerializers.errWithCause(val);\n },\n },\n }, context);\n }\n\n // To support winston like logging in pino\n private winstonLikeLoggerCall(logLevel: LogLevel, message: string, meta: unknown): void {\n if (meta) {\n this.#logger[logLevel](meta, message);\n } else {\n this.#logger[logLevel](message);\n }\n }\n\n private lazyLoggerCall(logLevel: LogLevel, fn: LazyLogFn): void {\n if (!this.#logger.isLevelEnabled(logLevel)) {\n return;\n }\n const [message, meta] = fn();\n this.winstonLikeLoggerCall(logLevel, message, meta);\n }\n\n private addMetadata(): Record<string, unknown> {\n const newMetadata = {};\n this.#contextMiddlewares.forEach((middleware) => {\n const metadata = middleware();\n Object.assign(newMetadata, metadata);\n });\n return newMetadata;\n }\n\n addContextMiddleware = (middleware: MiddlewareFunction): number => this.#contextMiddlewares.push(middleware);\n\n trace = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.trace, message, meta);\n /** A lazy version of trace that only evaluates the message and metadata only if the trace level is enabled */\n traceLazy = (fn: LazyLogFn): void => this.lazyLoggerCall(LogLevel.trace, fn);\n\n debug = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.debug, message, meta);\n /** A lazy version of trace that only evaluates the message and metadata only if the debug level is enabled */\n debugLazy = (fn: LazyLogFn): void => this.lazyLoggerCall(LogLevel.debug, fn);\n\n info = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.info, message, meta);\n /** A lazy version of trace that only evaluates the message and metadata only if the info level is enabled */\n infoLazy = (fn: LazyLogFn): void => this.lazyLoggerCall(LogLevel.info, fn);\n\n warn = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.warn, message, meta);\n /** A lazy version of trace that only evaluates the message and metadata only if the warn level is enabled */\n warnLazy = (fn: LazyLogFn): void => this.lazyLoggerCall(LogLevel.warn, fn);\n\n error = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.error, message, meta);\n /** A lazy version of trace that only evaluates the message and metadata only if the error level is enabled */\n errorLazy = (fn: LazyLogFn): void => this.lazyLoggerCall(LogLevel.error, fn);\n\n fatal = (message: string, meta?: unknown): void => this.winstonLikeLoggerCall(LogLevel.fatal, message, meta);\n /** A lazy version of trace that only evaluates the message and metadata only if the fatal level is enabled */\n fatalLazy = (fn: LazyLogFn): void => this.lazyLoggerCall(LogLevel.fatal, fn);\n\n child = (metadata: Record<string, unknown>): Pino.Logger => this.#logger.child(metadata);\n\n get level(): Pino.LevelWithSilentOrString {\n return this.#logger.level;\n }\n}\n\nexport default (loglevel?: `${LogLevel}`): LoggerInstanceManager => new LoggerInstanceManager(loglevel as LogLevel);\n"],"mappings":"yCAQA,MAAMA,EAAiD,CACrD,MAAO,QACP,MAAO,QACP,KAAM,OACN,KAAM,UACN,MAAO,QACP,MAAO,WACR,CAOD,SAAgB,EACd,EACA,EAA0B,EAAE,CACb,CACf,GAAM,CAAE,cAAa,WAAY,EAIjC,MAAO,CAEL,KAJW,GAAe,EAAU,C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