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

Child logger from winston default logger #1767

Closed
1 of 2 tasks
bobvanderlinden opened this issue Feb 25, 2020 · 4 comments
Closed
1 of 2 tasks

Child logger from winston default logger #1767

bobvanderlinden opened this issue Feb 25, 2020 · 4 comments

Comments

@bobvanderlinden
Copy link

Please tell us about your environment:

  • winston version?
    • winston@2
    • winston@3
  • node -v outputs: v12.13.0
  • Operating System? MacOS
  • Language? ES5

What is the problem?

The following is not possible:

const mychildlog = winston.child({ label: 'somelabel' })

While the documentation gives the following 2 statements:

You can create child loggers from existing loggers to pass metadata overrides

See https://github.com/winstonjs/winston#creating-child-loggers

and

The default logger is accessible through the winston module directly. Any method that you could call on an instance of a logger is available on the default logger

See https://github.com/winstonjs/winston#using-the-default-logger

However, it is not possible to call child on winston. This is inconvenient.

My usecase is the following:

const getLogger = function(module) {
    const path = module.filename.split('/').slice(-2).join('/');

    return winston.child({
      label: path
    });
};

So that I can have the module filename in the logs.

The documentation says the following:

What do you expect to happen instead?

I expect the following to work:

const mychildlog = winston.child({ label: 'somelabel' })
mychildlog.info('hello')
@Wernfried
Copy link

Wernfried commented Dec 17, 2021

I guess you are looking for a solution like this:

const logger = createLogger({
   defaultMeta: { mainLabel: 'default label' },
   level: 'info',
   format: combine(
      timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }),
      printf(({ message, timestamp, level, mainLabel, childLabel }) => {
         return `${timestamp} (${childLabel || mainLabel}) [${level}] -> ${message}`;
      })
   ),
   transports: [
      new transports.Console()
   ],
});

const childLogger = logger.child({ childLabel: 'child label' });

logger.info('the message');
childLogger.info('the child message');

Output

2021-12-17 17:46:07.781 (default label) [info] -> the message
2021-12-17 17:46:07.784 (child label) [info] -> the child message

Note, you cannot overwrite defaultMeta: { mainLabel: 'default label' }, (at least I did not manage it) thus you have to work with different variables.

@wbt
Copy link
Contributor

wbt commented Dec 17, 2021

This looks related to PR #1989, regarding the overwriting with the same variables.

@maverick1872
Copy link
Member

@bobvanderlinden @wbt Per the issue author they wanted to be able to create a new child logger from the default logger instance that is exported. It appears that this was solved by #1603. As such I am closing this issue.

@DABH do you know if we've cut a release and published it to NPM, that has this feature in it? If you don't know off the top of your head, that's fine and I'll confirm later.

@DABH
Copy link
Contributor

DABH commented Jan 14, 2022

Yeah, we have done a release since then, so this must be included now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants