Skip to content
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

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

Open
thoroc opened this issue Mar 6, 2024 · 1 comment
Open

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

thoroc opened this issue Mar 6, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@thoroc
Copy link

thoroc commented Mar 6, 2024

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<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();
  1. 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));
  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 thoroc added the bug Something isn't working label Mar 6, 2024
@thoroc
Copy link
Author

thoroc commented Mar 6, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant