Skip to content

Commit

Permalink
Merge pull request #186 from sasjs/log-level-trace
Browse files Browse the repository at this point in the history
feat: added log level Trace
  • Loading branch information
YuryShkoda authored Apr 5, 2022
2 parents 3749e29 + 5f2cab0 commit 1ae9f6d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
30 changes: 30 additions & 0 deletions src/logger/Logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ describe('Logger', () => {
expect(consola.debug).toHaveBeenCalledTimes(1)
})

it('should log debug messages when the log level is Trace', () => {
const logger = new Logger(LogLevel.Trace)
jest.spyOn(consola, 'debug')

logger.debug('This is debug.')

expect(consola.debug).toHaveBeenCalledTimes(1)
expect(consola.debug).toHaveBeenCalledWith('This is debug.')
})

it('should log trace messages when the log level is Trace', () => {
const logger = new Logger(LogLevel.Trace)
jest.spyOn(consola, 'debug')

logger.trace('This is trace.')

expect(consola.debug).toHaveBeenCalledTimes(1)
expect(consola.debug).toHaveBeenCalledWith('This is trace.')
})

it('should not log debug messages when the log level is Error', () => {
const logger = new Logger(LogLevel.Error)
jest.spyOn(consola, 'debug')
Expand All @@ -96,6 +116,16 @@ describe('Logger', () => {
expect(consola.success).toHaveBeenCalledTimes(1)
})

it('should log success messages when the log level is Trace', () => {
const logger = new Logger(LogLevel.Trace)
jest.spyOn(consola, 'success')

logger.success('This is success.')

expect(consola.success).toHaveBeenCalledTimes(1)
expect(consola.success).toHaveBeenCalledWith('This is success.')
})

it('should not log success messages when the log level is Error', () => {
const logger = new Logger(LogLevel.Error)
jest.spyOn(consola, 'success')
Expand Down
13 changes: 5 additions & 8 deletions src/logger/isLowerThanOrEqualTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ export const isLowerThanOrEqualTo = (
currentLevel: LogLevel,
level: LogLevel
): boolean => {
if (currentLevel === LogLevel.Off) {
return false
}
if (currentLevel === LogLevel.Off) return false

if (currentLevel === LogLevel.Debug) {
return true
}
if (currentLevel === LogLevel.Debug) return true

if (currentLevel === LogLevel.Trace) return true

if (currentLevel === LogLevel.Info) {
return (
Expand All @@ -20,9 +18,8 @@ export const isLowerThanOrEqualTo = (
)
}

if (currentLevel === LogLevel.Warn) {
if (currentLevel === LogLevel.Warn)
return level === LogLevel.Warn || level === LogLevel.Error
}

return level === LogLevel.Error
}
1 change: 1 addition & 0 deletions src/logger/logLevel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum LogLevel {
Trace = 'Trace',
Debug = 'Debug',
Info = 'Info',
Warn = 'Warn',
Expand Down
6 changes: 6 additions & 0 deletions src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export class Logger {

private _logLevel: LogLevel = LogLevel.Error

trace = (message: string, ...args: any): void => {
if (isLowerThanOrEqualTo(this._logLevel, LogLevel.Trace)) {
consola.debug(message, ...this.filterArgs(args))
}
}

debug = (message: string, ...args: any): void => {
if (isLowerThanOrEqualTo(this._logLevel, LogLevel.Debug)) {
consola.debug(message, ...this.filterArgs(args))
Expand Down

0 comments on commit 1ae9f6d

Please sign in to comment.