Skip to content

Commit cfcbfc9

Browse files
committed
fix: remove @secjs/config dependency for less coupling
1 parent af49656 commit cfcbfc9

File tree

7 files changed

+67
-50
lines changed

7 files changed

+67
-50
lines changed

package-lock.json

Lines changed: 9 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/logger",
3-
"version": "1.2.5",
3+
"version": "1.2.6",
44
"description": "",
55
"license": "MIT",
66
"author": "João Lenon",
@@ -22,10 +22,9 @@
2222
"debug": "4.3.1"
2323
},
2424
"devDependencies": {
25-
"@secjs/config": "1.0.9",
2625
"@secjs/env": "1.2.8",
2726
"@secjs/exceptions": "1.0.4",
28-
"@secjs/utils": "1.5.4",
27+
"@secjs/utils": "1.5.8",
2928
"@types/debug": "4.1.5",
3029
"@types/jest": "27.0.1",
3130
"@types/node": "14.14.37",

src/Drivers/ConsoleDriver.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { Config } from '@secjs/config'
1+
import { Env } from '@secjs/env'
22
import { Color } from '../utils/Color'
33
import { format } from '../utils/format'
4+
import { File, Path } from '@secjs/utils'
45
import { DriverContract } from '../Contracts/DriverContract'
6+
import { getConfigFile } from '../utils/getConfigFile'
57

68
export interface ConsoleDriverOpts {
79
color: Color
@@ -17,12 +19,14 @@ export class ConsoleDriver implements DriverContract {
1719
private readonly _streamType: string
1820

1921
constructor(channel: string) {
20-
const config = Config.get(`logging.channels.${channel}`) || {}
22+
const configFile = getConfigFile()
2123

22-
this._level = config.level || 'INFO'
23-
this._context = config.context || 'ConsoleDriver'
24-
this._formatter = config.formatter || 'context'
25-
this._streamType = config.streamType || 'stdout'
24+
const channelConfig = configFile.channels[channel]
25+
26+
this._level = channelConfig.level || 'INFO'
27+
this._context = channelConfig.context || 'ConsoleDriver'
28+
this._formatter = channelConfig.formatter || 'context'
29+
this._streamType = channelConfig.streamType || 'stdout'
2630
}
2731

2832
transport(message: string, options?: ConsoleDriverOpts): void {
@@ -41,3 +45,5 @@ export class ConsoleDriver implements DriverContract {
4145
process[this._streamType].write(`${message}\n`)
4246
}
4347
}
48+
49+
new ConsoleDriver('debug')

src/Drivers/DebugDriver.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { debug } from 'debug'
2+
import { Env } from '@secjs/env'
23
import { Color } from '../utils/Color'
3-
import { Config } from '@secjs/config'
44
import { format } from '../utils/format'
5+
import { File, Path } from '@secjs/utils'
56
import { DriverContract } from '../Contracts/DriverContract'
7+
import { getConfigFile } from '../utils/getConfigFile'
68

79
export interface DebugDriverOpts {
810
color: Color
@@ -19,12 +21,14 @@ export class DebugDriver implements DriverContract {
1921
private readonly _namespace: string
2022

2123
constructor(channel: string) {
22-
const config = Config.get(`logging.channels.${channel}`) || {}
24+
const configFile = getConfigFile()
2325

24-
this._level = config.level || 'DEBUG'
25-
this._context = config.context || 'DebugDriver'
26-
this._formatter = config.formatter || 'context'
27-
this._namespace = config.namespace || 'api:main'
26+
const channelConfig = configFile.channels[channel]
27+
28+
this._level = channelConfig.level || 'DEBUG'
29+
this._context = channelConfig.context || 'DebugDriver'
30+
this._formatter = channelConfig.formatter || 'context'
31+
this._namespace = channelConfig.namespace || 'api:main'
2832
}
2933

3034
transport(message: string, options?: DebugDriverOpts): void {

src/Drivers/FileDriver.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { parse } from 'path'
2-
import { Path } from '@secjs/utils'
2+
import { Env } from '@secjs/env'
33
import { Color } from '../utils/Color'
4-
import { Config } from '@secjs/config'
4+
import { File, Path } from '@secjs/utils'
55
import { DriverContract } from '../Contracts/DriverContract'
66
import { createWriteStream, existsSync, mkdirSync } from 'fs'
7+
import { getConfigFile } from '../utils/getConfigFile'
78

89
export interface FileDriverOpts {
910
level: string
@@ -19,12 +20,14 @@ export class FileDriver implements DriverContract {
1920
private readonly _formatter: string
2021

2122
constructor(channel: string) {
22-
const config = Config.get(`logging.channels.${channel}`) || {}
23+
const configFile = getConfigFile()
2324

24-
this._level = config.level || 'INFO'
25-
this._context = config.context || 'FileDriver'
26-
this._filePath = config.filePath || Path.noBuild().logs('secjs.log')
27-
this._formatter = config.formatter || 'log'
25+
const channelConfig = configFile.channels[channel]
26+
27+
this._level = channelConfig.level || 'INFO'
28+
this._context = channelConfig.context || 'FileDriver'
29+
this._filePath = channelConfig.filePath || Path.noBuild().logs('secjs.log')
30+
this._formatter = channelConfig.formatter || 'log'
2831
}
2932

3033
transport(message: string, options?: FileDriverOpts): void {

src/Logger.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import {
33
NotImplementedException,
44
} from '@secjs/exceptions'
55

6-
import { Config } from '@secjs/config'
76
import { Color } from './utils/Color'
87
import { Drivers } from './Drivers/Drivers'
8+
import { getConfigFile } from './utils/getConfigFile'
99
import { Formatters } from './Formatters/Formatters'
1010
import { DriverContract } from './Contracts/DriverContract'
1111
import { FormatterContract } from './Contracts/FormatterContract'
@@ -41,8 +41,12 @@ export class Logger {
4141

4242
constructor(options?: any) {
4343
this._options = options
44-
const defaultChannel = Config.get('logging.default')
45-
const channelConfig = Config.get(`logging.channels.${defaultChannel}`)
44+
45+
const configFile = getConfigFile()
46+
47+
const defaultChannel = configFile.default
48+
const channelConfig = configFile.channels[defaultChannel]
49+
4650
const driver =
4751
this._options && this._options.driver
4852
? this._options.driver
@@ -64,7 +68,9 @@ export class Logger {
6468
}
6569

6670
changeDefaultChannel(channel: string): Logger {
67-
const channelConfig = Config.get(`logging.channels.${channel}`)
71+
const configFile = getConfigFile()
72+
73+
const channelConfig = configFile.channels[channel]
6874

6975
if (!channelConfig) {
7076
throw new NotImplementedException(
@@ -84,7 +90,9 @@ export class Logger {
8490
}
8591

8692
channel(channel: string): Logger {
87-
const channelConfig = Config.get(`logging.channels.${channel}`)
93+
const configFile = getConfigFile()
94+
95+
const channelConfig = configFile.channels[channel]
8896

8997
if (!channelConfig) {
9098
throw new NotImplementedException(
@@ -108,8 +116,10 @@ export class Logger {
108116
channels(...channels: string[]): Logger {
109117
this._tempDrivers = []
110118

119+
const configFile = getConfigFile()
120+
111121
channels.forEach(channel => {
112-
const channelConfig = Config.get(`logging.channels.${channel}`)
122+
const channelConfig = configFile.channels[channel]
113123

114124
if (!channelConfig) {
115125
throw new NotImplementedException(

src/utils/getConfigFile.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Env } from '@secjs/env'
2+
import { File, Path } from '@secjs/utils'
3+
4+
export function getConfigFile() {
5+
const extension = Env('NODE_TS', '') === 'true' ? 'ts' : 'js'
6+
7+
return require(new File(Path.config(`logging.${extension}`)).path).default
8+
}

0 commit comments

Comments
 (0)