Last active
May 10, 2016 00:54
-
-
Save gillesdemey/71d9d02108e469320deaa1251c5f2ecf to your computer and use it in GitHub Desktop.
Pino RingBuffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pino = require('./') | |
var RingBuffer = require('./ringbuffer') | |
var rb = new RingBuffer({ limit: 10 }) | |
var log = pino(rb) | |
setInterval(() => { | |
log.info(new Date()) | |
}, 5) | |
setTimeout(() => { | |
console.log(rb.getRecords()) | |
process.exit(0) | |
}, 100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
var stream = require('stream') | |
var util = require('util') | |
function RingBuffer (options) { | |
var self = this | |
self.limit = options && options.limit ? options.limit : 100 | |
self.records = [] | |
stream.Writable.call(this) | |
} | |
util.inherits(RingBuffer, stream.Writable) | |
RingBuffer.prototype.write = function (record) { | |
if (this.records.length >= this.limit) this.records.pop() | |
this.records.push(record) | |
} | |
RingBuffer.prototype.getRecords = function () { | |
return this.records.map((record) => record.replace(/\n$/, '')) | |
} | |
module.exports = RingBuffer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yah cool then we'll add to readme, one other thing - would encourage exporting a function that returns a new instance of RingBuffer rather than exporting the constructor directly. This avoids issues that arise from forgetting new, naming convention stuff, fp paradigm friendly, generally makes for quicker api grocking imo