dush-logger
v1.2.2
Published
A file logger for Node.js apps
Downloads
29
Maintainers
Readme
DUSH-LOGGER
Easy, flexible, clean file logger for node
Installation
npm i dush-logger
npm install dush-logger
Documentation
Basic Use
const log = require('dush-logger');
let myVariable = true;
// Do Something
if (myVariable) {
log.info('Variable `myVariable` is true');
} else {
log.error('Variable `myVariable` is false');
}
Methods
log.<level>(message)
level
{String} The level to log a message atmessage
{String} The log message
Enter a new log message
Examples
// Below are the default log levels
log.info('This is an info log message');
log.notice('This is a notice log message');
log.success('This is a success log message');
log.warn('This is a warning log message');
log.error('This is an error log message');
log.debug('This is a debug log message');
log.critical('This is is critical log message');
log.verbose('This is a verbose log message');
log.createLevel(level, options)
level
{String}options
{Object}color
: {String} Log level color(hex)
prefix
: {String} String to be used as prefix of log messagefile
: {String} File path for the level
Create a new log level
// Create new level named server
log.createLevel('server', {prefix: 'SERV',color: '#28a745', file:'./logs/server.log'});
// Use the created level
log.server('New log messages under the server level');
log.clearLevel(level)
level
{String}
NB : Set level to 'main' to clear the main or default log file
Deletes all the log messages previously written under the level.
// Clearing the log file for the info level
log.clearLevel('info');
log.deleteLevel(level)
level
{String}
Deletes the log level ,any further attempt to use the level will result false
and produce a message.
// Delete error level
log.deleteLevel('error');
// Trying to log a new message using the error level will result in an error
log.error('My new error log message');
log.viewLog(level)
level
{String}
Outputs previously written log messages to the console and returns the messages as an array
log.viewLog('critical');
log.init(options)
options
{Object}file
: {String} Set path for default log fileuse_individual_files
: {Boolean}true
|false
Set to true to write in level's independent fileslevels
: {Object} A showing level configurations including new levels<level>
{Object} Level configurationcolor
: {String} Log level color(hex)prefix
: {String} String to be used as prefix of log messagefile
: {String} File path for the level
Set options for log
/* Below are default options
{
file: './main.log',
use_individual_files: false,
dateFormat: 'nothing-yet',
levels: {
info: {
color:'#ffffff',
prefix: 'INFO',
file: 'logs/info.log'
},
debug: {
color:'#1e7e34',
prefix: 'DEBUG',
file: 'logs/debug.log'
},
verbose: {
color:'#28a745',
prefix: 'VERBOSE',
file: 'logs/verbose.log'
},
notice: {
color:'#007bff',
prefix: 'NOTICE',
file: 'logs/notice.log'
},
success: {
color:'#28a745',
prefix: 'SUCCESS',
file: 'logs/success.log'
},
warn: {
color:'#ffc107',
prefix: 'WARN',
file: 'logs/warning.log'
},
error: {
color:'#dc3545',
prefix: 'ERROR',
file: 'logs/error.log'
},
critical: {
color:'#fd7e14',
prefix: 'CRITIC',
file: 'logs/critical.log'
}
}*/
log.init({
'use_individual_files': true,
'levels': {
'info': {
'prefix': 'INFORMATION'
},
'debug': {
'prefix': 'DEBUGING',
'file': './logs/debugger.log'
}
}
});
NB : Use the configuration file instead of log.init
if you want to set override options across multiple files
log.setLogFile(level, file)
level
{String}file
{String(path)}
Set the log file to be used for the level
log.setLogFile('info', './logs/info.log');
log.info('New info log message');
// The above log message will be written in the file ./logs/info.log instead of the default log file
Configuration File
Dush allows you to override default options using the dush configuration file. You simply create a dush-config.json
file in the directory containing your node app file.
Example dush-config.json
{
"use_individual_files": true,
"levels": {
"info": {
"prefix": "INFORMATION"
},
"debug": {
"prefix": "DEBUG",
"file": "./logs/debug.log"
}
}
}