Bug: Custom logger report calls from customer logger file
Describe the bug I would expect the custom log level implemented as per documentation to report on the file where the call was made instead of the file where the custom logger lives.
To Reproduce Steps to reproduce the behavior:
- create a custom logger as per docs:
import { Logger, ISettingsParam, ILogObjMeta } from 'tslog';
export interface ActionProps {
action: string;
message: string;
resources?: string[];
}
export class CustomLogger<LogObj> extends Logger<LogObj> {
constructor(settings?: ISettingsParam<LogObj>, logObj?: LogObj) {
super(settings, logObj);
}
public action(...args: unknown[]): (LogObj & ILogObjMeta) | undefined {
args = args.map((arg) => {
if (typeof arg === 'object') {
const { action, message, resources } = arg as ActionProps;
let msg = `${chalk.green(action)} ${message}`;
if (resources) {
msg += ` ${resources.map((r) => chalk.yellow(r)).join(', ')}`;
}
return msg;
}
});
return super.log(3, 'ACTION', ...args);
}
public custom(...args: unknown[]): (LogObj & ILogObjMeta) | undefined {
return super.log(8, 'CUSTOM', ...args);
}
}
export const log = new CustomLogger();
- Use it in your code somewhere:
log.action({ action: 'added', message: 'new pipeline', resources: [pipeline.id] });
log.custom(chalk.green('added'), 'new pipeline', chalk.yellow(pipeline.id));
log.info(chalk.green('added'), 'new pipeline', chalk.yellow(pipeline.id));
- Observe the logs
2024-03-06 08:30:10.463 ACTION /src/utils/logger.ts:29 added new pipeline dv
2024-03-06 08:30:10.464 CUSTOM /src/utils/logger.ts:38 added new pipeline dv
2024-03-06 08:30:10.464 INFO /src/generator.ts:35 added new pipeline dv
Expected behavior
the first two calls should have been reported being made in generator.ts in line 33 and 34 but instead were reported to have been made in logger.ts
Screenshots
Additional context Add any other context about the problem here.
Node.js Version v20.11.0
OS incl. Version MacOS: 14.3.1 (23D60)
Shell Zshell with oh-my-zshell
Yes I am aware this is using chalk to get some colors in.
same issue
the mini reproduct is :
export class CustomLoggerGenerator<LogObj extends any> extends Logger<LogObj> {
public customLog(...args: unknown[]): LogObj & ILogObjMeta | undefined {
return this.log(3, 'LOG', ...args);
}
}
// then create it
const lg = new CustomLoggerGenerator();
lg.customLog('log it');