Skip to content

Commit

Permalink
Merge pull request #9 from AthennaIO/develop
Browse files Browse the repository at this point in the history
fix: make logger always fallback to config/logging file options
  • Loading branch information
jlenon7 authored Apr 6, 2022
2 parents cb82c18 + 1f24d31 commit fcd6450
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/logger",
"version": "1.0.9",
"version": "1.1.0",
"description": "",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
15 changes: 12 additions & 3 deletions src/Drivers/ConsoleDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ export class ConsoleDriver implements DriverContract {

this._formatter = configs.formatter || channelConfig.formatter
this._streamType = configs.streamType || channelConfig.streamType
this._formatterConfig =
configs.formatterConfig || channelConfig.formatterConfig
this._formatterConfig = Object.assign(
{},
channelConfig.formatterConfig,
configs.formatterConfig,
)
}

transport(message: string, options?: ConsoleDriverOpts): void {
Expand All @@ -45,9 +48,15 @@ export class ConsoleDriver implements DriverContract {
options,
) as ConsoleDriverOpts

const formatterOptions = Object.assign(
{},
this._formatterConfig,
options.formatterConfig,
)

message = FormatterFactory.fabricate(options.formatter).format(
message,
options.formatterConfig || this._formatterConfig,
formatterOptions,
)

process[options.streamType].write(`${message}\n`)
Expand Down
15 changes: 12 additions & 3 deletions src/Drivers/DebugDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ export class DebugDriver implements DriverContract {

this._formatter = configs.formatter || channelConfig.formatter
this._namespace = configs.namespace || channelConfig.namespace
this._formatterConfig =
configs.formatterConfig || channelConfig.formatterConfig
this._formatterConfig = Object.assign(
{},
channelConfig.formatterConfig,
configs.formatterConfig,
)
}

transport(message: string, options?: DebugDriverOpts): void {
Expand All @@ -46,9 +49,15 @@ export class DebugDriver implements DriverContract {
options,
) as DebugDriverOpts

const formatterOptions = Object.assign(
{},
this._formatterConfig,
options.formatterConfig,
)

message = FormatterFactory.fabricate(options.formatter).format(
message,
options.formatterConfig || this._formatterConfig,
formatterOptions,
)

debug(options.namespace)(message)
Expand Down
15 changes: 12 additions & 3 deletions src/Drivers/FileDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ export class FileDriver implements DriverContract {

this._filePath = configs.filePath || channelConfig.filePath
this._formatter = configs.formatter || channelConfig.formatter
this._formatterConfig =
configs.formatterConfig || channelConfig.formatterConfig
this._formatterConfig = Object.assign(
{},
channelConfig.formatterConfig,
configs.formatterConfig,
)
}

async transport(message: string, options?: FileDriverOpts): Promise<void> {
Expand All @@ -53,9 +56,15 @@ export class FileDriver implements DriverContract {
mkdirSync(dir, { recursive: true })
}

const formatterOptions = Object.assign(
{},
this._formatterConfig,
options.formatterConfig,
)

message = FormatterFactory.fabricate(options.formatter).format(
message,
options.formatterConfig || this._formatterConfig,
formatterOptions,
)

return new Promise((resolve, reject) => {
Expand Down
6 changes: 2 additions & 4 deletions src/Formatters/CliFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { LevelTypes } from 'src/Contracts/LevelTypes'
import { FormatterContract } from 'src/Contracts/FormatterContract'

export interface CliFormatterOptions {
color: Chalk
chalk: Chalk
level: LevelTypes
}

Expand All @@ -30,9 +30,7 @@ export class CliFormatter implements FormatterContract {
return levelColors[level.toLowerCase()](`[ ${level.toLowerCase()} ]`)
}

format(message: string, options?: CliFormatterOptions): string {
options = Object.assign({}, { level: 'info' }, options)

format(message: string, options: CliFormatterOptions): string {
const level = CliFormatter.paintByLevel(options.level)

return `${level} ${message}`
Expand Down
11 changes: 3 additions & 8 deletions src/Formatters/JsonFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,14 @@ import { Color } from 'src/Utils/Color'
import { FormatterContract } from 'src/Contracts/FormatterContract'

export interface JsonFormatterOptions {
color: Chalk
chalk: Chalk
}

export class JsonFormatter implements FormatterContract {
format(
message: Record<any, unknown>,
options?: JsonFormatterOptions,
): string {
options = Object.assign({}, { color: Color.green }, options)

format(message: Record<any, unknown>, options: JsonFormatterOptions): string {
const pid = Color.yellow(`[Athenna] - PID: ${process.pid}`)

return `${pid} - ${Color.bold('JSON:')} ${options.color(
return `${pid} - ${Color.bold('JSON:')} ${options.chalk(
JSON.stringify(message, null, 2),
)}`
}
Expand Down
12 changes: 3 additions & 9 deletions src/Formatters/NestFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getTimestamp } from 'src/Utils/getTimestamp'
import { FormatterContract } from 'src/Contracts/FormatterContract'

export interface ContextFormatterOptions {
color: Chalk
chalk: Chalk
context: string
}

Expand All @@ -32,19 +32,13 @@ export class NestFormatter implements FormatterContract {
return result
}

format(message: string, options?: ContextFormatterOptions): string {
options = Object.assign(
{},
{ color: Color.green, context: 'Logger' },
options,
)

format(message: string, options: ContextFormatterOptions): string {
const pid = Color.yellow(`[Athenna] - PID: ${process.pid}`)
const timestamp = getTimestamp()
const messageCtx = Color.yellow(`[${options.context}] `)
const timestampDiff = NestFormatter.getTimestampDiff()

return `${pid} - ${timestamp} ${messageCtx}${options.color(
return `${pid} - ${timestamp} ${messageCtx}${options.chalk(
message,
)}${timestampDiff}`
}
Expand Down
10 changes: 4 additions & 6 deletions src/Formatters/SimpleFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { LevelTypes } from 'src/Contracts/LevelTypes'
import { FormatterContract } from 'src/Contracts/FormatterContract'

export interface LogFormatterOptions {
color: Chalk
chalk: Chalk
level: LevelTypes
}

Expand Down Expand Up @@ -42,16 +42,14 @@ export class SimpleFormatter implements FormatterContract {
success: Color.log,
}

return levelColors[level.toLowerCase()](`[${level}]`)
return levelColors[level.toLowerCase()](`[${level.toUpperCase()}]`)
}

format(message: string, options?: LogFormatterOptions): string {
options = Object.assign({}, { color: Color.green, level: 'info' }, options)

format(message: string, options: LogFormatterOptions): string {
const timestamp = getTimestamp()
const timestampDiff = SimpleFormatter.getTimestampDiff()
const level = SimpleFormatter.paintByLevel(options.level)

return `${level} - ${timestamp} ${options.color(message)}${timestampDiff}`
return `${level} - ${timestamp} ${options.chalk(message)}${timestampDiff}`
}
}
10 changes: 5 additions & 5 deletions src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class Logger {
streamType: 'stdout',
formatterConfig: {
level: 'INFO',
color: Color.cyan,
chalk: Color.cyan,
},
})

Expand All @@ -104,7 +104,7 @@ export class Logger {
streamType: 'stdout',
formatterConfig: {
level: 'WARN',
color: Color.orange,
chalk: Color.orange,
},
})

Expand All @@ -118,7 +118,7 @@ export class Logger {
streamType: 'stdout',
formatterConfig: {
level: 'ERROR',
color: Color.red,
chalk: Color.red,
},
})

Expand All @@ -132,7 +132,7 @@ export class Logger {
streamType: 'stdout',
formatterConfig: {
level: 'DEBUG',
color: Color.purple,
chalk: Color.purple,
},
})

Expand All @@ -146,7 +146,7 @@ export class Logger {
streamType: 'stdout',
formatterConfig: {
level: 'SUCCESS',
color: Color.green,
chalk: Color.green,
},
})

Expand Down
4 changes: 4 additions & 0 deletions tests/Stubs/config/logging.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Path } from '@secjs/utils'
import { Color } from 'src/Utils/Color'

export default {
/*
Expand Down Expand Up @@ -33,6 +34,7 @@ export default {
streamType: 'stdout',
formatterConfig: {
level: 'INFO',
chalk: Color.cyan,
context: 'Logger',
},
},
Expand All @@ -42,6 +44,7 @@ export default {
namespace: 'api:main',
formatterConfig: {
level: 'DEBUG',
chalk: Color.purple,
context: 'Debugger',
},
},
Expand All @@ -51,6 +54,7 @@ export default {
filePath: Path.noBuild().logs('athenna.log'),
formatterConfig: {
level: 'INFO',
chalk: Color.cyan,
context: 'Logger',
},
},
Expand Down

0 comments on commit fcd6450

Please sign in to comment.