Skip to content

Commit

Permalink
refactor: create formatterConfig to separate driver and formatter con…
Browse files Browse the repository at this point in the history
…figs
  • Loading branch information
jlenon7 committed Apr 4, 2022
1 parent 0f78392 commit 27142d8
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 130 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/logger",
"version": "1.0.6",
"version": "1.0.7",
"description": "",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down Expand Up @@ -153,7 +153,7 @@
}
},
"dependencies": {
"@athenna/ioc": "1.0.8",
"@athenna/ioc": "1.0.9",
"@secjs/utils": "1.8.0",
"reflect-metadata": "0.1.13",
"tscpaths": "0.0.9"
Expand Down
14 changes: 6 additions & 8 deletions src/Drivers/ConsoleDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,36 @@ export interface ConsoleDriverOpts {
context: string
formatter: string
streamType: string
formatterConfig: any
}

export class ConsoleDriver implements DriverContract {
private readonly _level: string
private readonly _context: string
private readonly _formatter: string
private readonly _streamType: string
private readonly _formatterConfig: any

constructor(channel: string, configs: any = {}) {
const channelConfig = Config.get(`logging.channels.${channel}`)

this._level = configs.level || channelConfig.level
this._context = configs.context || channelConfig.context
this._formatter = configs.formatter || channelConfig.formatter
this._streamType = configs.streamType || channelConfig.streamType
this._formatterConfig =
configs.formatterConfig || channelConfig.formatterConfig
}

transport(message: string, options?: ConsoleDriverOpts): void {
options = Object.assign(
{},
{
level: this._level,
context: this._context,
formatter: this._formatter,
streamType: this._streamType,
},
options,
)
) as ConsoleDriverOpts

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

process[options.streamType].write(`${message}\n`)
Expand Down
14 changes: 6 additions & 8 deletions src/Drivers/DebugDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,36 @@ export interface DebugDriverOpts {
context: string
formatter: string
namespace: string
formatterConfig: any
}

export class DebugDriver implements DriverContract {
private readonly _level: string
private readonly _context: string
private readonly _formatter: string
private readonly _namespace: string
private readonly _formatterConfig: any

constructor(channel: string, configs: any = {}) {
const channelConfig = Config.get(`logging.channels.${channel}`)

this._level = configs.level || channelConfig.level
this._context = configs.context || channelConfig.context
this._formatter = configs.formatter || channelConfig.formatter
this._namespace = configs.namespace || channelConfig.namespace
this._formatterConfig =
configs.formatterConfig || channelConfig.formatterConfig
}

transport(message: string, options?: DebugDriverOpts): void {
options = Object.assign(
{},
{
level: this._level,
context: this._context,
formatter: this._formatter,
namespace: this._namespace,
},
options,
)
) as DebugDriverOpts

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

debug(options.namespace)(message)
Expand Down
14 changes: 6 additions & 8 deletions src/Drivers/FileDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,32 @@ export interface FileDriverOpts {
context: string
formatter: string
filePath: string
formatterConfig: any
}

export class FileDriver implements DriverContract {
private readonly _level: string
private readonly _context: string
private readonly _filePath: string
private readonly _formatter: string
private readonly _formatterConfig: any

constructor(channel: string, configs: any = {}) {
const channelConfig = Config.get(`logging.channels.${channel}`)

this._level = configs.level || channelConfig.level
this._context = configs.context || channelConfig.context
this._filePath = configs.filePath || channelConfig.filePath
this._formatter = configs.formatter || channelConfig.formatter
this._formatterConfig =
configs.formatterConfig || channelConfig.formatterConfig
}

async transport(message: string, options?: FileDriverOpts): Promise<void> {
options = Object.assign(
{},
{
level: this._level,
context: this._context,
filePath: this._filePath,
formatter: this._formatter,
},
options,
)
) as FileDriverOpts

const filePath = options.filePath
const { dir } = parse(filePath)
Expand All @@ -57,7 +55,7 @@ export class FileDriver implements DriverContract {

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

return new Promise((resolve, reject) => {
Expand Down
Loading

0 comments on commit 27142d8

Please sign in to comment.