Skip to content

Commit

Permalink
fix: remove @secjs/config dependency for less coupling
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Dec 7, 2021
1 parent af49656 commit cfcbfc9
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 50 deletions.
31 changes: 9 additions & 22 deletions package-lock.json

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

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@secjs/logger",
"version": "1.2.5",
"version": "1.2.6",
"description": "",
"license": "MIT",
"author": "João Lenon",
Expand All @@ -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",
Expand Down
18 changes: 12 additions & 6 deletions src/Drivers/ConsoleDriver.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {
Expand All @@ -41,3 +45,5 @@ export class ConsoleDriver implements DriverContract {
process[this._streamType].write(`${message}\n`)
}
}

new ConsoleDriver('debug')
16 changes: 10 additions & 6 deletions src/Drivers/DebugDriver.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {
Expand Down
17 changes: 10 additions & 7 deletions src/Drivers/FileDriver.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {
Expand Down
22 changes: 16 additions & 6 deletions src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions src/utils/getConfigFile.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit cfcbfc9

Please sign in to comment.