Make prettier

This commit is contained in:
Christopher Hoskin 2020-07-02 11:01:23 +01:00
parent 4c8d29af59
commit fcd4a60657
2 changed files with 80 additions and 80 deletions

View file

@ -5,7 +5,7 @@ const OError = require('@overleaf/o-error')
const GCPLogging = require('@google-cloud/logging-bunyan') const GCPLogging = require('@google-cloud/logging-bunyan')
// bunyan error serializer // bunyan error serializer
const errSerializer = function(err) { const errSerializer = function (err) {
if (!err || !err.stack) { if (!err || !err.stack) {
return err return err
} }
@ -154,7 +154,7 @@ const Logger = (module.exports = {
error(attributes, message, ...args) { error(attributes, message, ...args) {
if (this.ringBuffer !== null && Array.isArray(this.ringBuffer.records)) { if (this.ringBuffer !== null && Array.isArray(this.ringBuffer.records)) {
attributes.logBuffer = this.ringBuffer.records.filter(function(record) { attributes.logBuffer = this.ringBuffer.records.filter(function (record) {
return record.level !== 50 return record.level !== 50
}) })
} }
@ -192,14 +192,14 @@ const Logger = (module.exports = {
fatal(attributes, message, callback) { fatal(attributes, message, callback) {
if (callback == null) { if (callback == null) {
callback = function() {} callback = function () {}
} }
this.logger.fatal(attributes, message) this.logger.fatal(attributes, message)
if (this.raven != null) { if (this.raven != null) {
var cb = function(e) { var cb = function (e) {
// call the callback once after 'logged' or 'error' event // call the callback once after 'logged' or 'error' event
callback() callback()
return (cb = function() {}) return (cb = function () {})
} }
this.captureException(attributes, message, 'fatal') this.captureException(attributes, message, 'fatal')
this.raven.once('logged', cb) this.raven.once('logged', cb)

View file

@ -11,8 +11,8 @@ const expect = chai.expect
const modulePath = path.join(__dirname, '../../logging-manager.js') const modulePath = path.join(__dirname, '../../logging-manager.js')
describe('LoggingManager', function() { describe('LoggingManager', function () {
beforeEach(function() { beforeEach(function () {
this.start = Date.now() this.start = Date.now()
this.clock = sinon.useFakeTimers(this.start) this.clock = sinon.useFakeTimers(this.start)
this.captureException = sinon.stub() this.captureException = sinon.stub()
@ -64,76 +64,76 @@ describe('LoggingManager', function() {
this.logger.initializeErrorReporting('test_dsn') this.logger.initializeErrorReporting('test_dsn')
}) })
afterEach(function() { afterEach(function () {
this.clock.restore() this.clock.restore()
}) })
describe('initialize', function() { describe('initialize', function () {
beforeEach(function() { beforeEach(function () {
this.checkLogLevelStub = sinon.stub(this.LoggingManager, 'checkLogLevel') this.checkLogLevelStub = sinon.stub(this.LoggingManager, 'checkLogLevel')
this.Bunyan.createLogger.reset() this.Bunyan.createLogger.reset()
}) })
afterEach(function() { afterEach(function () {
this.checkLogLevelStub.restore() this.checkLogLevelStub.restore()
}) })
describe('not in production', function() { describe('not in production', function () {
beforeEach(function() { beforeEach(function () {
this.logger = this.LoggingManager.initialize(this.loggerName) this.logger = this.LoggingManager.initialize(this.loggerName)
}) })
it('should default to log level debug', function() { it('should default to log level debug', function () {
this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal( this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
'debug' 'debug'
) )
}) })
it('should not run checkLogLevel', function() { it('should not run checkLogLevel', function () {
this.checkLogLevelStub.should.not.have.been.called this.checkLogLevelStub.should.not.have.been.called
}) })
}) })
describe('in production', function() { describe('in production', function () {
beforeEach(function() { beforeEach(function () {
process.env.NODE_ENV = 'production' process.env.NODE_ENV = 'production'
this.logger = this.LoggingManager.initialize(this.loggerName) this.logger = this.LoggingManager.initialize(this.loggerName)
}) })
afterEach(() => delete process.env.NODE_ENV) afterEach(() => delete process.env.NODE_ENV)
it('should default to log level warn', function() { it('should default to log level warn', function () {
this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal( this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
'warn' 'warn'
) )
}) })
it('should run checkLogLevel', function() { it('should run checkLogLevel', function () {
this.checkLogLevelStub.should.have.been.calledOnce this.checkLogLevelStub.should.have.been.calledOnce
}) })
describe('after 1 minute', () => describe('after 1 minute', () =>
it('should run checkLogLevel again', function() { it('should run checkLogLevel again', function () {
this.clock.tick(61 * 1000) this.clock.tick(61 * 1000)
this.checkLogLevelStub.should.have.been.calledTwice this.checkLogLevelStub.should.have.been.calledTwice
})) }))
describe('after 2 minutes', () => describe('after 2 minutes', () =>
it('should run checkLogLevel again', function() { it('should run checkLogLevel again', function () {
this.clock.tick(121 * 1000) this.clock.tick(121 * 1000)
this.checkLogLevelStub.should.have.been.calledThrice this.checkLogLevelStub.should.have.been.calledThrice
})) }))
}) })
describe('when LOG_LEVEL set in env', function() { describe('when LOG_LEVEL set in env', function () {
beforeEach(function() { beforeEach(function () {
process.env.LOG_LEVEL = 'trace' process.env.LOG_LEVEL = 'trace'
this.LoggingManager.initialize() this.LoggingManager.initialize()
}) })
afterEach(() => delete process.env.LOG_LEVEL) afterEach(() => delete process.env.LOG_LEVEL)
it('should use custom log level', function() { it('should use custom log level', function () {
this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal( this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
'trace' 'trace'
) )
@ -141,61 +141,61 @@ describe('LoggingManager', function() {
}) })
}) })
describe('bunyan logging', function() { describe('bunyan logging', function () {
beforeEach(function() { beforeEach(function () {
this.logArgs = [{ foo: 'bar' }, 'foo', 'bar'] this.logArgs = [{ foo: 'bar' }, 'foo', 'bar']
}) })
it('should log debug', function() { it('should log debug', function () {
this.logger.debug(this.logArgs) this.logger.debug(this.logArgs)
this.bunyanLogger.debug.should.have.been.calledWith(this.logArgs) this.bunyanLogger.debug.should.have.been.calledWith(this.logArgs)
}) })
it('should log error', function() { it('should log error', function () {
this.logger.error(this.logArgs) this.logger.error(this.logArgs)
this.bunyanLogger.error.should.have.been.calledWith(this.logArgs) this.bunyanLogger.error.should.have.been.calledWith(this.logArgs)
}) })
it('should log fatal', function() { it('should log fatal', function () {
this.logger.fatal(this.logArgs) this.logger.fatal(this.logArgs)
this.bunyanLogger.fatal.should.have.been.calledWith(this.logArgs) this.bunyanLogger.fatal.should.have.been.calledWith(this.logArgs)
}) })
it('should log info', function() { it('should log info', function () {
this.logger.info(this.logArgs) this.logger.info(this.logArgs)
this.bunyanLogger.info.should.have.been.calledWith(this.logArgs) this.bunyanLogger.info.should.have.been.calledWith(this.logArgs)
}) })
it('should log warn', function() { it('should log warn', function () {
this.logger.warn(this.logArgs) this.logger.warn(this.logArgs)
this.bunyanLogger.warn.should.have.been.calledWith(this.logArgs) this.bunyanLogger.warn.should.have.been.calledWith(this.logArgs)
}) })
it('should log err', function() { it('should log err', function () {
this.logger.err(this.logArgs) this.logger.err(this.logArgs)
this.bunyanLogger.error.should.have.been.calledWith(this.logArgs) this.bunyanLogger.error.should.have.been.calledWith(this.logArgs)
}) })
it('should log log', function() { it('should log log', function () {
this.logger.log(this.logArgs) this.logger.log(this.logArgs)
this.bunyanLogger.info.should.have.been.calledWith(this.logArgs) this.bunyanLogger.info.should.have.been.calledWith(this.logArgs)
}) })
}) })
describe('logger.error', function() { describe('logger.error', function () {
it('should report a single error to sentry', function() { it('should report a single error to sentry', function () {
this.logger.error({ foo: 'bar' }, 'message') this.logger.error({ foo: 'bar' }, 'message')
this.captureException.called.should.equal(true) this.captureException.called.should.equal(true)
}) })
it('should report the same error to sentry only once', function() { it('should report the same error to sentry only once', function () {
const error1 = new Error('this is the error') const error1 = new Error('this is the error')
this.logger.error({ foo: error1 }, 'first message') this.logger.error({ foo: error1 }, 'first message')
this.logger.error({ bar: error1 }, 'second message') this.logger.error({ bar: error1 }, 'second message')
this.captureException.callCount.should.equal(1) this.captureException.callCount.should.equal(1)
}) })
it('should report two different errors to sentry individually', function() { it('should report two different errors to sentry individually', function () {
const error1 = new Error('this is the error') const error1 = new Error('this is the error')
const error2 = new Error('this is the error') const error2 = new Error('this is the error')
this.logger.error({ foo: error1 }, 'first message') this.logger.error({ foo: error1 }, 'first message')
@ -203,7 +203,7 @@ describe('LoggingManager', function() {
this.captureException.callCount.should.equal(2) this.captureException.callCount.should.equal(2)
}) })
it('should remove the path from fs errors', function() { it('should remove the path from fs errors', function () {
const fsError = new Error( const fsError = new Error(
"Error: ENOENT: no such file or directory, stat '/tmp/3279b8d0-da10-11e8-8255-efd98985942b'" "Error: ENOENT: no such file or directory, stat '/tmp/3279b8d0-da10-11e8-8255-efd98985942b'"
) )
@ -219,7 +219,7 @@ describe('LoggingManager', function() {
.should.equal(true) .should.equal(true)
}) })
it('for multiple errors should only report a maximum of 5 errors to sentry', function() { it('for multiple errors should only report a maximum of 5 errors to sentry', function () {
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')
@ -232,7 +232,7 @@ describe('LoggingManager', function() {
this.captureException.callCount.should.equal(5) this.captureException.callCount.should.equal(5)
}) })
it('for multiple errors with a minute delay should report 10 errors to sentry', function() { it('for multiple errors with a minute delay should report 10 errors to sentry', function () {
// the first five errors should be reported to sentry // the first five errors should be reported to sentry
this.logger.error({ foo: 'bar' }, 'message') this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message') this.logger.error({ foo: 'bar' }, 'message')
@ -260,15 +260,15 @@ describe('LoggingManager', function() {
this.captureException.callCount.should.equal(10) this.captureException.callCount.should.equal(10)
}) })
describe('reportedToSentry', function() { describe('reportedToSentry', function () {
it('should mark the error as reported to sentry', function() { it('should mark the error as reported to sentry', function () {
const err = new Error() const err = new Error()
this.logger.error({ err }, 'message') this.logger.error({ err }, 'message')
expect(this.captureException.called).to.equal(true) expect(this.captureException.called).to.equal(true)
expect(err.reportedToSentry).to.equal(true) expect(err.reportedToSentry).to.equal(true)
}) })
it('should mark two errors as reported to sentry', function() { it('should mark two errors as reported to sentry', function () {
const err1 = new Error() const err1 = new Error()
const err2 = new Error() const err2 = new Error()
this.logger.error({ err: err1, err2 }, 'message') this.logger.error({ err: err1, err2 }, 'message')
@ -277,7 +277,7 @@ describe('LoggingManager', function() {
expect(err2.reportedToSentry).to.equal(true) expect(err2.reportedToSentry).to.equal(true)
}) })
it('should not mark arbitrary objects as reported to sentry', function() { it('should not mark arbitrary objects as reported to sentry', function () {
const err = new Error() const err = new Error()
const ctx = { foo: 'bar' } const ctx = { foo: 'bar' }
this.logger.error({ err, ctx }, 'message') this.logger.error({ err, ctx }, 'message')
@ -287,76 +287,76 @@ describe('LoggingManager', function() {
}) })
}) })
describe('checkLogLevel', function() { describe('checkLogLevel', function () {
it('should request log level override from the config map', function() { it('should request log level override from the config map', function () {
this.logger.checkLogLevel() this.logger.checkLogLevel()
this.Fs.readFile.should.have.been.calledWithMatch( this.Fs.readFile.should.have.been.calledWithMatch(
'/logging/tracingEndTime' '/logging/tracingEndTime'
) )
}) })
describe('when read errors', function() { describe('when read errors', function () {
beforeEach(function() { beforeEach(function () {
this.Fs.readFile.yields(new Error('error')) this.Fs.readFile.yields(new Error('error'))
this.logger.checkLogLevel() this.logger.checkLogLevel()
}) })
it('should only set default level', function() { it('should only set default level', function () {
this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith( this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
'debug' 'debug'
) )
}) })
}) })
describe('when the file is empty', function() { describe('when the file is empty', function () {
beforeEach(function() { beforeEach(function () {
this.Fs.readFile.yields(null, '') this.Fs.readFile.yields(null, '')
this.logger.checkLogLevel() this.logger.checkLogLevel()
}) })
it('should only set default level', function() { it('should only set default level', function () {
this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith( this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
'debug' 'debug'
) )
}) })
}) })
describe('when time value returned that is less than current time', function() { describe('when time value returned that is less than current time', function () {
beforeEach(function() { beforeEach(function () {
this.Fs.readFile.yields(null, '1') this.Fs.readFile.yields(null, '1')
this.logger.checkLogLevel() this.logger.checkLogLevel()
}) })
it('should only set default level', function() { it('should only set default level', function () {
this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith( this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
'debug' 'debug'
) )
}) })
}) })
describe('when time value returned that is more than current time', function() { describe('when time value returned that is more than current time', function () {
describe('when level is already set', function() { describe('when level is already set', function () {
beforeEach(function() { beforeEach(function () {
this.bunyanLogger.level.returns(10) this.bunyanLogger.level.returns(10)
this.Fs.readFile.yields(null, (this.start + 1000).toString()) this.Fs.readFile.yields(null, (this.start + 1000).toString())
this.logger.checkLogLevel() this.logger.checkLogLevel()
}) })
it('should set trace level', function() { it('should set trace level', function () {
this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith( this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
'trace' 'trace'
) )
}) })
}) })
describe('when level is not already set', function() { describe('when level is not already set', function () {
beforeEach(function() { beforeEach(function () {
this.bunyanLogger.level.returns(20) this.bunyanLogger.level.returns(20)
this.Fs.readFile.yields(null, (this.start + 1000).toString()) this.Fs.readFile.yields(null, (this.start + 1000).toString())
this.logger.checkLogLevel() this.logger.checkLogLevel()
}) })
it('should set trace level', function() { it('should set trace level', function () {
this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith( this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
'trace' 'trace'
) )
@ -365,8 +365,8 @@ describe('LoggingManager', function() {
}) })
}) })
describe('ringbuffer', function() { describe('ringbuffer', function () {
beforeEach(function() { beforeEach(function () {
this.logBufferMock = [ this.logBufferMock = [
{ msg: 'log 1' }, { msg: 'log 1' },
{ msg: 'log 2' }, { msg: 'log 2' },
@ -374,19 +374,19 @@ describe('LoggingManager', function() {
] ]
}) })
describe('when ring buffer size is positive', function() { describe('when ring buffer size is positive', function () {
beforeEach(function() { beforeEach(function () {
process.env.LOG_RING_BUFFER_SIZE = '20' process.env.LOG_RING_BUFFER_SIZE = '20'
this.logger = this.LoggingManager.initialize(this.loggerName) this.logger = this.LoggingManager.initialize(this.loggerName)
this.logger.ringBuffer.records = this.logBufferMock this.logger.ringBuffer.records = this.logBufferMock
this.logger.error({}, 'error') this.logger.error({}, 'error')
}) })
afterEach(function() { afterEach(function () {
process.env.LOG_RING_BUFFER_SIZE = undefined process.env.LOG_RING_BUFFER_SIZE = undefined
}) })
it('should include buffered logs in error log and filter out error logs in buffer', function() { it('should include buffered logs in error log and filter out error logs in buffer', function () {
this.bunyanLogger.error.lastCall.args[0].logBuffer.should.deep.equal([ this.bunyanLogger.error.lastCall.args[0].logBuffer.should.deep.equal([
{ msg: 'log 1' }, { msg: 'log 1' },
{ msg: 'log 2' } { msg: 'log 2' }
@ -394,50 +394,50 @@ describe('LoggingManager', function() {
}) })
}) })
describe('when ring buffer size is zero', function() { describe('when ring buffer size is zero', function () {
beforeEach(function() { beforeEach(function () {
process.env.LOG_RING_BUFFER_SIZE = '0' process.env.LOG_RING_BUFFER_SIZE = '0'
this.logger = this.LoggingManager.initialize(this.loggerName) this.logger = this.LoggingManager.initialize(this.loggerName)
this.logger.error({}, 'error') this.logger.error({}, 'error')
}) })
afterEach(function() { afterEach(function () {
process.env.LOG_RING_BUFFER_SIZE = undefined process.env.LOG_RING_BUFFER_SIZE = undefined
}) })
it('should not include buffered logs in error log', function() { it('should not include buffered logs in error log', function () {
expect(this.bunyanLogger.error.lastCall.args[0].logBuffer).be.undefined expect(this.bunyanLogger.error.lastCall.args[0].logBuffer).be.undefined
}) })
}) })
}) })
describe('stackdriver logging', function() { describe('stackdriver logging', function () {
describe('when STACKDRIVER_LOGGING is unset', function() { describe('when STACKDRIVER_LOGGING is unset', function () {
beforeEach(function() { beforeEach(function () {
process.env.STACKDRIVER_LOGGING = undefined process.env.STACKDRIVER_LOGGING = undefined
this.LoggingManager.initialize(this.loggerName) this.LoggingManager.initialize(this.loggerName)
}) })
it('is disabled', function() { it('is disabled', function () {
expect(this.bunyanLogger.addStream).not.to.have.been.calledWith( expect(this.bunyanLogger.addStream).not.to.have.been.calledWith(
this.stackdriverStreamConfig this.stackdriverStreamConfig
) )
}) })
}) })
describe('when STACKDRIVER_LOGGING is true', function() { describe('when STACKDRIVER_LOGGING is true', function () {
beforeEach(function() { beforeEach(function () {
process.env.STACKDRIVER_LOGGING = 'true' process.env.STACKDRIVER_LOGGING = 'true'
this.LoggingManager.initialize(this.loggerName) this.LoggingManager.initialize(this.loggerName)
}) })
it('is enabled', function() { it('is enabled', function () {
expect(this.bunyanLogger.addStream).to.have.been.calledWith( expect(this.bunyanLogger.addStream).to.have.been.calledWith(
this.stackdriverStreamConfig this.stackdriverStreamConfig
) )
}) })
it('is configured properly', function() { it('is configured properly', function () {
expect(this.GCPLogging.LoggingBunyan).to.have.been.calledWith({ expect(this.GCPLogging.LoggingBunyan).to.have.been.calledWith({
logName: this.loggerName, logName: this.loggerName,
serviceContext: { service: this.loggerName } serviceContext: { service: this.loggerName }