Skip to content

Commit

Permalink
Merge pull request #58 from AthennaIO/develop
Browse files Browse the repository at this point in the history
fix(vanilla): use vanilla logger in the main instance
  • Loading branch information
jlenon7 authored Feb 6, 2023
2 parents 037b440 + 3c49453 commit f3fd3db
Show file tree
Hide file tree
Showing 15 changed files with 196 additions and 150 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/logger",
"version": "3.1.1",
"version": "3.1.2",
"description": "The Athenna logging solution. Log in stdout, files and buckets.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
43 changes: 43 additions & 0 deletions src/Constants/VanillaChannels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @athenna/logger
*
* (c) João Lenon <lenon@athenna.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* Athenna default vanilla channels. This configurations
* will be used by the "channelOrVanilla" method. If the
* configuration does not exist, than the vanilla will be set.
*/
export const VANILLA_CHANNELS = {
default: {
driver: 'stack',
channels: ['application'],
},
stack: {
driver: 'stack',
channels: ['application'],
},
discard: {
driver: 'null',
},
console: {
level: 'trace',
formatter: 'cli',
driver: 'console',
},
exception: {
level: 'trace',
formatter: 'none',
driver: 'console',
streamType: 'stderr',
},
application: {
level: 'trace',
driver: 'console',
formatter: 'simple',
},
}
8 changes: 5 additions & 3 deletions src/Exceptions/NotImplementedConfigException.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
* file that was distributed with this source code.
*/

import { Path, Exception } from '@athenna/common'
import { Path, Exception, Is } from '@athenna/common'

export class NotImplementedConfigException extends Exception {
constructor(channelName: string, channels?: any[]) {
constructor(channelName: string) {
let help = ''

if (channels && channels.length) {
const channels = Config.get('logging.channels')

if (channels && !Is.Empty(channels)) {
const availableConfigs = Object.keys(channels).join(', ')

help += `Available configurations are: ${availableConfigs}.`
Expand Down
2 changes: 1 addition & 1 deletion src/Factories/DriverFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class DriverFactory {
*/
public static getChannelConfig(channelName: string): any {
if (channelName === 'default') {
channelName = Config.get('logging.default') || channelName
channelName = Config.get('logging.default', channelName)
}

const channelConfig = Config.get(`logging.channels.${channelName}`)
Expand Down
4 changes: 2 additions & 2 deletions src/Formatters/Formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export abstract class Formatter {
const level = this.configs.level

if (!Color[level]) {
return level
return Color.bold(`[ ${level} ]`)
}

return Color[level].bold(`[ ${level} ]`)
Expand All @@ -154,7 +154,7 @@ export abstract class Formatter {
const level = this.configs.level

if (!Color[level]) {
return level
return Color.bold(`[${level.toUpperCase()}]`)
}

return Color[level].bold(`[${level.toUpperCase()}]`)
Expand Down
2 changes: 1 addition & 1 deletion src/Formatters/NoneFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import { Formatter } from '#src/Formatters/Formatter'

export class NoneFormatter extends Formatter {
public format(message: string): string {
return this.clean(this.toString(message))
return this.clean(this.applyColorsByChalk(this.toString(message)))
}
}
97 changes: 74 additions & 23 deletions src/Logger/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import { Color } from '@athenna/common'
import { Config } from '@athenna/config'
import { Driver } from '#src/Drivers/Driver'
import { VanillaLogger } from '#src/Logger/VanillaLogger'
import { DriverFactory } from '#src/Factories/DriverFactory'
import { VANILLA_CHANNELS } from '../Constants/VanillaChannels.js'

export class Logger {
/**
Expand All @@ -25,18 +25,27 @@ export class Logger {
private runtimeConfigs = {}

public constructor() {
if (!Config.exists(`logging.channels.${Config.get('logging.default')}`)) {
return this
}
this.channelOrVanilla(Config.get('logging.default'))
}

/**
* Create a new standalone logger instance. Very
* useful to create new loggers without changing the
* channels that are already defined in the main instance.
*/
public static standalone(...configs: any[]) {
const logger = new Logger()

this.drivers.push(DriverFactory.fabricate('default', this.runtimeConfigs))
logger.vanilla(...configs)

return logger
}

/**
* Set runtime configurations for drivers and
* formatters.
*/
public config(runtimeConfigs: any) {
public config(runtimeConfigs: any): Logger {
this.runtimeConfigs = runtimeConfigs

return this
Expand All @@ -45,27 +54,66 @@ export class Logger {
/**
* Change the log channel.
*/
public channel(...channels: string[]) {
public channel(...channels: string[]): Logger {
this.drivers = []

channels.forEach(c => {
this.drivers.push(DriverFactory.fabricate(c, this.runtimeConfigs))
channels.forEach(channel => {
this.drivers.push(DriverFactory.fabricate(channel, this.runtimeConfigs))
})

return this
}

/**
* Call drivers to transport the log.
* Change the log drivers using vanilla configurations.
* This method does not depend in Athenna configuration
* files to be executed.
*/
private async log(level: string, ...args: any[]): Promise<any> {
const message = Color.apply(...args)
public vanilla(...configs: any[]): Logger {
this.drivers = []

const promises = this.drivers.map((driver: Driver) =>
driver.transport(level, message),
)
if (!configs.length) {
this.drivers.push(DriverFactory.fabricateVanilla())

return Promise.all(promises)
return this
}

configs.forEach(config => {
this.drivers.push(DriverFactory.fabricateVanilla(config))
})

return this
}

/**
* Verify if channel configuration exists. If not, Athenna will
* use the default vanilla configurations as drivers.
*/
public channelOrVanilla(channel: string, configs = {}): Logger {
if (channel === 'default') {
channel = Config.get(
`logging.channels.${Config.get('logging.default')}`,
'default',
)
}

if (Config.exists(`logging.channels.${channel}`)) {
return this.channel(channel)
}

return this.vanilla({
...VANILLA_CHANNELS[channel],
...configs,
})
}

/**
* Create a new standalone logger instance. Very
* useful to create new loggers without changing the
* channels that are already defined in the main instance.
*/
public standalone(...configs: any[]) {
return Logger.standalone(configs)
}

/**
Expand All @@ -78,7 +126,7 @@ export class Logger {
/**
* Creates a log of type debug in channel.
*/
public debug(...args): any | Promise<any> {
public debug(...args: any[]): any | Promise<any> {
return this.log('debug', ...args)
}

Expand Down Expand Up @@ -118,12 +166,15 @@ export class Logger {
}

/**
* Get a new instance of any log driver
* with vanilla configurations. By default,
* vanilla logger will use the "console" driver
* and "none" formatter.
* Call drivers to transport the log.
*/
public static getVanillaLogger(configs: any = {}): VanillaLogger {
return new VanillaLogger(configs)
private async log(level: string, ...args: any[]): Promise<any> {
const message = Color.apply(...args)

const promises = this.drivers.map((driver: Driver) =>
driver.transport(level, message),
)

return Promise.all(promises)
}
}
112 changes: 0 additions & 112 deletions src/Logger/VanillaLogger.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export * from './Facades/Log.js'
export * from './Logger/Logger.js'
export * from './Drivers/Driver.js'
export * from './Formatters/Formatter.js'
export * from './Logger/VanillaLogger.js'
export * from './Helpers/FactoryHelper.js'
export * from './Factories/DriverFactory.js'
export * from './Providers/LoggerProvider.js'
Expand Down
Loading

0 comments on commit f3fd3db

Please sign in to comment.