Skip to content

Commit

Permalink
dynamic log config support
Browse files Browse the repository at this point in the history
  • Loading branch information
janproch committed May 27, 2024
1 parent c2c814c commit 583f48a
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,29 @@ class PinoLikeLogger implements Logger {
this.log(this.packRecord(logLevelNumbers.fatal, data, msg));
}

packRecord(level: number, data, msg?): LogRecord {
packRecord(level: number, data: string | {}, msg?: string): LogRecord {
const config = this.config.getConfig ? this.config.getConfig() : this.config;
if (msg) {
// 2 parameters, unpack data
return {
...this.config.base,
...data,
...config.base,
...(data as {}),
level,
msg,
time: new Date().getTime(),
};
}
return {
...this.config.base,
...config.base,
level,
msg: data,
msg: data as string,
time: new Date().getTime(),
};
}

log(record: LogRecord) {
for (const target of this.config.targets) {
const config = this.config.getConfig ? this.config.getConfig() : this.config;
for (const target of config.targets) {
if (record.level < logLevelNames[target.level]) {
continue;
}
Expand Down Expand Up @@ -135,8 +138,9 @@ export interface LogTargetConfig {
}

export interface LogConfig {
base: {};
targets: LogTargetConfig[];
base?: {};
targets?: LogTargetConfig[];
getConfig?: () => LogConfig;
}

export function createLogger(config: LogConfig): Logger {
Expand Down

0 comments on commit 583f48a

Please sign in to comment.