diff --git a/CHANGES.md b/CHANGES.md index fe05769..66c034e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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.: diff --git a/README.md b/README.md index 0b7796b..292baea 100644 --- a/README.md +++ b/README.md @@ -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' - }, + } ] }); diff --git a/lib/bunyan.js b/lib/bunyan.js index 2c997ae..3f0bf5c 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -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;