A little customizable logger for NodeJS.
Log to the console, to a file, to a remote service or create a custom one.
Check out the new v1.x beta here
- βοΈ Easy
- βοΈ Customizable
- βοΈ Liteweighted
- Install with npm
npm install loggin-js --save// Require the logging library
const logging = require('loggin-js');Check the wiki for more in depth documentation.
Check this other repo for better integration with express.
The fastest way of creating a logger is by using the .getLogger method wich creates a logger based on some options.
Based on those options it will create one of [ConsoleLogger, FileLogger, RemoteLogger] _explained below.
Here is a little example:
const logging = require('loggin-js');
const logger = loggin.getLogger({ channel: 'my-cool-app' });
logger.debug('User is loggin in');Now let's check the .getLogger method a bit more in depth.
All Loggers and .getLogger accept what is called a Severity level, wich is used internally for filtering and managing log output. You can check more info about Severities here.
We can set a level in three ways:
- Passing a string (info):
logger.getLogger({ level: 'DEBUG' })
- Passing an int (info):
logger.getLogger({ level: 9 })
- Passing a severity instance (info):
const { Severity } = logging; logging.getLogger({ level: Severity.DEBUG });
You can also pass a set of options, like setting colored output, changing the format, and more.
// Require the logging library
const logging = require('loggin-js');
// Shortcuts
const { Severity } = logging;
// Get a logger with DEBUG severity.
// Severity DEBUG will output any severity.
const logger = logging.getLogger({
// level can be a <string> = 'DEBUG' a <int> = 7 or a <Severity> = Severity.DEBUG
level: 'DEBUG',
// If output should be colored
color: true,
// Set formatter to medium - one of: ['short', 'medium', 'long']
formatter: 'medium',
});
// Does the same as passing into settings, as done above
logger.setLevel(Severity.DEBUG);
logger.setColor(true);
logger.setFormatter('medium');
// Available predefined log levels
logger.info('info', { user: 'pedro', id: 10 });
logger.error('error');
logger.warning('warning');
logger.alert('alert');
logger.emergency('emergency');
logger.critical('critical');
logger.debug('debug');
logger.notice('Notice', {}, 'channel');
// If enabled set to false logs will not be output
logger.setEnabled(false);Log to files instead of the console
We create it making use of the FileLogger class.
// Require the logging library
const logging = require('loggin-js');
// Shortcut for the severity constants
const { Severity, Loggers, Notifiers } = logging;
// Create a file logger
const logger = new Loggers.FileLogger({
// Display line number at the begining of the log
lineNumbers: true,
// You can pass a pipes array to the file logger or you can do after instancing (showed below)
pipes: [
// Here we create a pipe that will pipe level ERROR logs to the file 'logs/error-logs.log'
new Notifiers.Pipe(Severity.ERROR, 'logs/error-logs.log'),
// This one will pipe level INFO logs to the file 'logs/info-logs.log'
new Notifiers.Pipe(Severity.INFO, 'logs/info-logs.log')
]
});
// You can also add pipes after creating the logger as follows
logger.pipe(Severity.ERROR, 'logs/error-logs.log');
logger.pipe(Severity.INFO, 'logs/info-logs.log');
// INFO message will log to 'logs/info-logs.log'
logger.info('Logging a info log');
// ERROR message will log to 'logs/error-logs.log'
logger.error('Logging a error log', new Error('An error'));
// Will not be logged as only the ERROR and INFO severities will be output to their respective files
logger.warning('Logging a warning log');
logger.notice('Logging a notice log');
logger.alert('Logging a error log');Custom formatter, customize the output of the log
const logging = require('loggin-js');
const logger = logging.getLogger({
level: logging.Severity.DEBUG,
color: true,
/**
* You can also use a custom formatter if the default one does not satisfy your needs.
* In the formatter you can access all log properties and you can also set the
* color of some segments of the log by using <%L> where L is one of:
* - r red
* - g green
* - gr gray
* - b blue
* - p pink
* - y yellow
* - c cyan
* - m magenta
* - (nnn) a number between 0-255 # not implemented yet
*/
formatter: '[{time.toLocaleString}] - <%m{user}> | {severityStr} | {message} - {JSON.stringify(data)}'
});
// Set user to root
logger.setUser('root');
// Set formatter
logger.setFormatter('[{time.toLocaleString}] - <%m{user}> | {severityStr} | {message} - {JSON.stringify(message)}');
// Log something
logger.debug('debug'); // $ [2018-6-2 00:46:24] - root - DEBUG - debug
logger.info('info', {data: 'Hi'}); // $ [2018-6-2 00:46:24] - root - INFO - info - {"data":"Hi"}
/**
* Aditionally you can set the color of some parts of the message:
* The output will be something like, with the last ERROR beeing red:
* $ [2018-6-2 00:46:24] - root - ERROR - There was an ERROR
*/
logger.error('There was an <%rERROR>'); If you found a bug or like to leave a feature request, please leave an issue and we will take care of it.
Just make sure it's not already filed.
Hi there, if you like the project don't hesitate in collaborating (if you like to), submit a pull request, post an issue, ...
Any help or ideas are apreciated!