What is console-log-level?
The console-log-level npm package is a simple logger for Node.js that allows you to log messages at different levels (trace, debug, info, warn, error, fatal). It is lightweight and easy to use, making it suitable for small to medium-sized applications where you need basic logging functionality.
What are console-log-level's main functionalities?
Basic Logging
This feature allows you to log messages at different levels such as info, warn, and error. You can set the logging level when initializing the logger.
const log = require('console-log-level')({ level: 'info' });
log.info('This is an info message');
log.warn('This is a warning message');
log.error('This is an error message');
Custom Logging Levels
This feature allows you to use custom logging levels like debug and trace, providing more granular control over the logging output.
const log = require('console-log-level')({ level: 'debug' });
log.debug('This is a debug message');
log.trace('This is a trace message');
Conditional Logging
This feature allows you to conditionally log messages based on the set logging level. In this example, only messages at the 'warn' level or higher will be logged.
const log = require('console-log-level')({ level: 'warn' });
log.info('This will not be logged');
log.warn('This will be logged');
Other packages similar to console-log-level
winston
Winston is a versatile logging library for Node.js with support for multiple transports (e.g., console, file, HTTP). It offers more advanced features compared to console-log-level, such as log formatting, log levels, and custom transports.
bunyan
Bunyan is a simple and fast JSON logging library for Node.js. It provides a structured logging approach, which is useful for log analysis and monitoring. It also supports log levels and multiple output streams.
pino
Pino is a low-overhead logging library for Node.js that focuses on performance. It provides fast logging with minimal impact on application performance. Pino also supports log levels and JSON output, making it suitable for high-performance applications.
console-log-level
A dead simple logger. Will log to STDOUT or STDERR depending on the
chosen log level. It uses console.info
, console.warn
and
console.error
and hence supports the same API.
Log levels supported: trace, debug, info, warn, error and fatal.
Installation
npm install console-log-level
Example usage
var log = require('console-log-level')({ level: 'info' })
log.trace('a')
log.debug('b')
log.info('c')
log.warn('d')
log.error('e')
log.fatal('f')
Options
Configure the logger by passing an options object:
var log = require('console-log-level')({
prefix: function (level) {
return new Date().toISOString()
},
level: 'info'
})
level
A string
to specify the log level. Defaults to info
.
prefix
Specify this option if you want to set a prefix for all log messages.
This must be a string
or a function
that returns a string.
Will get the level of the currently logged message as the first
argument.
stderr
A boolean
to log everything to stderr. Defauls to false
.
License
MIT