What is glogg?
The glogg npm package is a logging utility designed to enable global logging. It allows developers to create a global logger that can be required and used across different modules in a Node.js application. This makes it easier to manage and configure logging in a centralized manner, especially in large-scale applications where consistent logging practices are crucial.
What are glogg's main functionalities?
Creating a global logger
This feature allows you to create a global logger instance that can be used throughout your application. You can name your logger (in this case, 'my-logger') and use various methods such as 'info', 'warn', 'error', etc., to log messages.
const logger = require('glogg')('my-logger');
logger.info('This is an info message');
Listening to log events
This feature enables you to listen to log events. You can attach event listeners to your logger for different log levels (e.g., 'info', 'warn', 'error') and define custom actions, such as logging to a file or sending notifications, when these events occur.
const logger = require('glogg')('my-logger');
logger.on('info', (msg) => {
console.log(`Info: ${msg}`);
});
logger.info('This is an info message');
Other packages similar to glogg
winston
Winston is a multi-transport async logging library for Node.js. It is similar to glogg in that it provides a powerful and flexible logging mechanism. However, winston offers more customization options, including multiple logging transports (e.g., console, file, HTTP) and custom log levels.
bunyan
Bunyan is a simple and fast JSON logging library for Node.js services. Like glogg, it supports the concept of a global logger but focuses on JSON for log output, making it more suitable for machine parsing. Bunyan also provides stream-based logging and log rotation capabilities.
pino
Pino is a very low overhead Node.js logger, which is designed for speed and producing logs in a JSON format. It compares to glogg by offering high-performance logging capabilities. Pino also emphasizes child loggers and includes a rich set of tools for processing log files.
glogg
Global logging utility.
Usage
var getLogger = require('glogg');
var logger = getLogger('my-namespace');
logger.debug('The MOST verbose!');
logger.info('Some important info');
logger.warn('All the warnings to you');
logger.error('OH NO! SOMETHING HAPPENED!');
logger.info('%s style!', 'printf');
logger.debug({ my: 'obj' });
logger.info([1, 2, 3]);
logger.on('info', function (msg) {
});
logger.on('error', function (msg) {
});
API
Note: This module makes no assumptions about the log levels and they will always
be emitted. If you are looking to filter some out, your listeners will need to have
extra logic.
getLogger([namespace])
Create a new logger at the given namespace (or the default if no namespace is provided).
Returns an augmented sparkles
EventEmitter object
with 4 methods: debug()
, info()
, warn()
and error()
. When called, these methods emit
an event with the same name. If the first argument is a string, the arguments
are passed through node's util.format()
before being emitted. Other parts
of a node program can get the logger by namespace and listen for the events to
be emitted.
logger.debug(msg, ...args)
Emits a debug
event with the given msg
.
If the first argument is a string, all arguments are passed to node's
util.format()
before being emitted.
If the first argument is not a string, all arguments will be emitted directly.
logger.info(msg, ...args)
Emits a info
event with the given msg
.
If the first argument is a string, all arguments are passed to node's
util.format()
before being emitted.
If the first argument is not a string, all arguments will be emitted directly.
logger.warn(msg, ...args)
Emits a warn
event with the given msg
.
If the first argument is a string, all arguments are passed to node's
util.format()
before being emitted.
If the first argument is not a string, all arguments will be emitted directly.
logger.error(msg, ...args)
Emits a error
event with the given msg
.
If the first argument is a string, all arguments are passed to node's
util.format()
before being emitted.
If the first argument is not a string, all arguments will be emitted directly.
Note: You must handle this event in some way or the node process will crash
when an error
event is emitted.
logger.on(event, fn)
Standard API from node's EventEmitter
. Use this to listen for events from
the logger methods.
License
MIT