Skip to content

Commit

Permalink
Merge pull request #14 from SecJS/refactor/len-remove-config-methods
Browse files Browse the repository at this point in the history
refactor: remove config methods, tempDriver and changeDefaultChannel
  • Loading branch information
jlenon7 authored Feb 18, 2022
2 parents cfcbfc9 + ac34c59 commit cefa284
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 179 deletions.
15 changes: 4 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ export default {
> With the config/logging file created you can use Log and Logger classes to start logging.
```ts
import { Config } from '@secjs/config'
import { Log, Logger, Color } from '@secjs/logger'

// First you need to instantiate Config class and call loadSync method to load configuration files
new Confg().loadSync()

// Log and Logger will always use the default values of channel inside config/logging, the default channel in here is "application".
Log.log('Hello World!')
// [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Logger] Hello World! +0ms
Expand All @@ -117,17 +121,6 @@ Log.channel('debug').log('Hello debug world!', { namespace: 'api:example' })
// api:example [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Debugger] Hello debug world! +0ms
```

> You can use many channels to handle the log in all of then
```ts
Log.channels('debug', 'application', 'file').info('Hello World!', { namespace: 'api:example' })
// api:example [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Debugger] Hello World! +0ms
// [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Logger] Hello World! +0ms

// In storage/logs/secjs.log file
// [SecJS] - PID: 196416 - dd/mm/yyyy, hh:mm:ss [INFO] Hello World!
```

### Extending drivers, channels and formatters

> Nowadays, @secjs/logger has only FileDriver, DebugDriver and ConsoleDriver support, but you can extend the drivers for Logger class if you implement DriverContract interface.
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@secjs/logger",
"version": "1.2.6",
"version": "1.2.7",
"description": "",
"license": "MIT",
"author": "João Lenon",
Expand All @@ -22,6 +22,7 @@
"debug": "4.3.1"
},
"devDependencies": {
"@secjs/config": "1.1.3",
"@secjs/env": "1.2.8",
"@secjs/exceptions": "1.0.4",
"@secjs/utils": "1.5.8",
Expand Down
10 changes: 2 additions & 8 deletions src/Drivers/ConsoleDriver.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Env } from '@secjs/env'
import { Config } from '@secjs/config'
import { Color } from '../utils/Color'
import { format } from '../utils/format'
import { File, Path } from '@secjs/utils'
import { DriverContract } from '../Contracts/DriverContract'
import { getConfigFile } from '../utils/getConfigFile'

export interface ConsoleDriverOpts {
color: Color
Expand All @@ -19,9 +17,7 @@ export class ConsoleDriver implements DriverContract {
private readonly _streamType: string

constructor(channel: string) {
const configFile = getConfigFile()

const channelConfig = configFile.channels[channel]
const channelConfig = Config.get(`logging.channels.${channel}`)

this._level = channelConfig.level || 'INFO'
this._context = channelConfig.context || 'ConsoleDriver'
Expand All @@ -45,5 +41,3 @@ export class ConsoleDriver implements DriverContract {
process[this._streamType].write(`${message}\n`)
}
}

new ConsoleDriver('debug')
8 changes: 2 additions & 6 deletions src/Drivers/DebugDriver.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { debug } from 'debug'
import { Env } from '@secjs/env'
import { Config } from '@secjs/config'
import { Color } from '../utils/Color'
import { format } from '../utils/format'
import { File, Path } from '@secjs/utils'
import { DriverContract } from '../Contracts/DriverContract'
import { getConfigFile } from '../utils/getConfigFile'

export interface DebugDriverOpts {
color: Color
Expand All @@ -21,9 +19,7 @@ export class DebugDriver implements DriverContract {
private readonly _namespace: string

constructor(channel: string) {
const configFile = getConfigFile()

const channelConfig = configFile.channels[channel]
const channelConfig = Config.get(`logging.channels.${channel}`)

this._level = channelConfig.level || 'DEBUG'
this._context = channelConfig.context || 'DebugDriver'
Expand Down
22 changes: 9 additions & 13 deletions src/Drivers/FileDriver.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { parse } from 'path'
import { Env } from '@secjs/env'
import { Path } from '@secjs/utils'
import { Config } from '@secjs/config'
import { Color } from '../utils/Color'
import { File, Path } from '@secjs/utils'
import { DriverContract } from '../Contracts/DriverContract'
import { createWriteStream, existsSync, mkdirSync } from 'fs'
import { getConfigFile } from '../utils/getConfigFile'

export interface FileDriverOpts {
level: string
Expand All @@ -20,17 +19,15 @@ export class FileDriver implements DriverContract {
private readonly _formatter: string

constructor(channel: string) {
const configFile = getConfigFile()

const channelConfig = configFile.channels[channel]
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'
}

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

const stream = createWriteStream(options.filePath, { flags: 'a' })
return new Promise((resolve, reject) => {
const stream = createWriteStream(options.filePath, { flags: 'a' })

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

stream.on('error', err => {
throw err
stream.on('error', reject)
stream.end(resolve)
})

stream.end()
}
}
6 changes: 0 additions & 6 deletions src/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ export class Log {
return this
}

static channels(...channels: string[]): typeof Log {
this.logger.channels(...channels)

return this
}

static log(message: any, options?: any) {
options = {
...options,
Expand Down
Loading

0 comments on commit cefa284

Please sign in to comment.