Skip to content

Commit

Permalink
[breaking] Reverse log levels in npm and cli configs to conform to RF…
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Oct 29, 2015
1 parent 30d53cb commit 438331a
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 51 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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.

Expand All @@ -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'
})
]
});
```
Expand Down
26 changes: 13 additions & 13 deletions examples/custom-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
};

Expand All @@ -41,4 +41,4 @@ var logger = module.exports = new (winston.Logger)({
colors: config.colors
});

logger.data('hello')
logger.data('hello')
40 changes: 20 additions & 20 deletions lib/winston/config/cli-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
};
help: 'cyan',
data: 'grey',
info: 'green',
debug: 'blue',
prompt: 'grey',
verbose: 'cyan',
input: 'grey',
silly: 'magenta'
};
22 changes: 11 additions & 11 deletions lib/winston/config/npm-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
};
silly: 'magenta'
};
6 changes: 3 additions & 3 deletions lib/winston/config/syslog-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ syslogConfig.levels = {
warning: 4,
notice: 5,
info: 6,
debug: 7,
debug: 7
};

syslogConfig.colors = {
Expand All @@ -27,5 +27,5 @@ syslogConfig.colors = {
warning: 'red',
notice: 'yellow',
info: 'green',
debug: 'blue',
};
debug: 'blue'
};
4 changes: 2 additions & 2 deletions lib/winston/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 438331a

Please sign in to comment.