bunyan-rotating-file-stream
Advanced tools
Comparing version 1.3.0 to 1.3.1
# bunyan Changelog | ||
## 1.3.1 | ||
- Fix bug: If we started rotating files in the middle of a write batch, the remaining logs in the batch would be reversed. | ||
- Start rotating if a log record would breach a threshold rather than rotate after threshold reached. | ||
## 1.3.0 | ||
@@ -4,0 +9,0 @@ |
@@ -34,32 +34,46 @@ // A rotating file stream will just | ||
var forceWrite = false; | ||
function writer(logs, callback) { | ||
for (var i = 0; i < logs.length; i += 1) { | ||
if (stream) { | ||
for (var i = 0; stream && i < logs.length; i += 1) { | ||
var str; | ||
if (typeof (logs[i]) === 'string') { | ||
str = logs[i]; | ||
} else { | ||
str = JSON.stringify(logs[i], bunyan.safeCycles()) + '\n'; | ||
} | ||
var str; | ||
if (typeof (logs[i]) === 'string') { | ||
str = logs[i]; | ||
} else { | ||
str = JSON.stringify(logs[i], bunyan.safeCycles()) + '\n'; | ||
} | ||
var writeBuffer = new Buffer(str, 'utf8'); | ||
var writeBuffer = new Buffer(str, 'utf8'); | ||
streambytesWritten += writeBuffer.byteLength; | ||
try { | ||
stream.write(writeBuffer, function (err) { | ||
if (err) { | ||
base.emit('error', err); | ||
} | ||
}); | ||
} catch (err) { | ||
if (forceWrite) { | ||
// This log entry gets written regardless - don't emit | ||
forceWrite = false; | ||
} else { | ||
base.emit('data', { bytesWritten: streambytesWritten }); | ||
} | ||
if (!stream) { | ||
// This log entry caused a rotation | ||
// We can't write it, force it to be written | ||
// Next time round | ||
forceWrite = true; | ||
break; | ||
} | ||
try { | ||
stream.write(writeBuffer, function (err) { | ||
if (err) { | ||
base.emit('error', err); | ||
} | ||
streambytesWritten += writeBuffer.byteLength; | ||
base.emit('data', { bytesWritten: streambytesWritten }); | ||
} else { | ||
// We suddenly don't have a stream, save this log for later | ||
writeQueue.unshift(logs[i]); | ||
}); | ||
} catch (err) { | ||
base.emit('error', err); | ||
} | ||
} | ||
// If we didn't get all the way through the array, unshift the remaining | ||
// records back onto our queue in reverse order | ||
for (var rollback = logs.length -1; rollback >= i; rollback -= 1) { | ||
writeQueue.unshift(logs[rollback]); | ||
} | ||
@@ -89,2 +103,3 @@ | ||
rotator.on('closefile', function () { | ||
writeQueue.pause(); | ||
stream = null; | ||
@@ -97,2 +112,3 @@ }); | ||
base.emit('newfile', { path: newfile.path }); | ||
writeQueue.resume(); | ||
}); | ||
@@ -105,12 +121,11 @@ | ||
} | ||
writeQueue.resume(); | ||
}); | ||
} | ||
var rotating = false; | ||
function rotate() { | ||
if (writeQueue.paused()) { | ||
if (rotating) { | ||
return; | ||
} else { | ||
writeQueue.pause(); | ||
rotating = true; | ||
} | ||
@@ -123,3 +138,3 @@ | ||
writeQueue.resume(); | ||
rotating = false; | ||
}); | ||
@@ -126,0 +141,0 @@ } |
@@ -15,15 +15,20 @@ 'use strict'; | ||
var firstFile = true; | ||
function reset(data) { | ||
// Since we've got a new file: | ||
// check it's current size before we start writing | ||
try { | ||
var stats = fs.statSync(data.path); | ||
initialFileSize = stats.size; | ||
} catch (e) { | ||
if (e.code !== 'ENOENT') { | ||
throw e; | ||
} else { | ||
// File doesn't exist - it'll be made in time. | ||
initialFileSize = 0; | ||
} | ||
initialFileSize = 0; | ||
if (firstFile) { | ||
firstFile = false; | ||
// The first file may be an existing file | ||
// so we need to know how big it is so | ||
// that we can rollover at the right time | ||
fs.stat(data.path, function (err, stats) { | ||
if (err && err.code !== 'ENOENT') { | ||
base.emit('error', err); | ||
} else if (stats) { | ||
initialFileSize = stats.size; | ||
} | ||
}); | ||
} | ||
@@ -30,0 +35,0 @@ } |
{ | ||
"name": "bunyan-rotating-file-stream", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "a rotating file stream for the bunyan logging system", | ||
@@ -5,0 +5,0 @@ "author": "Jim Tupper <npm@tupper.org.uk> (http://github.com/rcomian)", |
@@ -23,2 +23,4 @@ Bunyan is **a simple and fast JSON logging library** for node.js services: | ||
[![Join the chat at https://gitter.im/Rcomian/bunyan-rotating-file-stream](https://badges.gitter.im/Rcomian/bunyan-rotating-file-stream.svg)](https://gitter.im/Rcomian/bunyan-rotating-file-stream?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
## 1.3 | ||
@@ -25,0 +27,0 @@ |
25
test.js
@@ -100,2 +100,22 @@ var bunyan = require('bunyan'); | ||
function toosmallthresholdstillgetswrites(next) { | ||
var name = 'testlogs/' + 'toosmallthresholdstillgetswrites'; | ||
async.series([ | ||
function (next) { rmdir(name, ignoreMissing(next)); }, | ||
function (next) { fx.mkdir(name, next); }, | ||
function (next) { runTest (name, { | ||
stream: { path: name + '/test.log', threshold: 1, totalFiles: 502 }, | ||
batch: { iterations: 500 } | ||
}, next); }, | ||
function (next) { | ||
var files = fs.readdirSync(name); | ||
assert.equal(501, files.length); | ||
console.log(name, 'passed'); | ||
next(); | ||
}, | ||
function (next) { rmdir(name, ignoreMissing(next)); } | ||
], next); | ||
} | ||
function timerotation(next) { | ||
@@ -154,3 +174,3 @@ var name = 'testlogs/' + 'timerotation'; | ||
var files = fs.readdirSync(name); | ||
assert.equal(10, files.length); | ||
assert.equal(11, files.length); | ||
console.log(name, 'passed'); | ||
@@ -356,3 +376,4 @@ next(); | ||
checksetlongtimeoutclearnormalperiods, | ||
multiplerotatorsonsamefile | ||
multiplerotatorsonsamefile, | ||
toosmallthresholdstillgetswrites | ||
], function (err) { | ||
@@ -359,0 +380,0 @@ if (err) console.log(err); |
57490069
37
1293
218