Permalink
Browse files

Add example for #394

  • Loading branch information...
1 parent dfa12b2 commit 4744d63dd397d57495ef2fb08c7e59dd7eae247f @trentm committed Apr 21, 2016
Showing with 57 additions and 0 deletions.
  1. +57 −0 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);

0 comments on commit 4744d63

Please sign in to comment.