From cfcbfc9b326c6104079d4ad37f1ce5079a805e91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Mon, 6 Dec 2021 22:48:07 -0300 Subject: [PATCH] fix: remove @secjs/config dependency for less coupling --- package-lock.json | 31 +++++++++---------------------- package.json | 5 ++--- src/Drivers/ConsoleDriver.ts | 18 ++++++++++++------ src/Drivers/DebugDriver.ts | 16 ++++++++++------ src/Drivers/FileDriver.ts | 17 ++++++++++------- src/Logger.ts | 22 ++++++++++++++++------ src/utils/getConfigFile.ts | 8 ++++++++ 7 files changed, 67 insertions(+), 50 deletions(-) create mode 100644 src/utils/getConfigFile.ts diff --git a/package-lock.json b/package-lock.json index c707304..78e8fab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,22 +1,21 @@ { "name": "@secjs/logger", - "version": "1.2.2", + "version": "1.2.6", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@secjs/logger", - "version": "1.2.2", + "version": "1.2.6", "license": "MIT", "dependencies": { "chalk": "4.1.1", "debug": "4.3.1" }, "devDependencies": { - "@secjs/config": "1.0.9", "@secjs/env": "1.2.8", "@secjs/exceptions": "1.0.4", - "@secjs/utils": "1.5.4", + "@secjs/utils": "1.5.8", "@types/debug": "4.1.5", "@types/jest": "27.0.1", "@types/node": "14.14.37", @@ -1298,12 +1297,6 @@ "node": ">= 8" } }, - "node_modules/@secjs/config": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/@secjs/config/-/config-1.0.9.tgz", - "integrity": "sha512-5eB/UoZBPG1cgcUBmoXSpBUU/SLnT6dhjuWuIuyrfWRtFODY7S3zAoaBHzHYWtEuwjhoAfCvjIziZme7C8+/bg==", - "dev": true - }, "node_modules/@secjs/env": { "version": "1.2.8", "resolved": "https://registry.npmjs.org/@secjs/env/-/env-1.2.8.tgz", @@ -1320,9 +1313,9 @@ "dev": true }, "node_modules/@secjs/utils": { - "version": "1.5.4", - "resolved": "https://registry.npmjs.org/@secjs/utils/-/utils-1.5.4.tgz", - "integrity": "sha512-43hDBBoOI632oKPmf0LlkflOB1CLYcBEm3Q4F3Vw9ArJp8b5MDfW7LHGC4k8U6ucJsJoptbnvI/X4fW6MHQnFw==", + "version": "1.5.8", + "resolved": "https://registry.npmjs.org/@secjs/utils/-/utils-1.5.8.tgz", + "integrity": "sha512-PZ9xHS8M1390XP4HKXw0mVOcc/UzYX5By28XppnBJglMbWAZ9zwhB9Ftf+w33nqRoxDmjaOYFa9cMIVF8TWnbA==", "dev": true, "dependencies": { "mime-types": "2.1.34" @@ -9602,12 +9595,6 @@ "fastq": "^1.6.0" } }, - "@secjs/config": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/@secjs/config/-/config-1.0.9.tgz", - "integrity": "sha512-5eB/UoZBPG1cgcUBmoXSpBUU/SLnT6dhjuWuIuyrfWRtFODY7S3zAoaBHzHYWtEuwjhoAfCvjIziZme7C8+/bg==", - "dev": true - }, "@secjs/env": { "version": "1.2.8", "resolved": "https://registry.npmjs.org/@secjs/env/-/env-1.2.8.tgz", @@ -9624,9 +9611,9 @@ "dev": true }, "@secjs/utils": { - "version": "1.5.4", - "resolved": "https://registry.npmjs.org/@secjs/utils/-/utils-1.5.4.tgz", - "integrity": "sha512-43hDBBoOI632oKPmf0LlkflOB1CLYcBEm3Q4F3Vw9ArJp8b5MDfW7LHGC4k8U6ucJsJoptbnvI/X4fW6MHQnFw==", + "version": "1.5.8", + "resolved": "https://registry.npmjs.org/@secjs/utils/-/utils-1.5.8.tgz", + "integrity": "sha512-PZ9xHS8M1390XP4HKXw0mVOcc/UzYX5By28XppnBJglMbWAZ9zwhB9Ftf+w33nqRoxDmjaOYFa9cMIVF8TWnbA==", "dev": true, "requires": { "mime-types": "2.1.34" diff --git a/package.json b/package.json index 77fa43d..6fb1203 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@secjs/logger", - "version": "1.2.5", + "version": "1.2.6", "description": "", "license": "MIT", "author": "João Lenon", @@ -22,10 +22,9 @@ "debug": "4.3.1" }, "devDependencies": { - "@secjs/config": "1.0.9", "@secjs/env": "1.2.8", "@secjs/exceptions": "1.0.4", - "@secjs/utils": "1.5.4", + "@secjs/utils": "1.5.8", "@types/debug": "4.1.5", "@types/jest": "27.0.1", "@types/node": "14.14.37", diff --git a/src/Drivers/ConsoleDriver.ts b/src/Drivers/ConsoleDriver.ts index 1670085..e462c2c 100644 --- a/src/Drivers/ConsoleDriver.ts +++ b/src/Drivers/ConsoleDriver.ts @@ -1,7 +1,9 @@ -import { Config } from '@secjs/config' +import { Env } from '@secjs/env' import { Color } from '../utils/Color' import { format } from '../utils/format' +import { File, Path } from '@secjs/utils' import { DriverContract } from '../Contracts/DriverContract' +import { getConfigFile } from '../utils/getConfigFile' export interface ConsoleDriverOpts { color: Color @@ -17,12 +19,14 @@ export class ConsoleDriver implements DriverContract { private readonly _streamType: string constructor(channel: string) { - const config = Config.get(`logging.channels.${channel}`) || {} + const configFile = getConfigFile() - this._level = config.level || 'INFO' - this._context = config.context || 'ConsoleDriver' - this._formatter = config.formatter || 'context' - this._streamType = config.streamType || 'stdout' + const channelConfig = configFile.channels[channel] + + this._level = channelConfig.level || 'INFO' + this._context = channelConfig.context || 'ConsoleDriver' + this._formatter = channelConfig.formatter || 'context' + this._streamType = channelConfig.streamType || 'stdout' } transport(message: string, options?: ConsoleDriverOpts): void { @@ -41,3 +45,5 @@ export class ConsoleDriver implements DriverContract { process[this._streamType].write(`${message}\n`) } } + +new ConsoleDriver('debug') diff --git a/src/Drivers/DebugDriver.ts b/src/Drivers/DebugDriver.ts index 07d356d..3c04a86 100644 --- a/src/Drivers/DebugDriver.ts +++ b/src/Drivers/DebugDriver.ts @@ -1,8 +1,10 @@ import { debug } from 'debug' +import { Env } from '@secjs/env' import { Color } from '../utils/Color' -import { Config } from '@secjs/config' import { format } from '../utils/format' +import { File, Path } from '@secjs/utils' import { DriverContract } from '../Contracts/DriverContract' +import { getConfigFile } from '../utils/getConfigFile' export interface DebugDriverOpts { color: Color @@ -19,12 +21,14 @@ export class DebugDriver implements DriverContract { private readonly _namespace: string constructor(channel: string) { - const config = Config.get(`logging.channels.${channel}`) || {} + const configFile = getConfigFile() - this._level = config.level || 'DEBUG' - this._context = config.context || 'DebugDriver' - this._formatter = config.formatter || 'context' - this._namespace = config.namespace || 'api:main' + const channelConfig = configFile.channels[channel] + + this._level = channelConfig.level || 'DEBUG' + this._context = channelConfig.context || 'DebugDriver' + this._formatter = channelConfig.formatter || 'context' + this._namespace = channelConfig.namespace || 'api:main' } transport(message: string, options?: DebugDriverOpts): void { diff --git a/src/Drivers/FileDriver.ts b/src/Drivers/FileDriver.ts index 09ba05a..28b9c83 100644 --- a/src/Drivers/FileDriver.ts +++ b/src/Drivers/FileDriver.ts @@ -1,9 +1,10 @@ import { parse } from 'path' -import { Path } from '@secjs/utils' +import { Env } from '@secjs/env' import { Color } from '../utils/Color' -import { Config } from '@secjs/config' +import { File, Path } from '@secjs/utils' import { DriverContract } from '../Contracts/DriverContract' import { createWriteStream, existsSync, mkdirSync } from 'fs' +import { getConfigFile } from '../utils/getConfigFile' export interface FileDriverOpts { level: string @@ -19,12 +20,14 @@ export class FileDriver implements DriverContract { private readonly _formatter: string constructor(channel: string) { - const config = Config.get(`logging.channels.${channel}`) || {} + const configFile = getConfigFile() - this._level = config.level || 'INFO' - this._context = config.context || 'FileDriver' - this._filePath = config.filePath || Path.noBuild().logs('secjs.log') - this._formatter = config.formatter || 'log' + const channelConfig = configFile.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' } transport(message: string, options?: FileDriverOpts): void { diff --git a/src/Logger.ts b/src/Logger.ts index fa53494..c9c3846 100644 --- a/src/Logger.ts +++ b/src/Logger.ts @@ -3,9 +3,9 @@ import { NotImplementedException, } from '@secjs/exceptions' -import { Config } from '@secjs/config' import { Color } from './utils/Color' import { Drivers } from './Drivers/Drivers' +import { getConfigFile } from './utils/getConfigFile' import { Formatters } from './Formatters/Formatters' import { DriverContract } from './Contracts/DriverContract' import { FormatterContract } from './Contracts/FormatterContract' @@ -41,8 +41,12 @@ export class Logger { constructor(options?: any) { this._options = options - const defaultChannel = Config.get('logging.default') - const channelConfig = Config.get(`logging.channels.${defaultChannel}`) + + const configFile = getConfigFile() + + const defaultChannel = configFile.default + const channelConfig = configFile.channels[defaultChannel] + const driver = this._options && this._options.driver ? this._options.driver @@ -64,7 +68,9 @@ export class Logger { } changeDefaultChannel(channel: string): Logger { - const channelConfig = Config.get(`logging.channels.${channel}`) + const configFile = getConfigFile() + + const channelConfig = configFile.channels[channel] if (!channelConfig) { throw new NotImplementedException( @@ -84,7 +90,9 @@ export class Logger { } channel(channel: string): Logger { - const channelConfig = Config.get(`logging.channels.${channel}`) + const configFile = getConfigFile() + + const channelConfig = configFile.channels[channel] if (!channelConfig) { throw new NotImplementedException( @@ -108,8 +116,10 @@ export class Logger { channels(...channels: string[]): Logger { this._tempDrivers = [] + const configFile = getConfigFile() + channels.forEach(channel => { - const channelConfig = Config.get(`logging.channels.${channel}`) + const channelConfig = configFile.channels[channel] if (!channelConfig) { throw new NotImplementedException( diff --git a/src/utils/getConfigFile.ts b/src/utils/getConfigFile.ts new file mode 100644 index 0000000..83a80a2 --- /dev/null +++ b/src/utils/getConfigFile.ts @@ -0,0 +1,8 @@ +import { Env } from '@secjs/env' +import { File, Path } from '@secjs/utils' + +export function getConfigFile() { + const extension = Env('NODE_TS', '') === 'true' ? 'ts' : 'js' + + return require(new File(Path.config(`logging.${extension}`)).path).default +}