What is loglevel-colored-level-prefix?
The loglevel-colored-level-prefix package is an extension for the loglevel logging library that adds colored level prefixes to log messages. This helps in distinguishing log levels visually, making it easier to read and debug logs.
What are loglevel-colored-level-prefix's main functionalities?
Colored Level Prefixes
This feature adds colored prefixes to log messages based on their level (info, warn, error, etc.). The colors help in quickly identifying the severity of the messages.
const log = require('loglevel');
const prefix = require('loglevel-colored-level-prefix');
prefix.apply(log);
log.info('This is an info message');
log.warn('This is a warning message');
log.error('This is an error message');
Custom Prefix Format
This feature allows you to customize the format of the log message prefixes. You can define a template that includes the log level, timestamp, and message.
const log = require('loglevel');
const prefix = require('loglevel-colored-level-prefix');
prefix.apply(log, {
template: '[%l] %t: %m',
level: 'info'
});
log.info('This is an info message');
log.warn('This is a warning message');
log.error('This is an error message');
Custom Colors
This feature allows you to customize the colors used for different log levels. You can use any color supported by the chalk library.
const log = require('loglevel');
const prefix = require('loglevel-colored-level-prefix');
prefix.apply(log, {
level: 'info',
format: {
info: chalk.blue,
warn: chalk.yellow,
error: chalk.red
}
});
log.info('This is an info message');
log.warn('This is a warning message');
log.error('This is an error message');
Other packages similar to loglevel-colored-level-prefix
winston
Winston is a versatile logging library for Node.js that supports multiple transports (e.g., console, file, HTTP) and log levels. It also supports custom formatting and coloring of log messages. Compared to loglevel-colored-level-prefix, Winston offers more advanced features and flexibility but may be more complex to set up.
bunyan
Bunyan is another logging library for Node.js that provides a simple and fast JSON-based logging system. It supports log levels, custom serializers, and streams. Bunyan's focus on JSON output makes it different from loglevel-colored-level-prefix, which focuses on colored text output.
pino
Pino is a fast and low-overhead logging library for Node.js. It supports log levels, custom serializers, and transports. Pino is designed for high performance and low overhead, making it suitable for production environments. Unlike loglevel-colored-level-prefix, Pino does not focus on colored output but on performance.
loglevel-colored-level-prefix
loglevel plugin that adds colored level prefix (node only)
The problem
loglevel
is great, and I find that I often want the log output to
be formatted the same way every time. Also I don't really like some of the
implementation of loglevel
(specifically the fact that it uses some browser
APIs for some things.
This solution
This exposes a function to get a logger (singleton) with colored prefixes for
the level. Note that this only works in Node because it uses chalk
.
Installation
This module is distributed via npm which is bundled with node and should
be installed as one of your project's dependencies
:
npm install --save loglevel-colored-level-prefix
Usage
const getLogger = require('loglevel-colored-level-prefix')
const options = {prefix: 'your-prefix', level: 'trace'}
const logger = getLogger(options)
logger.trace('WOW! What the stack trace!?')
logger.debug('sup debug?')
logger.info('Hey info')
logger.warn('Hi warn')
logger.error('Hello error')
Let's look at what that actually looks like...
options
prefix
?String - Whatever you want your prefix to be. Normally this is the tool
that you're logging for. The getLogger
function will return the same instance
of the logger based on the given prefix.
level
?String - What you want the initial level to be set to. This defaults to:
process.env.LOG_LEVEL || 'warn'
. Possible options are (in order of verbosity):
trace
, debug
, info
, warn
, error
.
returns
An instance of a loglevel
logger. Learn more about that API from the
loglevel
docs.
Inspiration
I wrote this because I wanted to use the plugin I created for
prettier-eslint
in
prettier-eslint-cli
. And I'll probably use it in other
projects/tools as well.
Other Solutions
I'm unaware of other plugins for loglevel
that do what this one does. But
there are many logging solutions out there...
Contributors
Thanks goes to these people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
LICENSE
MIT