hmpo-logger
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -11,2 +11,3 @@ var winston = require('winston'), | ||
options = options || {}; | ||
debug('constructor options', options); | ||
@@ -21,3 +22,4 @@ if (options.maxsize && options.dateRotate) { | ||
if (this.dateRotate) { | ||
this._dateRotateUpdateDay(); | ||
var logfileDate = this._getLogfileDate(); | ||
this._dateRotateUpdateDay(logfileDate); | ||
} | ||
@@ -30,4 +32,16 @@ }; | ||
FileRotate.prototype._dateRotateUpdateDay = function () { | ||
var d = new Date(); | ||
FileRotate.prototype._getLogfileDate = function () { | ||
var currentLogfile = path.join(this.dirname, this._basename); | ||
var logfileDate; | ||
try { | ||
logfileDate = fs.statSync(currentLogfile).mtime; | ||
debug('existing log file date', logfileDate); | ||
} catch (e) { | ||
debug('existing log file stat error', e); | ||
} | ||
return logfileDate; | ||
}; | ||
FileRotate.prototype._dateRotateUpdateDay = function (date) { | ||
var d = date || new Date(); | ||
var year = d.getUTCFullYear(); | ||
@@ -34,0 +48,0 @@ var month = d.getUTCMonth(); |
@@ -161,2 +161,7 @@ var winston = require('winston'), | ||
txt: new TokenFn(function () { | ||
var arg = Array.prototype.join.call(arguments, '.'); | ||
return arg; | ||
}), | ||
res: new TokenFn(function (arg) { | ||
@@ -163,0 +168,0 @@ if (!arg || !this.res || !this.res.getHeader) { return; } |
@@ -124,3 +124,5 @@ var winston = require('winston'), | ||
this._options.app = null; | ||
if (Manager.levels[this._options.appLevel] > Manager.levels[this._options.errorLevel]) { | ||
var appLevel = (Manager.levels[this._options.appLevel] + 1) || 0; | ||
var errorLevel = (Manager.levels[this._options.errorLevel] + 1) || 0; | ||
if (appLevel > errorLevel) { | ||
this._options.errorLevel = this._options.appLevel; | ||
@@ -127,0 +129,0 @@ } |
{ | ||
"name": "hmpo-logger", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Consistent logging for hmpo apps", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -30,3 +30,6 @@ | ||
describe('constructor', function () { | ||
var logfileDate; | ||
beforeEach(function () { | ||
logfileDate = new Date(); | ||
sinon.stub(FileRotateTransport.prototype, '_getLogfileDate').returns(logfileDate); | ||
sinon.stub(FileRotateTransport.prototype, '_dateRotateUpdateDay'); | ||
@@ -37,2 +40,3 @@ sinon.stub(fileLib, 'File'); | ||
afterEach(function () { | ||
FileRotateTransport.prototype._getLogfileDate.restore(); | ||
FileRotateTransport.prototype._dateRotateUpdateDay.restore(); | ||
@@ -54,11 +58,21 @@ fileLib.File.restore(); | ||
it('should call _dateRotateUpdateDay if dateRotate is set', function () { | ||
it('should set dateRotate to be true if specified in options', function () { | ||
var transport = new FileRotateTransport({ dateRotate: true }); | ||
transport.dateRotate.should.equal(true); | ||
FileRotateTransport.prototype._dateRotateUpdateDay.should.have.been.calledOnce; | ||
}); | ||
it('should not call _dateRotateUpdateDay if dateRotate is not set', function () { | ||
it('should call _getLogfileDate if dateRotate is set', function () { | ||
new FileRotateTransport({ dateRotate: true }); | ||
FileRotateTransport.prototype._getLogfileDate.should.have.been.calledOnce; | ||
}); | ||
it('should call _dateRotateUpdateDay with the log file date', function () { | ||
new FileRotateTransport({ dateRotate: true }); | ||
FileRotateTransport.prototype._dateRotateUpdateDay.should.have.been.calledWithExactly(logfileDate); | ||
}); | ||
it('should not call _getLogfileDate or _dateRotateUpdateDay if dateRotate is not set', function () { | ||
var transport = new FileRotateTransport(); | ||
expect(transport.dateRotate).to.not.be.ok; | ||
FileRotateTransport.prototype._getLogfileDate.should.not.have.been.called; | ||
FileRotateTransport.prototype._dateRotateUpdateDay.should.not.have.been.called; | ||
@@ -69,2 +83,29 @@ }); | ||
describe('_getLogfileDate', function () { | ||
var logfileDate; | ||
beforeEach(function () { | ||
logfileDate = new Date(); | ||
sinon.stub(fs, 'statSync').returns({ mtime: logfileDate }); | ||
}); | ||
afterEach(function () { | ||
fs.statSync.restore(); | ||
}); | ||
it('should return the last-modified-time of the current log file', function () { | ||
var transport = new FileRotateTransport({ filename: '/path/test.log' }); | ||
var result = transport._getLogfileDate(); | ||
fs.statSync.should.have.been.calledWithExactly('/path/test.log'); | ||
result.should.equal(logfileDate); | ||
}); | ||
it('should return undefined if the stat throws an error', function () { | ||
var transport = new FileRotateTransport({ filename: '/path/test.log' }); | ||
fs.statSync.throws(new Error); | ||
var result = transport._getLogfileDate(); | ||
expect(result).to.equal(undefined); | ||
}); | ||
}); | ||
describe('_dateRotateUpdateDay', function () { | ||
@@ -90,2 +131,12 @@ var clock; | ||
it('should set the time bounds of the current log file to the start and end of the date given', function () { | ||
var transport = new FileRotateTransport({ filename: 'test' }); | ||
var logfileDate = new Date(2015, 9, 10, 12, 11, 10); | ||
transport._dateRotateUpdateDay(logfileDate); | ||
new Date(transport._dateRotateStartTime).toISOString() | ||
.should.equal('2015-10-10T00:00:00.000Z'); | ||
new Date(transport._dateRotateEndTime).toISOString() | ||
.should.equal('2015-10-11T00:00:00.000Z'); | ||
}); | ||
}); | ||
@@ -92,0 +143,0 @@ |
@@ -75,2 +75,17 @@ | ||
}); | ||
it('should handle plain text meta with the txt token function', function () { | ||
var dest = {}; | ||
var sources = [ | ||
Logger.tokens | ||
]; | ||
logger._addMetaData(dest, { | ||
keyName: 'txt.Plain text with chars. Symbols + / " -' | ||
}, sources); | ||
dest.should.deep.equal({ | ||
keyName: 'Plain text with chars. Symbols + / " -' | ||
}); | ||
}); | ||
}); | ||
@@ -386,2 +401,14 @@ | ||
describe('txt', function () { | ||
it('should return the argument as the value', function () { | ||
Logger.tokens.txt.fn.call(null, 'value') | ||
.should.equal('value'); | ||
}); | ||
it('should join all aruments together with dots', function () { | ||
Logger.tokens.txt.fn.call(null, 'value', ' value 2') | ||
.should.equal('value. value 2'); | ||
}); | ||
}); | ||
describe('res', function () { | ||
@@ -388,0 +415,0 @@ var getHeader = sinon.stub(); |
@@ -136,2 +136,32 @@ | ||
it('should use higher known level if error level is not known', function () { | ||
manager.config({ | ||
app: './testapp.log', | ||
appLevel: 'info', | ||
error: './testapp.log', | ||
errorLevel: 'unknownlevel' | ||
}); | ||
var t = winston.loggers.options.transports; | ||
t.length.should.equal(2); | ||
t[1].name.should.equal('error'); | ||
t[1].level.should.equal('info'); | ||
}); | ||
it('should use higher known level if app level is not known', function () { | ||
manager.config({ | ||
app: './testapp.log', | ||
appLevel: 'unknownlevel', | ||
error: './testapp.log', | ||
errorLevel: 'info' | ||
}); | ||
var t = winston.loggers.options.transports; | ||
t.length.should.equal(2); | ||
t[1].name.should.equal('error'); | ||
t[1].level.should.equal('info'); | ||
}); | ||
it('should use non-logstash logging if JSON is false', function () { | ||
@@ -138,0 +168,0 @@ manager.config({ |
89034
2029