Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
fix: disable trace logging by default (#32)
Browse files Browse the repository at this point in the history
Disable trace logging unless the user has explicitly enabled it
  • Loading branch information
achingbrain committed Mar 24, 2023
1 parent 6e5ed97 commit 47915fe
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,31 @@ export interface Logger {
enabled: boolean
}

function createDisabledLogger (namespace: string): debug.Debugger {
const logger = (): void => {}
logger.enabled = false
logger.color = ''
logger.diff = 0
logger.log = (): void => {}
logger.namespace = namespace
logger.destroy = () => true
logger.extend = () => logger

return logger
}

export function logger (name: string): Logger {
// trace logging is a no-op by default
let trace: debug.Debugger = createDisabledLogger(`${name}:trace`)

// look at all the debug names and see if trace logging has explicitly been enabled
if (debug.enabled(`${name}:trace`) && debug.names.map(r => r.toString()).find(n => n.includes(':trace')) != null) {
trace = debug(`${name}:trace`)
}

return Object.assign(debug(name), {
error: debug(`${name}:error`),
trace: debug(`${name}:trace`)
trace
})
}

Expand Down
45 changes: 44 additions & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,55 @@
import { expect } from 'aegir/chai'
import { logger } from '../src/index.js'
import debug from 'debug'

describe('logger', () => {
it('creates a logger', () => {
const log = logger('hello')

expect(log).to.be.a('function')
expect(log).to.a.property('enabled').that.is.not.true()
expect(log).to.have.property('error').that.is.a('function')
expect(log).to.have.property('enabled')
expect(log).to.have.nested.property('error.enabled').that.is.not.true()
expect(log).to.have.property('trace').that.is.a('function')
expect(log).to.have.nested.property('trace.enabled').that.is.not.true()
})

it('creates a logger with logging enabled', () => {
debug.enable('enabled-logger')

const log = logger('enabled-logger')

expect(log).to.be.a('function')
expect(log).to.a.property('enabled').that.is.true()
expect(log).to.have.property('error').that.is.a('function')
expect(log).to.have.nested.property('error.enabled').that.is.not.true()
expect(log).to.have.property('trace').that.is.a('function')
expect(log).to.have.nested.property('trace.enabled').that.is.not.true()
})

it('creates a logger with logging and errors enabled', () => {
debug.enable('enabled-with-error-logger*')

const log = logger('enabled-with-error-logger')

expect(log).to.be.a('function')
expect(log).to.a.property('enabled').that.is.true()
expect(log).to.have.property('error').that.is.a('function')
expect(log).to.have.nested.property('error.enabled').that.is.true()
expect(log).to.have.property('trace').that.is.a('function')
expect(log).to.have.nested.property('trace.enabled').that.is.not.true()
})

it('creates a logger with trace enabled', () => {
debug.enable('enabled-with-trace-logger*,*:trace')

const log = logger('enabled-with-trace-logger')

expect(log).to.be.a('function')
expect(log).to.a.property('enabled').that.is.true()
expect(log).to.have.property('error').that.is.a('function')
expect(log).to.have.nested.property('error.enabled').that.is.true()
expect(log).to.have.property('trace').that.is.a('function')
expect(log).to.have.nested.property('trace.enabled').that.is.true()
})
})

0 comments on commit 47915fe

Please sign in to comment.