From 4744d63dd397d57495ef2fb08c7e59dd7eae247f Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Thu, 21 Apr 2016 00:49:18 -0700 Subject: [PATCH] Add example for #394 --- examples/rot-specific-levels.js | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 examples/rot-specific-levels.js diff --git a/examples/rot-specific-levels.js b/examples/rot-specific-levels.js new file mode 100644 index 0000000..8ef7ef4 --- /dev/null +++ b/examples/rot-specific-levels.js @@ -0,0 +1,57 @@ +var bunyan = require('./'), + safeCycles = bunyan.safeCycles; +var util = require('util'); + + +function SpecificLevelStream(levels, stream) { + var self = this; + this.levels = {}; + levels.forEach(function (lvl) { + self.levels[bunyan.resolveLevel(lvl)] = true; + }); + this.stream = stream; +} +SpecificLevelStream.prototype.write = function (rec) { + if (this.levels[rec.level] !== undefined) { + var str = JSON.stringify(rec, safeCycles()) + '\n'; + this.stream.write(str); + } +} + + +var log = bunyan.createLogger({ + name: 'rot-specific-levels', + streams: [ + { + type: 'raw', + level: 'debug', + stream: new SpecificLevelStream( + ['debug'], + new bunyan.RotatingFileStream({ + path: './rot-specific-levels.debug.log', + period: '3000ms', + count: 10 + }) + ) + }, + { + type: 'raw', + level: 'info', + stream: new SpecificLevelStream( + ['info'], + new bunyan.RotatingFileStream({ + path: './rot-specific-levels.info.log', + period: '3000ms', + count: 10 + }) + ) + } + ] +}); + + +setInterval(function () { + log.trace('hi on trace') // goes nowhere + log.debug('hi on debug') // goes to rot-specific-levels.debug.log.* + log.info('hi on info') // goes to rot-specific-levels.info.log.* +}, 1000);