Skip to content

Instantly share code, notes, and snippets.

@gcoonrod
Created January 7, 2016 20:57
Show Gist options
  • Save gcoonrod/fc25881512fd177c3c2f to your computer and use it in GitHub Desktop.
Save gcoonrod/fc25881512fd177c3c2f to your computer and use it in GitHub Desktop.
Testing winston custom logging
> $ npm start ⬡ 4.2.1
> winston-test@1.0.0 start /Users/coonrod/dev/node/winston-test
> node index.js
2016-01-07T20:59:32.426Z - info: Logger initialized, standard log levels.
2016-01-07T20:59:32.429Z - info: Testing syslog levels. Info through emerg should be visible.
Attempting level trace
Attempting level debug
Attempting level info
2016-01-07T20:59:32.432Z - info: Testing level info
Attempting level notice
2016-01-07T20:59:32.432Z - notice: Testing level notice
Attempting level warning
2016-01-07T20:59:32.432Z - warning: Testing level warning
Attempting level error
2016-01-07T20:59:32.432Z - error: Testing level error
Attempting level crit
2016-01-07T20:59:32.432Z - crit: Testing level crit
Attempting level alert
2016-01-07T20:59:32.432Z - alert: Testing level alert
Attempting level emerg
2016-01-07T20:59:32.432Z - emerg: Testing level emerg
2016-01-07T20:59:32.432Z - info: Testing custom levels. Only info through emerg should be visible.
Attempting level trace
2016-01-07T20:59:32.433Z - trace: Testing level trace
Attempting level debug
2016-01-07T20:59:32.433Z - debug: Testing level debug
Attempting level info
2016-01-07T20:59:32.433Z - info: Testing level info
Attempting level notice
Attempting level warning
Attempting level error
Attempting level crit
Attempting level alert
Attempting level emerg
var winston = require('winston');
var transports = [];
function consoleTransport(w){
return new(w.transports.Console)({
colorize: true,
level: 'info',
timestamp: true
});
}
transports.push(consoleTransport(winston));
var levels = {
"trace": 0,
"debug": 1,
"info": 2,
"notice": 3,
"warning": 4,
"error": 5,
"crit": 6,
"alert": 7,
"emerg": 8
};
var colors = {
"trace": "magenta",
"debug": "blue",
"info": "green",
"notice": "yellow",
"warning": "red",
"error": "red",
"crit": "red",
"alert": "yellow",
"emerg": "red"
};
var logger = new (winston.Logger)({transports: transports});
logger.setLevels(winston.config.syslog.levels);
function log(message, severity){
if(severity === undefined || severity === null){
severity = 'info';
}
var args = [severity, message];
args.push.apply(args, Array.prototype.slice.call(arguments, 2));
logger.log.apply(logger, args);
}
log("Logger initialized, standard log levels.", 'info');
try{
var testLevels = [
"trace",
"debug",
"info",
"notice",
"warning",
"error",
"crit",
"alert",
"emerg"
];
log("Testing syslog levels. Info through emerg should be visible.");
testLevels.forEach(function(level){
var message = "Testing level " + level;
console.log("Attempting level " + level);
log(message, level);
});
logger.setLevels(levels);
winston.addColors(colors);
log("Testing custom levels. Only info through emerg should be visible.");
testLevels.forEach(function(level){
var message = "Testing level " + level;
console.log("Attempting level " + level);
log(message, level);
});
} catch (e){
log("Unsupported logging level!", "error", e);
}
{
"name": "winston-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"author": "",
"license": "MIT",
"dependencies": {
"winston": "^2.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment