Skip to content

Commit

Permalink
feat: add options to constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Dec 6, 2021
1 parent 4358479 commit ba353f5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@secjs/logger",
"version": "1.2.3",
"version": "1.2.4",
"description": "",
"license": "MIT",
"author": "João Lenon",
Expand Down
35 changes: 35 additions & 0 deletions src/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import { DriverContract } from './Contracts/DriverContract'
import { FormatterContract } from './Contracts/FormatterContract'

export class Log {
private static _options?: any = {}
private static logger: Logger = new Logger()

static options(options?: any) {
this._options = options
}

static buildDriver(name: string, driver: DriverContract): typeof Log {
Logger.buildDriver(name, driver)

Expand Down Expand Up @@ -41,36 +46,66 @@ export class Log {
}

static log(message: any, options?: any) {
options = {
...options,
...this._options,
}

this.logger.log(message, options)

this.logger = new Logger()
}

static info(message: any, options?: any) {
options = {
...options,
...this._options,
}

this.logger.info(message, options)

this.logger = new Logger()
}

static warn(message: any, options?: any) {
options = {
...options,
...this._options,
}

this.logger.warn(message, options)

this.logger = new Logger()
}

static error(message: any, options?: any) {
options = {
...options,
...this._options,
}

this.logger.error(message, options)

this.logger = new Logger()
}

static debug(message: any, options?: any) {
options = {
...options,
...this._options,
}

this.logger.debug(message, options)

this.logger = new Logger()
}

static success(message: any, options?: any) {
options = {
...options,
...this._options,
}

this.logger.success(message, options)

this.logger = new Logger()
Expand Down
30 changes: 28 additions & 2 deletions src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { DriverContract } from './Contracts/DriverContract'
import { FormatterContract } from './Contracts/FormatterContract'

export class Logger {
private readonly _options?: any = {}
private _tempDrivers: DriverContract[] | null = null
private _defaultDriver: DriverContract | null = null

Expand Down Expand Up @@ -38,11 +39,16 @@ export class Logger {
return Object.keys(Formatters)
}

constructor() {
constructor(options?: any) {
this._options = options
const defaultChannel = Config.get('logging.default')
const channelConfig = Config.get(`logging.channels.${defaultChannel}`)
const driver =
this._options && this._options.driver
? this._options.driver
: channelConfig.driver

this._defaultDriver = new Drivers[channelConfig.driver](defaultChannel)
this._defaultDriver = new Drivers[driver](defaultChannel)
}

private _driver(message: any, options?: any) {
Expand Down Expand Up @@ -137,6 +143,10 @@ export class Logger {
options.level = 'INFO'
options.color = Color.cyan
options.streamType = 'stdout'
options = {
...options,
...this._options,
}

this._driver(message, options)

Expand All @@ -149,6 +159,10 @@ export class Logger {
options.level = 'WARN'
options.color = Color.orange
options.streamType = 'stdout'
options = {
...options,
...this._options,
}

this._driver(message, options)

Expand All @@ -161,6 +175,10 @@ export class Logger {
options.level = 'ERROR'
options.color = Color.red
options.streamType = 'stderr'
options = {
...options,
...this._options,
}

this._driver(message, options)

Expand All @@ -173,6 +191,10 @@ export class Logger {
options.level = 'DEBUG'
options.color = Color.purple
options.streamType = 'stdout'
options = {
...options,
...this._options,
}

this._driver(message, options)

Expand All @@ -185,6 +207,10 @@ export class Logger {
options.level = 'SUCCESS'
options.color = Color.green
options.streamType = 'stdout'
options = {
...options,
...this._options,
}

this._driver(message, options)

Expand Down

0 comments on commit ba353f5

Please sign in to comment.