Skip to content

Commit cefa284

Browse files
authored
Merge pull request #14 from SecJS/refactor/len-remove-config-methods
refactor: remove config methods, tempDriver and changeDefaultChannel
2 parents cfcbfc9 + ac34c59 commit cefa284

File tree

9 files changed

+61
-179
lines changed

9 files changed

+61
-179
lines changed

README.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,12 @@ export default {
9292
> With the config/logging file created you can use Log and Logger classes to start logging.
9393
9494
```ts
95+
import { Config } from '@secjs/config'
9596
import { Log, Logger, Color } from '@secjs/logger'
9697

98+
// First you need to instantiate Config class and call loadSync method to load configuration files
99+
new Confg().loadSync()
100+
97101
// Log and Logger will always use the default values of channel inside config/logging, the default channel in here is "application".
98102
Log.log('Hello World!')
99103
// [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Logger] Hello World! +0ms
@@ -117,17 +121,6 @@ Log.channel('debug').log('Hello debug world!', { namespace: 'api:example' })
117121
// api:example [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Debugger] Hello debug world! +0ms
118122
```
119123

120-
> You can use many channels to handle the log in all of then
121-
122-
```ts
123-
Log.channels('debug', 'application', 'file').info('Hello World!', { namespace: 'api:example' })
124-
// api:example [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Debugger] Hello World! +0ms
125-
// [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Logger] Hello World! +0ms
126-
127-
// In storage/logs/secjs.log file
128-
// [SecJS] - PID: 196416 - dd/mm/yyyy, hh:mm:ss [INFO] Hello World!
129-
```
130-
131124
### Extending drivers, channels and formatters
132125

133126
> Nowadays, @secjs/logger has only FileDriver, DebugDriver and ConsoleDriver support, but you can extend the drivers for Logger class if you implement DriverContract interface.

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/logger",
3-
"version": "1.2.6",
3+
"version": "1.2.7",
44
"description": "",
55
"license": "MIT",
66
"author": "João Lenon",
@@ -22,6 +22,7 @@
2222
"debug": "4.3.1"
2323
},
2424
"devDependencies": {
25+
"@secjs/config": "1.1.3",
2526
"@secjs/env": "1.2.8",
2627
"@secjs/exceptions": "1.0.4",
2728
"@secjs/utils": "1.5.8",

src/Drivers/ConsoleDriver.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { Env } from '@secjs/env'
1+
import { Config } from '@secjs/config'
22
import { Color } from '../utils/Color'
33
import { format } from '../utils/format'
4-
import { File, Path } from '@secjs/utils'
54
import { DriverContract } from '../Contracts/DriverContract'
6-
import { getConfigFile } from '../utils/getConfigFile'
75

86
export interface ConsoleDriverOpts {
97
color: Color
@@ -19,9 +17,7 @@ export class ConsoleDriver implements DriverContract {
1917
private readonly _streamType: string
2018

2119
constructor(channel: string) {
22-
const configFile = getConfigFile()
23-
24-
const channelConfig = configFile.channels[channel]
20+
const channelConfig = Config.get(`logging.channels.${channel}`)
2521

2622
this._level = channelConfig.level || 'INFO'
2723
this._context = channelConfig.context || 'ConsoleDriver'
@@ -45,5 +41,3 @@ export class ConsoleDriver implements DriverContract {
4541
process[this._streamType].write(`${message}\n`)
4642
}
4743
}
48-
49-
new ConsoleDriver('debug')

src/Drivers/DebugDriver.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { debug } from 'debug'
2-
import { Env } from '@secjs/env'
2+
import { Config } from '@secjs/config'
33
import { Color } from '../utils/Color'
44
import { format } from '../utils/format'
5-
import { File, Path } from '@secjs/utils'
65
import { DriverContract } from '../Contracts/DriverContract'
7-
import { getConfigFile } from '../utils/getConfigFile'
86

97
export interface DebugDriverOpts {
108
color: Color
@@ -21,9 +19,7 @@ export class DebugDriver implements DriverContract {
2119
private readonly _namespace: string
2220

2321
constructor(channel: string) {
24-
const configFile = getConfigFile()
25-
26-
const channelConfig = configFile.channels[channel]
22+
const channelConfig = Config.get(`logging.channels.${channel}`)
2723

2824
this._level = channelConfig.level || 'DEBUG'
2925
this._context = channelConfig.context || 'DebugDriver'

src/Drivers/FileDriver.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { parse } from 'path'
2-
import { Env } from '@secjs/env'
2+
import { Path } from '@secjs/utils'
3+
import { Config } from '@secjs/config'
34
import { Color } from '../utils/Color'
4-
import { File, Path } from '@secjs/utils'
55
import { DriverContract } from '../Contracts/DriverContract'
66
import { createWriteStream, existsSync, mkdirSync } from 'fs'
7-
import { getConfigFile } from '../utils/getConfigFile'
87

98
export interface FileDriverOpts {
109
level: string
@@ -20,17 +19,15 @@ export class FileDriver implements DriverContract {
2019
private readonly _formatter: string
2120

2221
constructor(channel: string) {
23-
const configFile = getConfigFile()
24-
25-
const channelConfig = configFile.channels[channel]
22+
const channelConfig = Config.get(`logging.channels.${channel}`)
2623

2724
this._level = channelConfig.level || 'INFO'
2825
this._context = channelConfig.context || 'FileDriver'
2926
this._filePath = channelConfig.filePath || Path.noBuild().logs('secjs.log')
3027
this._formatter = channelConfig.formatter || 'log'
3128
}
3229

33-
transport(message: string, options?: FileDriverOpts): void {
30+
async transport(message: string, options?: FileDriverOpts): Promise<void> {
3431
options = Object.assign(
3532
{},
3633
{ level: this._level, context: this._context, filePath: this._filePath },
@@ -43,14 +40,13 @@ export class FileDriver implements DriverContract {
4340
mkdirSync(path.dir, { recursive: true })
4441
}
4542

46-
const stream = createWriteStream(options.filePath, { flags: 'a' })
43+
return new Promise((resolve, reject) => {
44+
const stream = createWriteStream(options.filePath, { flags: 'a' })
4745

48-
stream.write(`${Color.removeColors(message)}` + '\n')
46+
stream.write(`${Color.removeColors(message)}` + '\n')
4947

50-
stream.on('error', err => {
51-
throw err
48+
stream.on('error', reject)
49+
stream.end(resolve)
5250
})
53-
54-
stream.end()
5551
}
5652
}

src/Log.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ export class Log {
3939
return this
4040
}
4141

42-
static channels(...channels: string[]): typeof Log {
43-
this.logger.channels(...channels)
44-
45-
return this
46-
}
47-
4842
static log(message: any, options?: any) {
4943
options = {
5044
...options,

0 commit comments

Comments
 (0)