[issue #210] Export bunyan.nameFromLevel and bunyan.levelFromName

This commit is contained in:
Trent Mick 2015-01-15 22:18:29 -08:00
parent 2f95bb8091
commit 7c0b566e63
3 changed files with 16 additions and 10 deletions

View file

@ -8,6 +8,9 @@ Known issues:
## bunyan 1.2.4 (not yet released) ## bunyan 1.2.4 (not yet released)
- [issue #210] Export `bunyan.nameFromLevel` and `bunyan.levelFromName`. It can
be a pain for custom streams to have to reproduce that.
- [issue #100] Gracefully handle the case of an unbound - [issue #100] Gracefully handle the case of an unbound
`Logger.{info,debug,...}` being used for logging, e.g.: `Logger.{info,debug,...}` being used for logging, e.g.:

View file

@ -995,17 +995,14 @@ For some, the raw log records might not be desired. To have a rendered log line
you'll want to add your own stream, starting with something like this: you'll want to add your own stream, starting with something like this:
```javascript ```javascript
var bunyan = require('./lib/bunyan');
function MyRawStream() {} function MyRawStream() {}
MyRawStream.prototype.write = function (rec) { MyRawStream.prototype.write = function (rec) {
var nameFromLevel = { console.log('[%s] %s: %s',
TRACE: 'TRACE' rec.time.toISOString(),
DEBUG: 'DEBUG', bunyan.nameFromLevel[rec.level],
INFO: 'INFO', rec.msg);
WARN: 'WARN',
ERROR: 'ERROR',
FATAL: 'FATAL'
};
console.log('[%s] %s: %s', rec.time, nameFromLevel[rec.level], rec.msg);
} }
var log = bunyan.createLogger({ var log = bunyan.createLogger({
@ -1015,7 +1012,7 @@ var log = bunyan.createLogger({
level: 'info', level: 'info',
stream: new MyRawStream(), stream: new MyRawStream(),
type: 'raw' type: 'raw'
}, }
] ]
}); });

View file

@ -197,6 +197,10 @@ var levelFromName = {
'error': ERROR, 'error': ERROR,
'fatal': FATAL 'fatal': FATAL
}; };
var nameFromLevel = {};
Object.keys(levelFromName).forEach(function (name) {
nameFromLevel[levelFromName[name]] = name;
});
// Dtrace probes. // Dtrace probes.
var dtp = undefined; var dtp = undefined;
@ -1362,6 +1366,8 @@ module.exports.WARN = WARN;
module.exports.ERROR = ERROR; module.exports.ERROR = ERROR;
module.exports.FATAL = FATAL; module.exports.FATAL = FATAL;
module.exports.resolveLevel = resolveLevel; module.exports.resolveLevel = resolveLevel;
module.exports.levelFromName = levelFromName;
module.exports.nameFromLevel = nameFromLevel;
module.exports.VERSION = VERSION; module.exports.VERSION = VERSION;
module.exports.LOG_VERSION = LOG_VERSION; module.exports.LOG_VERSION = LOG_VERSION;