This is a very simple logger, that will allow you to console color coded debug statements, in addition to writing to log files. Currently three files are created info, warning, and error. Will be adding features over time.
Collaboraters welcome, looking forward to some pull requests!
> npm install ezcodin-logger
const Log = require('ezcodin-logger')
//without callback or Options
let log = new Log()
const Log = require('ezcodin-logger')
//Optional Callback
let log = new Log( result => console.log(result))
const Log = require('ezcodin-logger')
//Optional Options Object
let options = {
colors:{
error: 'red',
warning: 'yellow',
info: 'blue'
},
filePaths: {
error: './logs/ez.error.log',
warning: './logs/ez.warning.log',
info: './logs/ez.info.log'
}
}
let log = new Log( options)
const Log = require('ezcodin-logger')
//Optional Options Object
let options = {
colors:{
error: 'red',
warning: 'yellow',
info: 'blue'
},
filePaths: {
error: './logs/ez.error.log',
warning: './logs/ez.warning.log',
info: './logs/ez.info.log'
}
}
//With Options object and Callback
let log = new Log( options, result => console.log(result.msg)) //console => 'ezcodin-logger initialized'
# available colors:
'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'gray', 'redBright',
'greenBright', 'yellowBright', 'blueBright', 'magentaBright', 'cyanBright', 'whiteBright'
//debug logger
log.debugError('debug error')
log.debugWarning('debug warning')
log.debugInfo('debug info')
Log files are created in the root directory automatically upon instantiating the logger, use the options object to place logs in another location.
//Log to file
log.logError('log error')
log.logWarning('log warning')
log.logInfo('log info')
app.use(function (req, res, next) {
let err = new Error(`${req.ip} tried to reach ${req.originalUrl}, resource not found`);
err.statusCode = 404;
log.logInfo(err.statusCode + err.message )
res.status(err.statusCode).json({ status: err.statusCode, msg: err.message });
});
app.use(function(err, req, res, next) {
if (!err.statusCode) err.statusCode = 500;
log.logError(err.statusCode + err.message )
res.status(err.statusCode).json({ status: err.statusCode, msg: err.message });
}
});