Skip to content

Commit

Permalink
Merge pull request #2 from AthennaIO/develop
Browse files Browse the repository at this point in the history
fix: add configs in drivers constructor
  • Loading branch information
jlenon7 authored Mar 30, 2022
2 parents b04450e + bec6cb3 commit eb71811
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/logger",
"version": "1.0.2",
"version": "1.0.3",
"description": "",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
14 changes: 8 additions & 6 deletions src/Drivers/ConsoleDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface ConsoleDriverOpts {
color: Color
level: string
context: string
formatter: string
streamType: string
}

Expand All @@ -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 {
Expand All @@ -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,
)
Expand Down
13 changes: 7 additions & 6 deletions src/Drivers/DebugDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
)
Expand Down
21 changes: 13 additions & 8 deletions src/Drivers/FileDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<void> {
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,
)

Expand All @@ -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,
)
Expand Down
4 changes: 4 additions & 0 deletions tests/Stubs/config/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit eb71811

Please sign in to comment.