From 47915fe85dc8b50713c4344d15cb9082d8147b8f Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Fri, 24 Mar 2023 08:50:51 +0000 Subject: [PATCH] fix: disable trace logging by default (#32) Disable trace logging unless the user has explicitly enabled it --- src/index.ts | 23 ++++++++++++++++++++++- test/index.spec.ts | 45 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2f850a9..4a5e3f1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 }) } diff --git a/test/index.spec.ts b/test/index.spec.ts index 71d2b48..4c753bc 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -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() }) })