Skip to content

Commit

Permalink
feat: remove DebugLogLevel support
Browse files Browse the repository at this point in the history
As it is slowly replaced by CommonLogger interface from js-lib
And its AppEngine implementation in backend-lib
  • Loading branch information
kirillgroshkov committed Nov 9, 2021
1 parent 79c69d1 commit 7e56341
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 43 deletions.
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { tableDiff, TableDiffOptions } from './diff/tableDiff'
import { getGot } from './got/getGot'
import { GetGotOptions } from './got/got.model'
import { memoryUsage, memoryUsageFull, processSharedUtil } from './infra/process.util'
import { Debug, DebugLogLevel, IDebug, IDebugger } from './log/debug'
import { Debug, IDebug, IDebugger } from './log/debug'
import {
base64ToBuffer,
base64ToString,
Expand Down Expand Up @@ -287,7 +287,6 @@ export {
bufferToBase64,
base64ToBuffer,
Debug,
DebugLogLevel,
getSecretMap,
setSecretMap,
loadSecretsFromEnv,
Expand Down
12 changes: 1 addition & 11 deletions src/log/debug.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Debug, DebugLogLevel } from './debug'
import { Debug } from './debug'

const log = Debug('nodejs-lib')

Expand All @@ -24,16 +24,6 @@ test('debug', () => {
expect(log.enabled).toBe(true)

log('hello log', obj)
log.info('hello log', obj)
log.debug('hello log', obj)
log.warn('hello log', obj)
log.error('hello log', obj)

const level = DebugLogLevel.warn
log[level]('hello level')

log(err)
log.debug(err)
log.warn(err)
log.error(err)
})
26 changes: 1 addition & 25 deletions src/log/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ export interface DebugFormatters {
export interface IDebugger {
// (formatter: any, ...args: any[]): void;
(...args: any[]): void
debug: (...args: any[]) => void
info: (...args: any[]) => void // alias to just log()
warn: (...args: any[]) => void
error: (...args: any[]) => void

color: string
enabled: boolean
Expand All @@ -33,35 +29,15 @@ export interface IDebugger {
// extend: (namespace: string, delimiter?: string) => IDebugger
}

export enum DebugLogLevel {
debug = 'debug',
info = 'info',
warn = 'warn',
error = 'error',
}

const originalDebug = require('debug') as IDebug

// eslint-disable-next-line @typescript-eslint/naming-convention
export const Debug = ((namespace: string) => {
const instance = originalDebug(namespace)
instance.log = console.log.bind(console) // this enables colors for objects
instance.info = instance.bind(instance)

const instanceDebug = originalDebug([namespace, 'debug'].join(':'))
instanceDebug.log = console.debug.bind(console)
instance.debug = instanceDebug.bind(instanceDebug)

const instanceWarn = originalDebug([namespace, 'warn'].join(':'))
instanceWarn.log = console.warn.bind(console)
instance.warn = instanceWarn.bind(instanceWarn)

const instanceError = originalDebug([namespace, 'error'].join(':'))
instanceError.log = console.error.bind(console)
instance.error = instanceError.bind(instanceError)

return instance
}) as IDebug

Debug.coerce = originalDebug.coerce.bind(originalDebug)
Debug.disable = originalDebug.disable.bind(originalDebug)
Debug.enable = originalDebug.enable.bind(originalDebug)
Expand Down
6 changes: 1 addition & 5 deletions src/test/debug.ts → src/test/debug.script.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
DEBUG=* yarn tsn ./src/test/debug.ts
DEBUG=* yarn tsn ./src/test/debug.script.ts
*/

Expand All @@ -21,11 +21,7 @@ const err = new Error('Im an error')

console.log('console1', obj)
log('hello log', obj)
log.warn('hello log', obj)
log.error('hello log', obj)

log(err)
log.warn(err)
log.error(err)

console.log({ enabled: log.enabled, enabled2: Debug.enabled('nodejs-lib') })

0 comments on commit 7e56341

Please sign in to comment.