Skip to content

Commit

Permalink
fix: Logger fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nklomp committed Jul 24, 2024
1 parent e969b97 commit 75b6925
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
"@sphereon/oid4vci-client": "0.14.0",
"@sphereon/oid4vci-issuer": "0.14.0",
"@noble/hashes": "1.2.0",
"debug": "^4.3.5",
"did-jwt": "6.11.6",
"did-jwt-vc": "3.1.3",
"ethr-did": "2.3.9",
Expand Down
2 changes: 1 addition & 1 deletion packages/ssi-types/__tests__/logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ describe('Simple logging', () => {
it('Should perform a debug package, console and event log', () => {
Loggers.DEFAULT.options('all', { methods: [LogMethod.DEBUG_PKG, LogMethod.CONSOLE, LogMethod.EVENT] })
.get('all')
.log('TEST ALL')
.log('TEST ALL', 'additional arg1', {arg2: "value2"})
})
})
45 changes: 28 additions & 17 deletions packages/ssi-types/src/logging/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Debug from 'debug'
import {debug} from 'debug'
import { EventEmitter } from 'events'

export enum LogLevel {
Expand Down Expand Up @@ -96,7 +96,7 @@ export type ISimpleLogger<LogType> = {
trace(value: LogType, ...args: any[]): void
warning(value: LogType, ...args: any[]): void
error(value: LogType, ...args: any[]): void
logl(level: LogLevel, value: LogType, ...args: any[]): void
logl(level: LogLevel, value: LogType, ...argsW: any[]): void
}

export class SimpleLogger implements ISimpleLogger<any> {
Expand All @@ -116,28 +116,32 @@ export class SimpleLogger implements ISimpleLogger<any> {
}

trace(value: any, ...args: any[]) {
this.logl(LogLevel.TRACE, value, args)
this.logImpl(LogLevel.TRACE, value, ...args)
}

debug(value: any, ...args: any[]) {
this.logl(LogLevel.DEBUG, value, args)
this.logImpl(LogLevel.DEBUG, value, ...args)
}

info(value: any, ...args: any[]) {
this.logl(LogLevel.INFO, value, args)
this.logImpl(LogLevel.INFO, value, ...args)
}

warning(value: any, ...args: any[]) {
this.logl(LogLevel.WARNING, value, args)
this.logImpl(LogLevel.WARNING, value, ...args)
}

error(value: any, ...args: any[]) {
this.logl(LogLevel.ERROR, value, args)
this.logImpl(LogLevel.ERROR, value, ...args)
}

logl(level: LogLevel, value: any, ...args: any[]) {
this.logImpl(level, value, ...args)
}

private logImpl(level: LogLevel, value: any, ...args: any[]) {
const date = new Date().toISOString()
const filteredArgs = args.filter((v) => v!!)
const filteredArgs = args?.filter((v) => v!!) ?? []
const arg = filteredArgs.length === 0 || filteredArgs[0] == undefined ? undefined : filteredArgs

function toLogValue(options: SimpleLogOptions): any {
Expand All @@ -156,32 +160,39 @@ export class SimpleLogger implements ISimpleLogger<any> {
logArgs.push(args)
}
if (this.options.methods.includes(LogMethod.DEBUG_PKG)) {
const log = debug(this._options.namespace)
if (arg) {
Debug(this._options.namespace)(`${date}- ${value}`, arg)
log(`${date}- ${value},`, ...arg)
} else {
Debug(this._options.namespace)(`${date}- ${value}`)
log(`${date}- ${value}`)
}
}

if (this.options.methods.includes(LogMethod.CONSOLE)) {
const [value, args] = logArgs
let logMethod = console.info
switch (level) {
case LogLevel.TRACE:
console.trace(value, args)
logMethod = console.trace
break
case LogLevel.DEBUG:
console.debug(value, args)
logMethod = console.debug
break
case LogLevel.INFO:
console.info(value, args)
logMethod = console.info
break
case LogLevel.WARNING:
console.warn(value, args)
logMethod = console.warn
break
case LogLevel.ERROR:
console.error(value, args)
logMethod = console.error
break
}
if (args) {
logMethod(value + ',', ...args)
} else {
logMethod(value)
}
}

if (this.options.methods.includes(LogMethod.EVENT)) {
Expand All @@ -195,8 +206,8 @@ export class SimpleLogger implements ISimpleLogger<any> {
}
}

log(value: any, args?: any[]) {
this.logl(this.options.defaultLogLevel, value, args)
log(value: any, ...args: any[]) {
this.logImpl(this.options.defaultLogLevel, value, ...args)
}
}

Expand Down

0 comments on commit 75b6925

Please # to comment.