|
|
@@ -7,6 +7,7 @@ const sinonChai = require('sinon-chai')
|
|
|
|
|
|
chai.use(sinonChai)
|
|
|
chai.should()
|
|
|
+const expect = chai.expect
|
|
|
|
|
|
const modulePath = path.join(__dirname, '../../logging-manager.js')
|
|
|
|
|
|
@@ -15,7 +16,7 @@ describe('LoggingManager', function() {
|
|
|
this.start = Date.now()
|
|
|
this.clock = sinon.useFakeTimers(this.start)
|
|
|
this.captureException = sinon.stub()
|
|
|
- this.mockBunyanLogger = {
|
|
|
+ this.bunyanLogger = {
|
|
|
debug: sinon.stub(),
|
|
|
error: sinon.stub(),
|
|
|
fatal: sinon.stub(),
|
|
|
@@ -23,25 +24,28 @@ describe('LoggingManager', function() {
|
|
|
level: sinon.stub(),
|
|
|
warn: sinon.stub()
|
|
|
}
|
|
|
- this.mockRavenClient = {
|
|
|
+ this.ravenClient = {
|
|
|
captureException: this.captureException,
|
|
|
once: sinon.stub().yields()
|
|
|
}
|
|
|
+ this.Bunyan = {
|
|
|
+ createLogger: sinon.stub().returns(this.bunyanLogger),
|
|
|
+ RingBuffer: bunyan.RingBuffer,
|
|
|
+ stdSerializers: {
|
|
|
+ req: sinon.stub(),
|
|
|
+ res: sinon.stub()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ this.Raven = {
|
|
|
+ Client: sinon.stub().returns(this.ravenClient)
|
|
|
+ }
|
|
|
+ this.Request = sinon.stub()
|
|
|
this.LoggingManager = SandboxedModule.require(modulePath, {
|
|
|
globals: { console, process },
|
|
|
requires: {
|
|
|
- bunyan: (this.Bunyan = {
|
|
|
- createLogger: sinon.stub().returns(this.mockBunyanLogger),
|
|
|
- RingBuffer: bunyan.RingBuffer,
|
|
|
- stdSerializers: {
|
|
|
- req: sinon.stub(),
|
|
|
- res: sinon.stub()
|
|
|
- }
|
|
|
- }),
|
|
|
- raven: (this.Raven = {
|
|
|
- Client: sinon.stub().returns(this.mockRavenClient)
|
|
|
- }),
|
|
|
- request: (this.Request = sinon.stub())
|
|
|
+ bunyan: this.Bunyan,
|
|
|
+ raven: this.Raven,
|
|
|
+ request: this.Request
|
|
|
}
|
|
|
})
|
|
|
this.loggerName = 'test'
|
|
|
@@ -133,37 +137,37 @@ describe('LoggingManager', function() {
|
|
|
|
|
|
it('should log debug', function() {
|
|
|
this.logger.debug(this.logArgs)
|
|
|
- this.mockBunyanLogger.debug.should.have.been.calledWith(this.logArgs)
|
|
|
+ this.bunyanLogger.debug.should.have.been.calledWith(this.logArgs)
|
|
|
})
|
|
|
|
|
|
it('should log error', function() {
|
|
|
this.logger.error(this.logArgs)
|
|
|
- this.mockBunyanLogger.error.should.have.been.calledWith(this.logArgs)
|
|
|
+ this.bunyanLogger.error.should.have.been.calledWith(this.logArgs)
|
|
|
})
|
|
|
|
|
|
it('should log fatal', function() {
|
|
|
this.logger.fatal(this.logArgs)
|
|
|
- this.mockBunyanLogger.fatal.should.have.been.calledWith(this.logArgs)
|
|
|
+ this.bunyanLogger.fatal.should.have.been.calledWith(this.logArgs)
|
|
|
})
|
|
|
|
|
|
it('should log info', function() {
|
|
|
this.logger.info(this.logArgs)
|
|
|
- this.mockBunyanLogger.info.should.have.been.calledWith(this.logArgs)
|
|
|
+ this.bunyanLogger.info.should.have.been.calledWith(this.logArgs)
|
|
|
})
|
|
|
|
|
|
it('should log warn', function() {
|
|
|
this.logger.warn(this.logArgs)
|
|
|
- this.mockBunyanLogger.warn.should.have.been.calledWith(this.logArgs)
|
|
|
+ this.bunyanLogger.warn.should.have.been.calledWith(this.logArgs)
|
|
|
})
|
|
|
|
|
|
it('should log err', function() {
|
|
|
this.logger.err(this.logArgs)
|
|
|
- this.mockBunyanLogger.error.should.have.been.calledWith(this.logArgs)
|
|
|
+ this.bunyanLogger.error.should.have.been.calledWith(this.logArgs)
|
|
|
})
|
|
|
|
|
|
it('should log log', function() {
|
|
|
this.logger.log(this.logArgs)
|
|
|
- this.mockBunyanLogger.info.should.have.been.calledWith(this.logArgs)
|
|
|
+ this.bunyanLogger.info.should.have.been.calledWith(this.logArgs)
|
|
|
})
|
|
|
})
|
|
|
|
|
|
@@ -253,9 +257,7 @@ describe('LoggingManager', function() {
|
|
|
headers: {
|
|
|
'Metadata-Flavor': 'Google'
|
|
|
},
|
|
|
- uri: `http://metadata.google.internal/computeMetadata/v1/project/attributes/${
|
|
|
- this.loggerName
|
|
|
- }-setLogLevelEndTime`
|
|
|
+ uri: `http://metadata.google.internal/computeMetadata/v1/project/attributes/${this.loggerName}-setLogLevelEndTime`
|
|
|
}
|
|
|
this.Request.should.have.been.calledWithMatch(options)
|
|
|
})
|
|
|
@@ -267,7 +269,7 @@ describe('LoggingManager', function() {
|
|
|
})
|
|
|
|
|
|
it('should only set default level', function() {
|
|
|
- this.mockBunyanLogger.level.should.have.been.calledOnce.and.calledWith(
|
|
|
+ this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
|
|
|
'debug'
|
|
|
)
|
|
|
})
|
|
|
@@ -280,7 +282,7 @@ describe('LoggingManager', function() {
|
|
|
})
|
|
|
|
|
|
it('should only set default level', function() {
|
|
|
- this.mockBunyanLogger.level.should.have.been.calledOnce.and.calledWith(
|
|
|
+ this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
|
|
|
'debug'
|
|
|
)
|
|
|
})
|
|
|
@@ -293,22 +295,22 @@ describe('LoggingManager', function() {
|
|
|
})
|
|
|
|
|
|
it('should only set default level', function() {
|
|
|
- this.mockBunyanLogger.level.should.have.been.calledOnce.and.calledWith(
|
|
|
+ this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
|
|
|
'debug'
|
|
|
)
|
|
|
})
|
|
|
})
|
|
|
|
|
|
- describe('when time value returned that is less than current time', function() {
|
|
|
+ describe('when time value returned that is more than current time', function() {
|
|
|
describe('when level is already set', function() {
|
|
|
beforeEach(function() {
|
|
|
- this.mockBunyanLogger.level.returns(10)
|
|
|
+ this.bunyanLogger.level.returns(10)
|
|
|
this.Request.yields(null, { statusCode: 200 }, this.start + 1000)
|
|
|
this.logger.checkLogLevel()
|
|
|
})
|
|
|
|
|
|
it('should set trace level', function() {
|
|
|
- this.mockBunyanLogger.level.should.have.been.calledOnce.and.calledWith(
|
|
|
+ this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
|
|
|
'trace'
|
|
|
)
|
|
|
})
|
|
|
@@ -316,13 +318,13 @@ describe('LoggingManager', function() {
|
|
|
|
|
|
describe('when level is not already set', function() {
|
|
|
beforeEach(function() {
|
|
|
- this.mockBunyanLogger.level.returns(20)
|
|
|
+ this.bunyanLogger.level.returns(20)
|
|
|
this.Request.yields(null, { statusCode: 200 }, this.start + 1000)
|
|
|
this.logger.checkLogLevel()
|
|
|
})
|
|
|
|
|
|
it('should set trace level', function() {
|
|
|
- this.mockBunyanLogger.level.should.have.been.calledOnce.and.calledWith(
|
|
|
+ this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
|
|
|
'trace'
|
|
|
)
|
|
|
})
|
|
|
@@ -333,16 +335,9 @@ describe('LoggingManager', function() {
|
|
|
describe('ringbuffer', function() {
|
|
|
beforeEach(function() {
|
|
|
this.logBufferMock = [
|
|
|
- {
|
|
|
- msg: 'log 1'
|
|
|
- },
|
|
|
- {
|
|
|
- msg: 'log 2'
|
|
|
- },
|
|
|
- {
|
|
|
- level: 50,
|
|
|
- msg: 'error'
|
|
|
- }
|
|
|
+ { msg: 'log 1' },
|
|
|
+ { msg: 'log 2' },
|
|
|
+ { level: 50, msg: 'error' }
|
|
|
]
|
|
|
})
|
|
|
|
|
|
@@ -359,13 +354,9 @@ describe('LoggingManager', function() {
|
|
|
})
|
|
|
|
|
|
it('should include buffered logs in error log and filter out error logs in buffer', function() {
|
|
|
- this.mockBunyanLogger.error.lastCall.args[0].logBuffer.should.deep.equal([
|
|
|
- {
|
|
|
- msg: 'log 1'
|
|
|
- },
|
|
|
- {
|
|
|
- msg: 'log 2'
|
|
|
- },
|
|
|
+ this.bunyanLogger.error.lastCall.args[0].logBuffer.should.deep.equal([
|
|
|
+ { msg: 'log 1' },
|
|
|
+ { msg: 'log 2' }
|
|
|
])
|
|
|
})
|
|
|
})
|
|
|
@@ -382,8 +373,7 @@ describe('LoggingManager', function() {
|
|
|
})
|
|
|
|
|
|
it('should not include buffered logs in error log', function() {
|
|
|
- chai.expect(this.mockBunyanLogger.error.lastCall.args[0].logBuffer).be
|
|
|
- .undefined
|
|
|
+ expect(this.bunyanLogger.error.lastCall.args[0].logBuffer).be.undefined
|
|
|
})
|
|
|
})
|
|
|
})
|