[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)
- [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
`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:
```javascript
var bunyan = require('./lib/bunyan');
function MyRawStream() {}
MyRawStream.prototype.write = function (rec) {
var nameFromLevel = {
TRACE: 'TRACE'
DEBUG: 'DEBUG',
INFO: 'INFO',
WARN: 'WARN',
ERROR: 'ERROR',
FATAL: 'FATAL'
};
console.log('[%s] %s: %s', rec.time, nameFromLevel[rec.level], rec.msg);
console.log('[%s] %s: %s',
rec.time.toISOString(),
bunyan.nameFromLevel[rec.level],
rec.msg);
}
var log = bunyan.createLogger({
@ -1015,7 +1012,7 @@ var log = bunyan.createLogger({
level: 'info',
stream: new MyRawStream(),
type: 'raw'
},
}
]
});

View file

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