From ba353f5cc0f0cc64a9329d45a8e666b7f3f2445a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Mon, 6 Dec 2021 11:38:49 -0300 Subject: [PATCH] feat: add options to constructor --- package.json | 2 +- src/Log.ts | 35 +++++++++++++++++++++++++++++++++++ src/Logger.ts | 30 ++++++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 6ff75e1..24b2b33 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@secjs/logger", - "version": "1.2.3", + "version": "1.2.4", "description": "", "license": "MIT", "author": "João Lenon", diff --git a/src/Log.ts b/src/Log.ts index ab0bfc7..56eca25 100644 --- a/src/Log.ts +++ b/src/Log.ts @@ -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) @@ -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() diff --git a/src/Logger.ts b/src/Logger.ts index 7133320..fa53494 100644 --- a/src/Logger.ts +++ b/src/Logger.ts @@ -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 @@ -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) { @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)