diff --git a/README.md b/README.md index d0217f848..37ece601f 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,8 @@ There are two different ways to use winston: directly via the default logger, or ## Logging +Logging levels in `winston` conform to the severity ordering specified by [RFC524](https://tools.ietf.org/html/rfc5424): _severity of all levels is assumed to be numerically **ascending** from most important to least important._ + ### Using the Default Logger The default logger is accessible through the winston module directly. Any method that you could call on an instance of a logger is available on the default logger: @@ -327,6 +329,20 @@ The `exitOnError` option can also be a function to prevent exit on only certain ## Logging Levels +Each `level` is given a specific integer priority. The higher the priority the more important the message is considered to be, and the lower the corresponding integer priority. For example, `npm` logging levels are prioritized from 0 to 5 (highest to lowest): + +``` js +{ error: 0, warn: 1, info: 2, verbose: 3, debug: 4, silly: 5 } +``` + +Similarly, as specified exactly in RFC524 the `syslog` levels are prioritized from 0 to 7 (highest to lowest). + +```js +{ emerg: 0, alert: 1, crit: 2, error: 3, warning: 4, notice: 5, info: 6, debug: 7 } +``` + +If you do not explicitly define the levels that `winston` should use the `npm` levels above will be used. + ### Using Logging Levels Setting the level for your logging message can be accomplished in one of two ways. You can pass a string representing the logging level to the log() method or use the level specified methods defined on every winston Logger. @@ -351,13 +367,16 @@ Setting the level for your logging message can be accomplished in one of two way winston.info("127.0.0.1 - there's no place like home"); ``` -Winston allows you to set a `level` on each transport that specifies the level of messages this transport should log. For example, you could log only errors to the console, with the full logs in a file (note that the default level of a transport is `info`): +`winston` allows you to define a `level` property on each transport which specifies the **maximum** level of messages that a transport should log. For example, using the `npm` levels you could log only `error` messages to the console and everything `info` and below to a file (which includes `error` messages): ``` js var logger = new (winston.Logger)({ transports: [ new (winston.transports.Console)({ level: 'error' }), - new (winston.transports.File)({ filename: 'somefile.log' }) + new (winston.transports.File)({ + filename: 'somefile.log', + level: 'info' + }) ] }); ``` diff --git a/examples/custom-levels.js b/examples/custom-levels.js index e425ecc57..932e46306 100644 --- a/examples/custom-levels.js +++ b/examples/custom-levels.js @@ -12,22 +12,22 @@ var winston = require('../lib/winston'); // var config = { levels: { - silly: 0, - verbose: 1, - info: 2, + error: 0, + debug: 1, + warn: 2, data: 3, - warn: 4, - debug: 5, - error: 6 + info: 4, + verbose: 5, + silly: 6 }, colors: { - silly: 'magenta', - verbose: 'cyan', - info: 'green', - data: 'grey', - warn: 'yellow', + error: 'red', debug: 'blue', - error: 'red' + warn: 'yellow', + data: 'grey', + info: 'green', + verbose: 'cyan', + silly: 'magenta' } }; @@ -41,4 +41,4 @@ var logger = module.exports = new (winston.Logger)({ colors: config.colors }); -logger.data('hello') \ No newline at end of file +logger.data('hello') diff --git a/lib/winston/config/cli-config.js b/lib/winston/config/cli-config.js index ab4994215..764d2a8c3 100644 --- a/lib/winston/config/cli-config.js +++ b/lib/winston/config/cli-config.js @@ -9,27 +9,27 @@ var cliConfig = exports; cliConfig.levels = { - silly: 0, - input: 1, - verbose: 2, - prompt: 3, - debug: 4, - info: 5, - data: 6, - help: 7, - warn: 8, - error: 9 + error: 0, + warn: 1, + help: 2, + data: 3, + info: 4, + debug: 5, + prompt: 6, + verbose: 7, + input: 8, + silly: 9, }; cliConfig.colors = { - silly: 'magenta', - input: 'grey', - verbose: 'cyan', - prompt: 'grey', - debug: 'blue', - info: 'green', - data: 'grey', - help: 'cyan', + error: 'red', warn: 'yellow', - error: 'red' -}; \ No newline at end of file + help: 'cyan', + data: 'grey', + info: 'green', + debug: 'blue', + prompt: 'grey', + verbose: 'cyan', + input: 'grey', + silly: 'magenta' +}; diff --git a/lib/winston/config/npm-config.js b/lib/winston/config/npm-config.js index 12161a556..6402ab3af 100644 --- a/lib/winston/config/npm-config.js +++ b/lib/winston/config/npm-config.js @@ -9,19 +9,19 @@ var npmConfig = exports; npmConfig.levels = { - silly: 0, - debug: 1, - verbose: 2, - info: 3, - warn: 4, - error: 5 + error: 0, + warn: 1, + info: 2, + verbose: 3, + debug: 4, + silly: 5 }; npmConfig.colors = { - silly: 'magenta', + error: 'red', + warn: 'yellow', + info: 'green', verbose: 'cyan', debug: 'blue', - info: 'green', - warn: 'yellow', - error: 'red' -}; \ No newline at end of file + silly: 'magenta' +}; diff --git a/lib/winston/config/syslog-config.js b/lib/winston/config/syslog-config.js index 55ed5e4cf..67c6a0938 100644 --- a/lib/winston/config/syslog-config.js +++ b/lib/winston/config/syslog-config.js @@ -16,7 +16,7 @@ syslogConfig.levels = { warning: 4, notice: 5, info: 6, - debug: 7, + debug: 7 }; syslogConfig.colors = { @@ -27,5 +27,5 @@ syslogConfig.colors = { warning: 'red', notice: 'yellow', info: 'green', - debug: 'blue', -}; \ No newline at end of file + debug: 'blue' +}; diff --git a/lib/winston/logger.js b/lib/winston/logger.js index c3211c5fb..0c25d3d4d 100755 --- a/lib/winston/logger.js +++ b/lib/winston/logger.js @@ -181,8 +181,8 @@ Logger.prototype.log = function (level) { // function emit(name, next) { var transport = self.transports[name]; - if ((transport.level && self.levels[transport.level] <= self.levels[level]) - || (!transport.level && self.levels[self.level] <= self.levels[level])) { + if ((transport.level && self.levels[transport.level] >= self.levels[level]) + || (!transport.level && self.levels[self.level] >= self.levels[level])) { transport.log(level, msg, meta, function (err) { if (err) { err.transport = transport;