From bec6cb34b314a88cdc78d477a89b51e6941565fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Wed, 30 Mar 2022 18:12:13 -0300 Subject: [PATCH] fix: add configs in drivers constructor --- package.json | 2 +- src/Drivers/ConsoleDriver.ts | 14 ++++++++------ src/Drivers/DebugDriver.ts | 13 +++++++------ src/Drivers/FileDriver.ts | 21 +++++++++++++-------- tests/Stubs/config/logging.ts | 4 ++++ 5 files changed, 33 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 2aab566..3c2ab91 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/logger", - "version": "1.0.2", + "version": "1.0.3", "description": "", "license": "MIT", "author": "João Lenon ", diff --git a/src/Drivers/ConsoleDriver.ts b/src/Drivers/ConsoleDriver.ts index a313532..1c1d01c 100644 --- a/src/Drivers/ConsoleDriver.ts +++ b/src/Drivers/ConsoleDriver.ts @@ -16,6 +16,7 @@ export interface ConsoleDriverOpts { color: Color level: string context: string + formatter: string streamType: string } @@ -25,13 +26,13 @@ export class ConsoleDriver implements DriverContract { private readonly _formatter: string private readonly _streamType: string - constructor(channel: string) { + constructor(channel: string, configs: any = {}) { const channelConfig = Config.get(`logging.channels.${channel}`) - this._level = channelConfig.level || 'INFO' - this._context = channelConfig.context || 'ConsoleDriver' - this._formatter = channelConfig.formatter || 'context' - this._streamType = channelConfig.streamType || 'stdout' + this._level = configs.level || channelConfig.level + this._context = configs.context || channelConfig.context + this._formatter = configs.formatter || channelConfig.formatter + this._streamType = configs.streamType || channelConfig.streamType } transport(message: string, options?: ConsoleDriverOpts): void { @@ -40,12 +41,13 @@ export class ConsoleDriver implements DriverContract { { level: this._level, context: this._context, + formatter: this._formatter, streamType: this._streamType, }, options, ) - message = FormatterFactory.fabricate(this._formatter).format( + message = FormatterFactory.fabricate(options.formatter).format( message, options, ) diff --git a/src/Drivers/DebugDriver.ts b/src/Drivers/DebugDriver.ts index e54b5c8..7f7b366 100644 --- a/src/Drivers/DebugDriver.ts +++ b/src/Drivers/DebugDriver.ts @@ -27,13 +27,13 @@ export class DebugDriver implements DriverContract { private readonly _formatter: string private readonly _namespace: string - constructor(channel: string) { + constructor(channel: string, configs: any = {}) { const channelConfig = Config.get(`logging.channels.${channel}`) - this._level = channelConfig.level || 'DEBUG' - this._context = channelConfig.context || 'DebugDriver' - this._formatter = channelConfig.formatter || 'context' - this._namespace = channelConfig.namespace || 'api:main' + this._level = configs.level || channelConfig.level + this._context = configs.context || channelConfig.context + this._formatter = configs.formatter || channelConfig.formatter + this._namespace = configs.namespace || channelConfig.namespace } transport(message: string, options?: DebugDriverOpts): void { @@ -42,12 +42,13 @@ export class DebugDriver implements DriverContract { { level: this._level, context: this._context, + formatter: this._formatter, namespace: this._namespace, }, options, ) - message = FormatterFactory.fabricate(this._formatter).format( + message = FormatterFactory.fabricate(options.formatter).format( message, options, ) diff --git a/src/Drivers/FileDriver.ts b/src/Drivers/FileDriver.ts index 4192ed9..27e2471 100644 --- a/src/Drivers/FileDriver.ts +++ b/src/Drivers/FileDriver.ts @@ -8,8 +8,8 @@ */ import { parse } from 'path' +import { Config } from '@secjs/utils' import { Color } from 'src/Utils/Color' -import { Path, Config } from '@secjs/utils' import { DriverContract } from 'src/Contracts/DriverContract' import { createWriteStream, existsSync, mkdirSync } from 'fs' import { FormatterFactory } from 'src/Factories/FormatterFactory' @@ -27,19 +27,24 @@ export class FileDriver implements DriverContract { private readonly _filePath: string private readonly _formatter: string - constructor(channel: string) { + constructor(channel: string, configs: any = {}) { const channelConfig = Config.get(`logging.channels.${channel}`) - this._level = channelConfig.level || 'INFO' - this._context = channelConfig.context || 'FileDriver' - this._filePath = channelConfig.filePath || Path.noBuild().logs('secjs.log') - this._formatter = channelConfig.formatter || 'log' + this._level = configs.level || channelConfig.level + this._context = configs.context || channelConfig.context + this._filePath = configs.filePath || channelConfig.filePath + this._formatter = configs.formatter || channelConfig.formatter } async transport(message: string, options?: FileDriverOpts): Promise { options = Object.assign( {}, - { level: this._level, context: this._context, filePath: this._filePath }, + { + level: this._level, + context: this._context, + filePath: this._filePath, + formatter: this._formatter, + }, options, ) @@ -50,7 +55,7 @@ export class FileDriver implements DriverContract { mkdirSync(dir, { recursive: true }) } - message = FormatterFactory.fabricate(this._formatter).format( + message = FormatterFactory.fabricate(options.formatter).format( message, options, ) diff --git a/tests/Stubs/config/logging.ts b/tests/Stubs/config/logging.ts index 24e4d6c..ab4b802 100644 --- a/tests/Stubs/config/logging.ts +++ b/tests/Stubs/config/logging.ts @@ -29,17 +29,21 @@ export default { channels: { application: { driver: 'console', + level: 'INFO', context: 'Logger', formatter: 'context', + streamType: 'stdout', }, debug: { driver: 'debug', + level: 'DEBUG', context: 'Debugger', formatter: 'context', namespace: 'api:main', }, file: { driver: 'file', + level: 'INFO', context: 'Logger', formatter: 'log', filePath: Path.noBuild().logs('athenna.log'),