question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Child logger loses its transports when added to `winston.loggers`

See original GitHub issue

So I’m starting to use winston after transferring from python to JS. I am used to child loggers from logging and so I tried to do the same with winston.

Here is my config:

import winston from 'winston';

const base_logger = winston.create({
    level: 'info',
    format: winston.format.json(),
    transports: [
        new winston.transports.Console({}),
    ]
});

const child = base_logger.child({});
console.log(child.transports);  // working perfectly, has the console transport
winston.loggers.add('child', child);
console.log(winston.loggers.get('child'). transports); // doesn't work, shows an empty array

I will debug the code to try to find the problem, but I am about 90% sure I am using winston wrong.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
kouhincommented, Jan 13, 2021

I think this is a bug of winston.Container.

You can simply confirm this issue by following code:

const consoleTransport = new winston.transports.Console();
const log = new winston.Container({
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.simple()
  ),
  transports: [consoleTransport],
});

log.add('test1', {
  level: 'warn',
});

log.add('test2', {
  level: 'info',
});

const log1 = log.get('test1');
const log2 = log.get('test2');

// You can see both log1 and log2 are set to 'warn'

log1.info('[info] log1');
log1.warn('[warn] log1');
log2.info('[info] log2');
log2.warn('[warn] log2');

// This is the reason
console.info(consoleTransport.parent === log1, consoleTransport.parent === log2);
// Output: true false
0reactions
robross0606commented, Sep 23, 2021

I just ran into this as well. I wanted to have a default logger, and then spin off child loggers that are named. It don’t see how this is possible with the current API.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A Complete Guide to Winston Logging in Node.js - Better Stack
Winston is the most popular logging library for Node.js. ... to use two File transports, one that logs all messages to a combined.log...
Read more >
Add module name in winston log entries - node.js
I found a better way to do this. I added an additional layer over the winston logger, in this case a function, that...
Read more >
How to use the winston.createLogger function in winston - Snyk
createLogger ({ level: 'info', format: winston.format.json(), transports: [ // - Write to all logs with level `info` and below to `combined.log` ...
Read more >
Node Logging Basics - The Ultimate Guide To Logging - Loggly
Winston. “A multi-transport async logging library for Node.js.” ... Child loggers specialize the parent logger object, effectively adding more context to ...
Read more >
Best practices to collect, customize and centralize Node.js logs
You also want to spend some time configuring relevant logging transports. If you need to add a file logger, for example, don't forget...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found