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.

Logging in the separate thread

See original GitHub issue

Is it possible to delegate logging to separate thread in NodeJS using Winston out of the box?

I want to minimize blocking operations and other side affects for performance.

And I think about moving logging to the separate thread.


const winston = require('winston');
const { Worker, isMainThread,  workerData } = require('worker_threads');

class MyLogger
{
    info(message) {
        if (isMainThread) {
            this.thread = new Worker(__filename)
        } else {
            this.getLoggerInstance().info(message);
        }
    }

    getLoggerInstance() {
        if (! this.logger) {
            this.logger = winston.createLogger({
                level: 'info',
                format: winston.format.json(),
                defaultMeta: { service: 'user-service' },
                transports: [
                    new winston.transports.File({ filename: 'error.log', level: 'error' }),
                    new winston.transports.File({ filename: 'combined.log' })
                ]
            });
        }
        return this.logger;
    }
}


// USAGE
const myLogger = new MyLogger();

myLogger.info('something I want to log');

Does anyone tries to do the same?

Do you know any limitations?

Will it help for performance?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
cjbarthcommented, Nov 9, 2021

Pino has been around for a while and works well, however, because it logs in a separate thread it may actually create issues for some types of applications.

For example, if you are working with a worker pool, depending on how you configure your logger and your pool, you can have messages interleaved in unexpected ways and you might have less resources available to process incoming connections due to a smaller pool size. A separate thread isn’t always the correct solution; the entire system must be looked at together.

Case in point, if you log to syslog, then there is very little logging overhead for Node. Granted, you still have to transform the object, but then, as fast as it can be written to a socket, it is out of Node’s hands. The overhead of transforming the object would need to be done in some form if you tried to get that object to another thread to have it logged from there. So, you save yourself nothing.

In fact, you could easily say that syslog would be the multi-threaded solution 😃

1reaction
Saegercommented, Nov 6, 2021

There is a new logger called “pino” which does exactly the initial intention of this post, by actually writing logs in a separate thread with worker threads. Check it out here: https://github.com/pinojs/pino

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is creating a separate thread for a logger ok? - Stack Overflow
The (dis)advantages of logging from each thread directly are the inverse of the above. No two threads can log at the same time...
Read more >
Logging requests on a separate thread
I have an ASP.NET MVC 5 application that needs to log to the database every request for a specific controller action and every...
Read more >
Thread-Safe Logging in Python
You can log directly from multiple threads because the logging module ... Different threads may perform different tasks and may encounter a ...
Read more >
SEQ, Serilog and mssing events. Due to threading?
I guess it has something to due with threading and maybe the logging object ... simply returns ok to the client and starts...
Read more >
Multithreaded Logger - How-To - YouTube
Thread Pool Tutorial - How-To · Logging Tutorial in Python | DON'T use Print for logging | How to Log messages in Python...
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