Skip to content

Commit

Permalink
feat(logger): add log method for all levels
Browse files Browse the repository at this point in the history
  • Loading branch information
devsheva committed Sep 3, 2024
1 parent caa1202 commit 282065b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
61 changes: 59 additions & 2 deletions spec/logger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { describe, expect, it } from 'vitest'
import { describe, expect, expectTypeOf, it, vi } from 'vitest'
import Base from '../src/base.js'
import { loggerDecorator } from '../src/logger.js'
import {
getLogLevel,
loggerDecorator,
LogLevel,
LogLevelStrings
} from '../src/logger.js'

describe('logger', () => {
it('has a logger property', () => {
Expand Down Expand Up @@ -39,4 +44,56 @@ describe('logger', () => {
})
})
})

describe('logLevels', () => {
it.each(['verbose', 'debug', 'info', 'warn', 'error'])(
`has a %s method`,
(level) => {
class Test extends Base {}
loggerDecorator()(Test)
const instance = new Test()
expect(instance.logger).toHaveProperty(level)
expect(instance.logger[level]).toBeInstanceOf(Function)
}
)

it('should log a message at the specified level', () => {
class Test extends Base {
hello() {
this.logger.debug('hello')
}
}

loggerDecorator('test', { level: 'debug' })(Test)
const testInstance = new Test()
const consoleSpy = vi.spyOn(console, 'debug')
testInstance.hello()
expect(consoleSpy).toHaveBeenCalledOnce()
})

it('should not log a message at a lower level', () => {
class Test extends Base {
hello() {
this.logger.debug('hello')
}
}

loggerDecorator('test', { level: 'error' })(Test)
const testInstance = new Test()
const consoleSpy = vi.spyOn(console, 'debug')
testInstance.hello()
expect(consoleSpy).not.toHaveBeenCalled()
})
})
})

describe('getLogLevel', () => {
it.each(['verbose', 'debug', 'info', 'warn', 'error'])(
'returns the correct log level for %s',
(level) => {
expect(getLogLevel(level as LogLevelStrings)).toBe(
LogLevel[level.toUpperCase() as keyof typeof LogLevel]
)
}
)
})
26 changes: 25 additions & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ function Logger(
level: options.level
}

Object.assign(logger, {
verbose(...args: any[]) {
if (getLogLevel(logger.level) <= LogLevel.VERBOSE)
console.log(`[${name}] - `, ...args)
},
debug(...args: any[]) {
if (getLogLevel(logger.level) <= LogLevel.DEBUG)
console.debug(`[${name}] - `, ...args)
},
info(...args: any[]) {
if (getLogLevel(logger.level) <= LogLevel.INFO)
console.info(`[${name}] - `, ...args)
},
warn(...args: any[]) {
if (getLogLevel(logger.level) <= LogLevel.WARN)
console.warn(`[${name}] - `, ...args)
},
error(...args: any[]) {
if (getLogLevel(logger.level) <= LogLevel.ERROR)
console.error(`[${name}] - `, ...args)
}
})

Object.defineProperty(target.prototype, 'logger', {
value: logger
})
Expand All @@ -39,4 +62,5 @@ function Logger(
const loggerDecorator = Logger

export default Logger
export { LogLevel, loggerDecorator }
export { LogLevel, loggerDecorator, getLogLevel }
export type { LogLevelStrings }

0 comments on commit 282065b

Please sign in to comment.