-
Notifications
You must be signed in to change notification settings - Fork 5
feature: logger hook functions #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,9 @@ | ||
import { Logger as NestLogger } from '@nestjs/common'; | ||
import { LogLevel, Logger as NestLogger } from '@nestjs/common'; | ||
import { Logger as NestJSPinoLogger } from 'nestjs-pino'; | ||
import { ContextStore } from './store/context-store'; | ||
import { omitBy, isNil, isEmpty } from 'lodash'; | ||
import { ContextLoggerFactoryOptions } from './interfaces/context-logger.interface'; | ||
|
||
type Bindings = Record<string, any>; | ||
|
||
export interface LogEntry { | ||
[key: string]: any; | ||
err?: Error | string; | ||
} | ||
import { Bindings, LogEntry } from './types'; | ||
|
||
export class ContextLogger { | ||
private static internalLogger: NestJSPinoLogger; | ||
|
@@ -85,7 +79,7 @@ export class ContextLogger { | |
this.callInternalLogger('error', message, adaptedBindings, error); | ||
} | ||
|
||
private callInternalLogger(level: string, message: string, bindings: Bindings, error?: Error | string) { | ||
private callInternalLogger(level: LogLevel, message: string, bindings: Bindings, error?: Error | string) { | ||
// If it's a bootstrap log and ignoreBootstrapLogs is true, do nothing | ||
if (typeof bindings === 'string' && ContextLogger.options?.ignoreBootstrapLogs) { | ||
return; | ||
|
@@ -99,6 +93,7 @@ export class ContextLogger { | |
logObject = this.createLogEntry(bindings, error); | ||
} | ||
const logger = ContextLogger.internalLogger ?? this.fallbackLogger; | ||
this.callHooks(level, message, logObject); | ||
return logger[level](logObject, ...[message, this.moduleName]); | ||
} | ||
|
||
|
@@ -133,4 +128,18 @@ export class ContextLogger { | |
|
||
return { [key]: obj }; | ||
} | ||
|
||
private callHooks(level: LogLevel, message: string, bindings: Bindings): void { | ||
const hooks = ContextLogger.options?.hooks; | ||
|
||
if (!hooks) { | ||
return; | ||
} | ||
|
||
[...(hooks[level] || []), ...(hooks['all'] || [])].forEach((hook) => { | ||
hook(message, bindings); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure I understand the purpose behind this, you want to be able to shove side effects right before the log is sent? But the hooks are set one time during app bootstrap, why not use the context interceptor to enrich the context and such new fields enrichment will be done automatically? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn’t meant for enriching the logs themselves, but rather for triggering small side effects right before or after a log is sent (could be changed) - like tracking metrics or triggering alerts. It’s meant as a lightweight runtime hook without needing to run the same block of code before/after each log, wrap logger functions or modify individual log calls. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as in "also send a sentry when an error is logged?" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, for example.. |
||
}); | ||
|
||
return; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { LogLevel } from '@nestjs/common'; | ||
|
||
export type Bindings = Record<string, any>; | ||
|
||
export interface LogEntry { | ||
[key: string]: any; | ||
err?: Error | string; | ||
} | ||
|
||
export type LoggerHookKeys = LogLevel | 'all'; |
Uh oh!
There was an error while loading. Please reload this page.