Skip to content

Commit fbd5f6b

Browse files
authored
Merge pull request #11 from SecJS/refactor/len-driver-support
feat: add options to constructor
2 parents d982ace + ba353f5 commit fbd5f6b

3 files changed

Lines changed: 64 additions & 3 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/logger",
3-
"version": "1.2.3",
3+
"version": "1.2.4",
44
"description": "",
55
"license": "MIT",
66
"author": "João Lenon",

src/Log.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ import { DriverContract } from './Contracts/DriverContract'
33
import { FormatterContract } from './Contracts/FormatterContract'
44

55
export class Log {
6+
private static _options?: any = {}
67
private static logger: Logger = new Logger()
78

9+
static options(options?: any) {
10+
this._options = options
11+
}
12+
813
static buildDriver(name: string, driver: DriverContract): typeof Log {
914
Logger.buildDriver(name, driver)
1015

@@ -41,36 +46,66 @@ export class Log {
4146
}
4247

4348
static log(message: any, options?: any) {
49+
options = {
50+
...options,
51+
...this._options,
52+
}
53+
4454
this.logger.log(message, options)
4555

4656
this.logger = new Logger()
4757
}
4858

4959
static info(message: any, options?: any) {
60+
options = {
61+
...options,
62+
...this._options,
63+
}
64+
5065
this.logger.info(message, options)
5166

5267
this.logger = new Logger()
5368
}
5469

5570
static warn(message: any, options?: any) {
71+
options = {
72+
...options,
73+
...this._options,
74+
}
75+
5676
this.logger.warn(message, options)
5777

5878
this.logger = new Logger()
5979
}
6080

6181
static error(message: any, options?: any) {
82+
options = {
83+
...options,
84+
...this._options,
85+
}
86+
6287
this.logger.error(message, options)
6388

6489
this.logger = new Logger()
6590
}
6691

6792
static debug(message: any, options?: any) {
93+
options = {
94+
...options,
95+
...this._options,
96+
}
97+
6898
this.logger.debug(message, options)
6999

70100
this.logger = new Logger()
71101
}
72102

73103
static success(message: any, options?: any) {
104+
options = {
105+
...options,
106+
...this._options,
107+
}
108+
74109
this.logger.success(message, options)
75110

76111
this.logger = new Logger()

src/Logger.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { DriverContract } from './Contracts/DriverContract'
1111
import { FormatterContract } from './Contracts/FormatterContract'
1212

1313
export class Logger {
14+
private readonly _options?: any = {}
1415
private _tempDrivers: DriverContract[] | null = null
1516
private _defaultDriver: DriverContract | null = null
1617

@@ -38,11 +39,16 @@ export class Logger {
3839
return Object.keys(Formatters)
3940
}
4041

41-
constructor() {
42+
constructor(options?: any) {
43+
this._options = options
4244
const defaultChannel = Config.get('logging.default')
4345
const channelConfig = Config.get(`logging.channels.${defaultChannel}`)
46+
const driver =
47+
this._options && this._options.driver
48+
? this._options.driver
49+
: channelConfig.driver
4450

45-
this._defaultDriver = new Drivers[channelConfig.driver](defaultChannel)
51+
this._defaultDriver = new Drivers[driver](defaultChannel)
4652
}
4753

4854
private _driver(message: any, options?: any) {
@@ -137,6 +143,10 @@ export class Logger {
137143
options.level = 'INFO'
138144
options.color = Color.cyan
139145
options.streamType = 'stdout'
146+
options = {
147+
...options,
148+
...this._options,
149+
}
140150

141151
this._driver(message, options)
142152

@@ -149,6 +159,10 @@ export class Logger {
149159
options.level = 'WARN'
150160
options.color = Color.orange
151161
options.streamType = 'stdout'
162+
options = {
163+
...options,
164+
...this._options,
165+
}
152166

153167
this._driver(message, options)
154168

@@ -161,6 +175,10 @@ export class Logger {
161175
options.level = 'ERROR'
162176
options.color = Color.red
163177
options.streamType = 'stderr'
178+
options = {
179+
...options,
180+
...this._options,
181+
}
164182

165183
this._driver(message, options)
166184

@@ -173,6 +191,10 @@ export class Logger {
173191
options.level = 'DEBUG'
174192
options.color = Color.purple
175193
options.streamType = 'stdout'
194+
options = {
195+
...options,
196+
...this._options,
197+
}
176198

177199
this._driver(message, options)
178200

@@ -185,6 +207,10 @@ export class Logger {
185207
options.level = 'SUCCESS'
186208
options.color = Color.green
187209
options.streamType = 'stdout'
210+
options = {
211+
...options,
212+
...this._options,
213+
}
188214

189215
this._driver(message, options)
190216

0 commit comments

Comments
 (0)