2019-03-08 16:06:24 +00:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const bunyan = require('bunyan')
|
|
|
|
const chai = require('chai')
|
|
|
|
const path = require('path')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const sinonChai = require('sinon-chai')
|
2019-03-06 20:08:38 +00:00
|
|
|
|
2019-03-08 16:06:24 +00:00
|
|
|
chai.use(sinonChai)
|
|
|
|
chai.should()
|
2019-10-23 20:25:48 +00:00
|
|
|
const expect = chai.expect
|
2019-03-06 20:08:38 +00:00
|
|
|
|
2019-03-08 16:06:24 +00:00
|
|
|
const modulePath = path.join(__dirname, '../../logging-manager.js')
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('LoggingManager', function () {
|
|
|
|
beforeEach(function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.start = Date.now()
|
|
|
|
this.clock = sinon.useFakeTimers(this.start)
|
|
|
|
this.captureException = sinon.stub()
|
2019-10-23 20:25:48 +00:00
|
|
|
this.bunyanLogger = {
|
2019-10-24 20:41:46 +00:00
|
|
|
addStream: sinon.stub(),
|
2019-03-08 14:09:44 +00:00
|
|
|
debug: sinon.stub(),
|
|
|
|
error: sinon.stub(),
|
|
|
|
fatal: sinon.stub(),
|
|
|
|
info: sinon.stub(),
|
|
|
|
level: sinon.stub(),
|
|
|
|
warn: sinon.stub()
|
2019-03-08 16:06:24 +00:00
|
|
|
}
|
2019-10-23 20:25:48 +00:00
|
|
|
this.ravenClient = {
|
2019-03-08 14:09:44 +00:00
|
|
|
captureException: this.captureException,
|
|
|
|
once: sinon.stub().yields()
|
2019-03-08 16:06:24 +00:00
|
|
|
}
|
2019-10-23 20:25:48 +00:00
|
|
|
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)
|
|
|
|
}
|
2020-05-15 11:31:28 +00:00
|
|
|
this.Fs = {
|
|
|
|
readFile: sinon.stub()
|
|
|
|
}
|
2019-10-24 20:41:46 +00:00
|
|
|
this.stackdriverStreamConfig = { stream: 'stackdriver' }
|
|
|
|
this.stackdriverClient = {
|
|
|
|
stream: sinon.stub().returns(this.stackdriverStreamConfig)
|
|
|
|
}
|
|
|
|
this.GCPLogging = {
|
|
|
|
LoggingBunyan: sinon.stub().returns(this.stackdriverClient)
|
|
|
|
}
|
2019-03-08 14:09:44 +00:00
|
|
|
this.LoggingManager = SandboxedModule.require(modulePath, {
|
2019-07-11 10:11:09 +00:00
|
|
|
globals: { console, process },
|
2019-03-08 14:09:44 +00:00
|
|
|
requires: {
|
2019-10-23 20:25:48 +00:00
|
|
|
bunyan: this.Bunyan,
|
|
|
|
raven: this.Raven,
|
2020-05-15 11:31:28 +00:00
|
|
|
fs: this.Fs,
|
2019-10-24 20:41:46 +00:00
|
|
|
'@google-cloud/logging-bunyan': this.GCPLogging
|
2019-03-08 14:09:44 +00:00
|
|
|
}
|
2019-03-08 16:06:24 +00:00
|
|
|
})
|
|
|
|
this.loggerName = 'test'
|
|
|
|
this.logger = this.LoggingManager.initialize(this.loggerName)
|
|
|
|
this.logger.initializeErrorReporting('test_dsn')
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
afterEach(function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.clock.restore()
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('initialize', function () {
|
|
|
|
beforeEach(function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.checkLogLevelStub = sinon.stub(this.LoggingManager, 'checkLogLevel')
|
|
|
|
this.Bunyan.createLogger.reset()
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
afterEach(function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.checkLogLevelStub.restore()
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('not in production', function () {
|
|
|
|
beforeEach(function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger = this.LoggingManager.initialize(this.loggerName)
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should default to log level debug', function () {
|
2019-03-08 14:09:44 +00:00
|
|
|
this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
|
2019-03-08 16:06:24 +00:00
|
|
|
'debug'
|
|
|
|
)
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should not run checkLogLevel', function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.checkLogLevelStub.should.not.have.been.called
|
|
|
|
})
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('in production', function () {
|
|
|
|
beforeEach(function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
process.env.NODE_ENV = 'production'
|
|
|
|
this.logger = this.LoggingManager.initialize(this.loggerName)
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2019-03-08 16:06:24 +00:00
|
|
|
afterEach(() => delete process.env.NODE_ENV)
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should default to log level warn', function () {
|
2019-03-08 14:09:44 +00:00
|
|
|
this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
|
2019-03-08 16:06:24 +00:00
|
|
|
'warn'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should run checkLogLevel', function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.checkLogLevelStub.should.have.been.calledOnce
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('after 1 minute', () =>
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should run checkLogLevel again', function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.clock.tick(61 * 1000)
|
|
|
|
this.checkLogLevelStub.should.have.been.calledTwice
|
|
|
|
}))
|
|
|
|
|
|
|
|
describe('after 2 minutes', () =>
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should run checkLogLevel again', function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.clock.tick(121 * 1000)
|
|
|
|
this.checkLogLevelStub.should.have.been.calledThrice
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('when LOG_LEVEL set in env', function () {
|
|
|
|
beforeEach(function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
process.env.LOG_LEVEL = 'trace'
|
|
|
|
this.LoggingManager.initialize()
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2019-03-08 16:06:24 +00:00
|
|
|
afterEach(() => delete process.env.LOG_LEVEL)
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should use custom log level', function () {
|
2019-03-08 14:09:44 +00:00
|
|
|
this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
|
2019-03-08 16:06:24 +00:00
|
|
|
'trace'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('bunyan logging', function () {
|
|
|
|
beforeEach(function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logArgs = [{ foo: 'bar' }, 'foo', 'bar']
|
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should log debug', function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.debug(this.logArgs)
|
2019-10-23 20:25:48 +00:00
|
|
|
this.bunyanLogger.debug.should.have.been.calledWith(this.logArgs)
|
2019-03-08 16:06:24 +00:00
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should log error', function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.error(this.logArgs)
|
2019-10-23 20:25:48 +00:00
|
|
|
this.bunyanLogger.error.should.have.been.calledWith(this.logArgs)
|
2019-03-08 16:06:24 +00:00
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should log fatal', function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.fatal(this.logArgs)
|
2019-10-23 20:25:48 +00:00
|
|
|
this.bunyanLogger.fatal.should.have.been.calledWith(this.logArgs)
|
2019-03-08 16:06:24 +00:00
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should log info', function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.info(this.logArgs)
|
2019-10-23 20:25:48 +00:00
|
|
|
this.bunyanLogger.info.should.have.been.calledWith(this.logArgs)
|
2019-03-08 16:06:24 +00:00
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should log warn', function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.warn(this.logArgs)
|
2019-10-23 20:25:48 +00:00
|
|
|
this.bunyanLogger.warn.should.have.been.calledWith(this.logArgs)
|
2019-03-08 16:06:24 +00:00
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should log err', function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.err(this.logArgs)
|
2019-10-23 20:25:48 +00:00
|
|
|
this.bunyanLogger.error.should.have.been.calledWith(this.logArgs)
|
2019-03-08 16:06:24 +00:00
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should log log', function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.log(this.logArgs)
|
2019-10-23 20:25:48 +00:00
|
|
|
this.bunyanLogger.info.should.have.been.calledWith(this.logArgs)
|
2019-03-08 16:06:24 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('logger.error', function () {
|
|
|
|
it('should report a single error to sentry', function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.captureException.called.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should report the same error to sentry only once', function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
const error1 = new Error('this is the error')
|
|
|
|
this.logger.error({ foo: error1 }, 'first message')
|
|
|
|
this.logger.error({ bar: error1 }, 'second message')
|
|
|
|
this.captureException.callCount.should.equal(1)
|
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should report two different errors to sentry individually', function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
const error1 = new Error('this is the error')
|
|
|
|
const error2 = new Error('this is the error')
|
|
|
|
this.logger.error({ foo: error1 }, 'first message')
|
|
|
|
this.logger.error({ bar: error2 }, 'second message')
|
|
|
|
this.captureException.callCount.should.equal(2)
|
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should remove the path from fs errors', function () {
|
2019-03-08 14:09:44 +00:00
|
|
|
const fsError = new Error(
|
|
|
|
"Error: ENOENT: no such file or directory, stat '/tmp/3279b8d0-da10-11e8-8255-efd98985942b'"
|
2019-03-08 16:06:24 +00:00
|
|
|
)
|
|
|
|
fsError.path = '/tmp/3279b8d0-da10-11e8-8255-efd98985942b'
|
|
|
|
this.logger.error({ err: fsError }, 'message')
|
2019-03-08 14:09:44 +00:00
|
|
|
this.captureException
|
|
|
|
.calledWith(
|
|
|
|
sinon.match.has(
|
2019-03-08 16:06:24 +00:00
|
|
|
'message',
|
|
|
|
'Error: ENOENT: no such file or directory, stat'
|
2019-03-08 14:09:44 +00:00
|
|
|
)
|
|
|
|
)
|
2019-03-08 16:06:24 +00:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('for multiple errors should only report a maximum of 5 errors to sentry', function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.captureException.callCount.should.equal(5)
|
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('for multiple errors with a minute delay should report 10 errors to sentry', function () {
|
2019-03-08 14:09:44 +00:00
|
|
|
// the first five errors should be reported to sentry
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
2019-03-08 14:09:44 +00:00
|
|
|
// the following errors should not be reported
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
2019-03-08 14:09:44 +00:00
|
|
|
// allow a minute to pass
|
2019-03-08 16:06:24 +00:00
|
|
|
this.clock.tick(this.start + 61 * 1000)
|
2019-03-08 14:09:44 +00:00
|
|
|
// after a minute the next five errors should be reported to sentry
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
2019-03-08 14:09:44 +00:00
|
|
|
// the following errors should not be reported to sentry
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.logger.error({ foo: 'bar' }, 'message')
|
|
|
|
this.captureException.callCount.should.equal(10)
|
|
|
|
})
|
2020-03-23 09:48:55 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('reportedToSentry', function () {
|
|
|
|
it('should mark the error as reported to sentry', function () {
|
2020-03-23 09:48:55 +00:00
|
|
|
const err = new Error()
|
|
|
|
this.logger.error({ err }, 'message')
|
|
|
|
expect(this.captureException.called).to.equal(true)
|
|
|
|
expect(err.reportedToSentry).to.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should mark two errors as reported to sentry', function () {
|
2020-03-23 09:48:55 +00:00
|
|
|
const err1 = new Error()
|
|
|
|
const err2 = new Error()
|
|
|
|
this.logger.error({ err: err1, err2 }, 'message')
|
|
|
|
expect(this.captureException.called).to.equal(true)
|
|
|
|
expect(err1.reportedToSentry).to.equal(true)
|
|
|
|
expect(err2.reportedToSentry).to.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should not mark arbitrary objects as reported to sentry', function () {
|
2020-03-23 09:48:55 +00:00
|
|
|
const err = new Error()
|
|
|
|
const ctx = { foo: 'bar' }
|
|
|
|
this.logger.error({ err, ctx }, 'message')
|
|
|
|
expect(this.captureException.called).to.equal(true)
|
|
|
|
expect(ctx.reportedToSentry).to.equal(undefined)
|
|
|
|
})
|
|
|
|
})
|
2019-03-08 16:06:24 +00:00
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('checkLogLevel', function () {
|
|
|
|
it('should request log level override from the config map', function () {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.checkLogLevel()
|
2020-06-30 12:56:13 +00:00
|
|
|
this.Fs.readFile.should.have.been.calledWithMatch(
|
|
|
|
'/logging/tracingEndTime'
|
|
|
|
)
|
2019-03-08 16:06:24 +00:00
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('when read errors', function () {
|
|
|
|
beforeEach(function () {
|
2020-05-15 11:31:28 +00:00
|
|
|
this.Fs.readFile.yields(new Error('error'))
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.checkLogLevel()
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should only set default level', function () {
|
2019-10-23 20:25:48 +00:00
|
|
|
this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
|
2019-03-08 16:06:24 +00:00
|
|
|
'debug'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('when the file is empty', function () {
|
|
|
|
beforeEach(function () {
|
2020-05-15 11:31:28 +00:00
|
|
|
this.Fs.readFile.yields(null, '')
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.checkLogLevel()
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should only set default level', function () {
|
2019-10-23 20:25:48 +00:00
|
|
|
this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
|
2019-03-08 16:06:24 +00:00
|
|
|
'debug'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('when time value returned that is less than current time', function () {
|
|
|
|
beforeEach(function () {
|
2020-05-15 11:31:28 +00:00
|
|
|
this.Fs.readFile.yields(null, '1')
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.checkLogLevel()
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should only set default level', function () {
|
2019-10-23 20:25:48 +00:00
|
|
|
this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
|
2019-03-08 16:06:24 +00:00
|
|
|
'debug'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('when time value returned that is more than current time', function () {
|
|
|
|
describe('when level is already set', function () {
|
|
|
|
beforeEach(function () {
|
2019-10-23 20:25:48 +00:00
|
|
|
this.bunyanLogger.level.returns(10)
|
2020-05-15 11:31:28 +00:00
|
|
|
this.Fs.readFile.yields(null, (this.start + 1000).toString())
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.checkLogLevel()
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should set trace level', function () {
|
2019-10-23 20:25:48 +00:00
|
|
|
this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
|
2019-03-08 16:06:24 +00:00
|
|
|
'trace'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('when level is not already set', function () {
|
|
|
|
beforeEach(function () {
|
2019-10-23 20:25:48 +00:00
|
|
|
this.bunyanLogger.level.returns(20)
|
2020-05-15 11:31:28 +00:00
|
|
|
this.Fs.readFile.yields(null, (this.start + 1000).toString())
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.checkLogLevel()
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should set trace level', function () {
|
2019-10-23 20:25:48 +00:00
|
|
|
this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
|
2019-03-08 16:06:24 +00:00
|
|
|
'trace'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('ringbuffer', function () {
|
|
|
|
beforeEach(function () {
|
2019-03-08 14:09:44 +00:00
|
|
|
this.logBufferMock = [
|
2019-10-23 20:25:48 +00:00
|
|
|
{ msg: 'log 1' },
|
|
|
|
{ msg: 'log 2' },
|
|
|
|
{ level: 50, msg: 'error' }
|
2019-03-08 16:06:24 +00:00
|
|
|
]
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('when ring buffer size is positive', function () {
|
|
|
|
beforeEach(function () {
|
2020-07-02 09:51:37 +00:00
|
|
|
process.env.LOG_RING_BUFFER_SIZE = '20'
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger = this.LoggingManager.initialize(this.loggerName)
|
|
|
|
this.logger.ringBuffer.records = this.logBufferMock
|
|
|
|
this.logger.error({}, 'error')
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
afterEach(function () {
|
2020-07-02 09:51:37 +00:00
|
|
|
process.env.LOG_RING_BUFFER_SIZE = undefined
|
2019-03-08 16:06:24 +00:00
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should include buffered logs in error log and filter out error logs in buffer', function () {
|
2019-10-23 20:25:48 +00:00
|
|
|
this.bunyanLogger.error.lastCall.args[0].logBuffer.should.deep.equal([
|
|
|
|
{ msg: 'log 1' },
|
|
|
|
{ msg: 'log 2' }
|
2019-03-12 12:23:37 +00:00
|
|
|
])
|
2019-03-08 16:06:24 +00:00
|
|
|
})
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('when ring buffer size is zero', function () {
|
|
|
|
beforeEach(function () {
|
2020-07-02 09:51:37 +00:00
|
|
|
process.env.LOG_RING_BUFFER_SIZE = '0'
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger = this.LoggingManager.initialize(this.loggerName)
|
|
|
|
this.logger.error({}, 'error')
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
afterEach(function () {
|
2020-07-02 09:51:37 +00:00
|
|
|
process.env.LOG_RING_BUFFER_SIZE = undefined
|
2019-03-11 14:27:35 +00:00
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('should not include buffered logs in error log', function () {
|
2019-10-23 20:25:48 +00:00
|
|
|
expect(this.bunyanLogger.error.lastCall.args[0].logBuffer).be.undefined
|
2019-03-08 16:06:24 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-10-24 20:41:46 +00:00
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('stackdriver logging', function () {
|
|
|
|
describe('when STACKDRIVER_LOGGING is unset', function () {
|
|
|
|
beforeEach(function () {
|
2020-07-02 09:51:37 +00:00
|
|
|
process.env.STACKDRIVER_LOGGING = undefined
|
2019-10-24 20:41:46 +00:00
|
|
|
this.LoggingManager.initialize(this.loggerName)
|
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('is disabled', function () {
|
2019-10-24 20:41:46 +00:00
|
|
|
expect(this.bunyanLogger.addStream).not.to.have.been.calledWith(
|
|
|
|
this.stackdriverStreamConfig
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
describe('when STACKDRIVER_LOGGING is true', function () {
|
|
|
|
beforeEach(function () {
|
2020-07-02 09:51:37 +00:00
|
|
|
process.env.STACKDRIVER_LOGGING = 'true'
|
2019-10-24 20:41:46 +00:00
|
|
|
this.LoggingManager.initialize(this.loggerName)
|
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('is enabled', function () {
|
2019-10-24 20:41:46 +00:00
|
|
|
expect(this.bunyanLogger.addStream).to.have.been.calledWith(
|
|
|
|
this.stackdriverStreamConfig
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-07-02 10:01:23 +00:00
|
|
|
it('is configured properly', function () {
|
2019-10-24 20:41:46 +00:00
|
|
|
expect(this.GCPLogging.LoggingBunyan).to.have.been.calledWith({
|
|
|
|
logName: this.loggerName,
|
|
|
|
serviceContext: { service: this.loggerName }
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-03-08 16:06:24 +00:00
|
|
|
})
|