Skip to content

Commit

Permalink
Fix #1: Adding an option to print the level in String instead of Integer
Browse files Browse the repository at this point in the history
This commit fixes Issue #1 by adding an option to display the level
using its String representation. Suggesting it to be the 0.2.0 version.

*	modified:   lib/format-record.js
- Adding a method to map the level suggested by @olsonpm at
  trentm/node-bunyan#194 (comment)
- If the option is used, just change the level.

*	modified:   README.md
- Updating the documentation by adding the examples of outputing the
  reports using the levels in String.

*	new file:   example/bunyan-string-level.js
- Adding the new example that logs the level using the string
  representation.

*	modified:   package.json
- Bumping the version to 0.2.0
  • Loading branch information
marcellodesales authored and thlorenz committed Jan 19, 2015
1 parent e3d016b commit ba068ad
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ log.error('OOOOHHH it burns!', new Error('temperature: 200'));
log.fatal('I died! Do you know what that means???');
```

* Printing the level in String representation for Json objects

```js
var bunyan = require('bunyan')
, bformat = require('../')
, formatOut = bformat({ outputMode: 'bunyan', levelInString: true })
;
```

The output would use the string levels:

```
$ node example/json-string-level.js
{"name":"app","hostname":"ubuntu","pid":28081,"level":"INFO","msg":"starting up","time":"2014-12-01T19:41:29.136Z","v":0}
{"name":"app","hostname":"ubuntu","pid":28081,"level":"DEBUG","msg":"things are heating up { temperature: 80,\n status: { started: 'yes', overheated: 'no' } }","time":"2014-12-01T19:41:29.142Z","v":0}
{"name":"app","hostname":"ubuntu","pid":28081,"level":"WARN","msg":"getting a bit hot { temperature: 120 }","time":"2014-12-01T19:41:29.143Z","v":0}
{"name":"app","hostname":"ubuntu","pid":28081,"level":"ERROR","msg":"OOOOHHH it burns! [Error: temperature: 200]","time":"2014-12-01T19:41:29.144Z","v":0}
{"name":"app","hostname":"ubuntu","pid":28081,"level":"FATAL","msg":"I died! Do you know what that means???","time":"2014-12-01T19:41:29.144Z","v":0}
```

![demo](https://github.com/thlorenz/bunyan-format/raw/master/assets/bunyan-format-demo.gif)

## Installation
Expand Down
14 changes: 14 additions & 0 deletions example/bunyan-string-level.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var bunyan = require('bunyan')
, bformat = require('../')
, formatOut = bformat({ outputMode: 'bunyan', levelInString: true })
;

var log = bunyan.createLogger({ name: 'app', stream: formatOut, level: 'debug' } );

log.info('starting up');
log.debug('things are heating up', { temperature: 80, status: { started: 'yes', overheated: 'no' } });
log.warn('getting a bit hot', { temperature: 120 });
log.error('OOOOHHH it burns!', new Error('temperature: 200'));
log.fatal('I died! Do you know what that means???');
30 changes: 30 additions & 0 deletions lib/format-record.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,29 @@ function stylizeWithoutColor(str, color) {
return str;
}

/**
* @param {int} level is the level of the record.
* @return The level value to its String representation.
* This is only used on json-related formats output and first suggested at
* https://github.com/trentm/node-bunyan/issues/194#issuecomment-64858117
*/
function mapLevelToName(level) {
switch (level) {
case TRACE:
return 'TRACE';
case DEBUG:
return 'DEBUG';
case INFO:
return 'INFO';
case WARN:
return 'WARN';
case ERROR:
return 'ERROR';
case FATAL:
return 'FATAL';
}
}

/**
* Print out a single result, considering input options.
*/
Expand Down Expand Up @@ -368,9 +391,15 @@ module.exports = function formatRecord(rec, opts) {
return util.inspect(rec, false, Infinity, true) + '\n';

case OM_BUNYAN:
if (opts.levelInString) {
rec.level = mapLevelToName(rec.level);
}
return JSON.stringify(rec, null, 0) + '\n';

case OM_JSON:
if (opts.levelInString) {
rec.level = mapLevelToName(rec.level);
}
return JSON.stringify(rec, null, opts.jsonIndent) + '\n';

case OM_SIMPLE:
Expand All @@ -386,3 +415,4 @@ module.exports = function formatRecord(rec, opts) {
throw new Error('unknown output mode: '+opts.outputMode);
}
}

0 comments on commit ba068ad

Please sign in to comment.