Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel committed Jun 28, 2024
1 parent 53a2ff3 commit dba315b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@ import { logger } from './winston.config'

@Injectable()
export class LoggerService implements NestLoggerService {
private context: string

setContext(context: string) {
this.context = context
}

log(message: string, ...optionalParams: any[]) {
console.log(optionalParams[0])
logger.info(message, optionalParams)
logger.info(message, this.getMeta(optionalParams))
}

info(message: string, ...optionalParams: any[]) {
Expand Down Expand Up @@ -39,9 +32,18 @@ export class LoggerService implements NestLoggerService {
}

private getMeta(...optionalParams: any[]): object {
return {
...(this.context ? { context: this.context } : {}),
...(optionalParams.length && optionalParams[0] ? { context: optionalParams[0] } : {})
if (!optionalParams.length) {
return {}
}

if (typeof optionalParams[0][0] === 'string') {
return { context: optionalParams[0][0] }
}

if (typeof optionalParams[0] === 'object') {
return { ...optionalParams[0][0] }
}

return {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const formatLocalLog = (info: winston.Logform.TransformableInfo) => {
levelColor = '\x1b[0m' // Reset color
}

return `${info.timestamp} [${info.context}] ${levelColor}${info.level.toUpperCase()}\x1b[0m: ${message}${rest ? `\n${stringify(rest, 2)}` : ''}`
return `${info.timestamp} ${levelColor}[${info.level.toUpperCase()}]\x1b[0m: ${info.message}\n${stringify(rest, 2)}`
}

export const logger = winston.createLogger({
Expand All @@ -89,7 +89,6 @@ export const logger = winston.createLogger({
winston.format.timestamp(),
winston.format.json(),
winston.format.printf((info) => {
console.log({ info })
if (!isProduction) {
return formatLocalLog(info)
}
Expand All @@ -98,7 +97,6 @@ export const logger = winston.createLogger({

return stringify({
level: info.level,
context: info.context,
...details
})
})
Expand Down

0 comments on commit dba315b

Please sign in to comment.