Skip to content

Commit

Permalink
Merge pull request #54 from AthennaIO/develop
Browse files Browse the repository at this point in the history
fix(types): add missing types
  • Loading branch information
jlenon7 authored Jan 15, 2023
2 parents 48d76df + 1339ecc commit ab9d1eb
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 55 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.0.7",
"version": "3.0.8",
"description": "The Athenna logging solution. Log in stdout, files and buckets.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
17 changes: 11 additions & 6 deletions src/Factories/DriverFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { TelegramDriver } from '#src/Drivers/TelegramDriver'
import { DriverExistException } from '#src/Exceptions/DriverExistException'
import { NotFoundDriverException } from '#src/Exceptions/NotFoundDriverException'
import { NotImplementedConfigException } from '#src/Exceptions/NotImplementedConfigException'
import { Options } from '@athenna/common'

export class DriverFactory {
/**
Expand Down Expand Up @@ -70,17 +71,21 @@ export class DriverFactory {
}

/**
* Fabricate a new instance of a driver without
* Fabricate a new instance of a driver with vanilla
* configurations.
*
* @param {string} driverName
* @param {any} runtimeConfig
* @param {any} configs
* @return {any}
*/
static fabricateOnly(driverName, runtimeConfig = {}) {
const { Driver } = this.#drivers.get(driverName)
static fabricateVanilla(configs = {}) {
configs = Options.create(configs, {
driver: 'console',
formatter: 'none',
})

const { Driver } = this.#drivers.get(configs.driver)

return new Driver(runtimeConfig)
return new Driver(configs)
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/Helpers/ConsoleLogger.js → src/Helpers/VanillaLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { ColorHelper } from '#src/index'
import { DriverFactory } from '#src/Factories/DriverFactory'

export class ConsoleLogger {
export class VanillaLogger {
/**
* The driver responsible for transporting the logs.
*
Expand All @@ -21,18 +21,18 @@ export class ConsoleLogger {
/**
* Creates a new instance of ConsoleLogger.
*
* @param {any} [runtimeConfigs]
* @return {ConsoleLogger}
* @param {any} [configs]
* @return {VanillaLogger}
*/
constructor(runtimeConfigs) {
this.#drivers.push(DriverFactory.fabricateOnly('console', runtimeConfigs))
constructor(configs) {
this.#drivers.push(DriverFactory.fabricateVanilla(configs))
}

/**
* Set runtime configurations for drivers and
* formatters.
*
* @return {ConsoleLogger}
* @return {VanillaLogger}
*/
config() {
return this
Expand All @@ -41,7 +41,7 @@ export class ConsoleLogger {
/**
* Change the log channel.
*
* @return {ConsoleLogger}
* @return {VanillaLogger}
*/
channel() {
return this
Expand Down
62 changes: 53 additions & 9 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,24 @@ export class FormatterFactory {
static createFormatter(name: string, formatter: () => any): void
}

export class ConsoleLogger {
export class VanillaLogger {
/**
* Set runtime configurations for drivers and
* formatters.
*
* @param {any} runtimeConfig
* @return {VanillaLogger}
*/
config(runtimeConfig: any): VanillaLogger

/**
* Change the log channel.
*
* @param {string[]} channels
* @return {VanillaLogger}
*/
channel(...channels: string[]): VanillaLogger

/**
* Creates a log of type trace in channel.
*
Expand Down Expand Up @@ -619,6 +636,22 @@ export class ConsoleLogger {
* @return {any | Promise<any>}
*/
error(...args: string[] | any[]): any | Promise<any>

/**
* Creates a log of type fatal in channel.
*
* @param {string|any} message
* @return {any | Promise<any>}
*/
fatal(message: string | any): any | Promise<any>

/**
* Creates a log of type fatal in channel.
*
* @param {string[]|any[]} args
* @return {any | Promise<any>}
*/
fatal(...args: string[] | any[]): any | Promise<any>
}

export class Logger {
Expand Down Expand Up @@ -736,18 +769,29 @@ export class Logger {
error(...args: string[] | any[]): any | Promise<any>

/**
* Get a new instance of the ConsoleLogger.
* Creates a log of type fatal in channel.
*
* @param {any} [runtimeConfigs]
* @return {ConsoleLogger}
* @param {string|any} message
* @return {any | Promise<any>}
*/
fatal(message: string | any): any | Promise<any>

/**
* Creates a log of type fatal in channel.
*
* @param {string[]|any[]} args
* @return {any | Promise<any>}
*/
getConsoleLogger(runtimeConfigs?: any): ConsoleLogger
fatal(...args: string[] | any[]): any | Promise<any>

/**
* Get a new instance of the ConsoleLogger.
* Get a new instance of any log driver
* with vanilla configurations. By default,
* vanilla logger will use the "console" driver
* and "none" formatter.
*
* @param {any} [runtimeConfigs]
* @return {ConsoleLogger}
* @param {any} [configs]
* @return {VanillaLogger}
*/
static getConsoleLogger(runtimeConfigs?: any): ConsoleLogger
static getVanillaLogger(configs?: any): VanillaLogger
}
27 changes: 10 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
*/

import { ColorHelper } from '#src/index'
import { ConsoleLogger } from '#src/Helpers/ConsoleLogger'
import { VanillaLogger } from '#src/Helpers/VanillaLogger'
import { DriverFactory } from '#src/Factories/DriverFactory'

export * from './Facades/Log.js'

export * from './Helpers/ColorHelper.js'
export * from './Helpers/ConsoleLogger.js'
export * from './Helpers/VanillaLogger.js'
export * from './Helpers/FactoryHelper.js'

export * from './Drivers/Driver.js'
Expand Down Expand Up @@ -166,22 +166,15 @@ export class Logger {
}

/**
* Get a new instance of the ConsoleLogger.
* Get a new instance of any log driver
* with vanilla configurations. By default,
* vanilla logger will use the "console" driver
* and "none" formatter.
*
* @param {any} [runtimeConfigs]
* @return {ConsoleLogger}
* @param {any} [configs]
* @return {VanillaLogger}
*/
getConsoleLogger(runtimeConfigs) {
return Logger.getConsoleLogger(runtimeConfigs)
}

/**
* Get a new instance of the ConsoleLogger.
*
* @param {any} [runtimeConfigs]
* @return {ConsoleLogger}
*/
static getConsoleLogger(runtimeConfigs) {
return new ConsoleLogger(runtimeConfigs)
static getVanillaLogger(configs = {}) {
return new VanillaLogger(configs)
}
}
11 changes: 0 additions & 11 deletions tests/Stubs/transporters/consoleLogger.js

This file was deleted.

12 changes: 12 additions & 0 deletions tests/Stubs/transporters/vanillaLogger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Logger } from '#src/index'

// Just testing the drivers behavior without loading configurations.
const vanillaLogger = new Logger().constructor.getVanillaLogger()

vanillaLogger.trace('hello')
vanillaLogger.debug('%s', 'hello')
vanillaLogger.info('hello')
vanillaLogger.success('%s', 'hello')
vanillaLogger.warn('({yellow,notFound,bold} hello) hello')
vanillaLogger.error('%s', 'hello')
vanillaLogger.fatal('hello')
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { exec } from 'node:child_process'
import { test } from '@japa/runner'
import { File, Path } from '@athenna/common'

test.group('ConsoleLoggerTest', group => {
test.group('VanillaLoggerTest', group => {
test('should be able to log in console using console logger', async ({ assert }) => {
const file = await new File(Path.stubs('transporters/consoleLogger.js')).load()
const file = await new File(Path.stubs('transporters/vanillaLogger.js')).load()

await exec(`node --input-type=module --eval="${file.getContentSync().toString()}"`, (_, stdout, stderr) => {
const logs = [...stdout.split('\n').filter(l => l !== ''), ...stderr.split('\n').filter(l => l !== '')]
Expand Down

0 comments on commit ab9d1eb

Please sign in to comment.