fullstack-build / tslog

📝 tslog - Universal Logger for TypeScript and JavaScript
https://tslog.js.org
MIT License
1.33k stars 63 forks source link

Bug: Custom logger report calls from customer logger file #282

Open thoroc opened 8 months ago

thoroc commented 8 months ago

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:

  1. 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 extends Logger { constructor(settings?: ISettingsParam, 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();

2. Use it in your code somewhere:
```javascript
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));
  1. 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

thoroc commented 8 months ago

Yes I am aware this is using chalk to get some colors in.

Lyoko-Jeremie commented 2 months ago

same issue

Lyoko-Jeremie commented 2 months ago

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');