Skip to content

Commit

Permalink
Merge pull request #101 from sasjs/logger-fix
Browse files Browse the repository at this point in the history
fix(logger): add filter not valid arguments
  • Loading branch information
YuryShkoda authored Jul 20, 2021
2 parents f8b9c6c + af03039 commit f5569d0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/logger/Logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,25 @@ describe('Logger', () => {
expect(consola.log).toHaveBeenCalledTimes(1)
})

it('should log correct error message when empty string has been provided as argument', () => {
const logger = new Logger(LogLevel.Error)
spyOn(consola, 'error')

const message = `'test_results' not found in server response, to debug click https://server.com/SASJobExecution/?_program=/Public/sasjs/jobs/tests/macros/friday.test&_debug=2477&_contextName=SAS%20Job%20Execution%20compute%20context`

logger.error(message, '')

expect(consola.error).toHaveBeenLastCalledWith(message)
})

it('should ignore not valid args', () => {
const logger = new Logger(LogLevel.Error)

expect(logger['filterArgs']([false, '', undefined, null, 'test'])).toEqual([
'test'
])
})

describe('logger.table', () => {
it('should log a table with default border style without head', () => {
const logger = new Logger(LogLevel.Debug)
Expand Down
14 changes: 9 additions & 5 deletions src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ export class Logger {

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

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

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

Expand All @@ -49,12 +49,12 @@ export class Logger {

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

log = (message: string, ...args: any): void => {
consola.log(message, ...args)
consola.log(message, ...this.filterArgs(args))
}

table = (
Expand Down Expand Up @@ -106,4 +106,8 @@ export class Logger {
: table.toString()) + '\n'
)
}

private filterArgs(args: any[]) {
return args.filter((arg: any) => (arg ? arg : false))
}
}

0 comments on commit f5569d0

Please sign in to comment.