Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set log level in Winston/Node.js

I am using Winston logging with my Node.js app and have defined a file transport. Throughout my code, I log using either logger.error, logger.warn, or logger.info.

My question is, how do I specify the log level? Is there a config file and value that I can set so that only the appropriate log messages are logged? For example, I'd like the log level to be "info" in my development environment but "error" in production.

like image 463
Silent User Avatar asked Mar 20 '13 22:03

Silent User


People also ask

How do I set up a Winston logger?

Here's a guide that will help you understand NPM in detail. Install the Winston package using the command npm install winston . Logging with Winston is simple, with just four steps, as shown in the example below, and you have your log recorded. Add the Winston module with a require() function.

How do you change the logging level?

To change log levels as a root user, perform the following: To enable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=DEBUG) To disable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=INFO)

What is Winston logs?

winston is designed to be a simple and universal logging library with support for multiple transports. A transport is essentially a storage device for your logs. Each winston logger can have multiple transports (see: Transports) configured at different levels (see: Logging levels).


1 Answers

If you are using the default logger, you can adjust the log levels like this:

const winston = require('winston'); // ... winston.level = 'debug'; 

will set the log level to 'debug'. (Tested with winston 0.7.3, default logger is still around in 3.2.1).

However, the documentation recommends creating a new logger with the appropriate log levels and then using that logger:

const myLogger = winston.createLogger({   level: 'debug' }); myLogger.debug('hello world'); 

If you are already using the default logger in your code base this may require you to replace all usages with this new logger that you are using:

const winston = require('winston'); // default logger winston.log('debug', 'default logger being used');  // custom logger myLogger.log('debug', 'custom logger being used'); 
like image 199
AndreasPizsa Avatar answered Sep 21 '22 04:09

AndreasPizsa