From abc41ac749e8ecc0901ba8a32e1b8719e53a48b9 Mon Sep 17 00:00:00 2001 From: Pascal Perrenoud Date: Sat, 27 Jul 2024 13:35:17 +0200 Subject: [PATCH] fmt --- src/color.ts | 21 +++++---- src/logger.ts | 121 +++++++++++++++++++++++++------------------------- src/types.ts | 28 ++++++------ 3 files changed, 88 insertions(+), 82 deletions(-) diff --git a/src/color.ts b/src/color.ts index 2ef8620..52ca221 100644 --- a/src/color.ts +++ b/src/color.ts @@ -1,14 +1,19 @@ import {Chalk} from 'chalk' import {Level} from './types' -const chalk = new Chalk({ level: 2 }) // 256 colors +const chalk = new Chalk({level: 2}) // 256 colors export function get_color(level: Level) { - switch (level) { - case Level.DEBUG: return chalk.blueBright - case Level.TRACE: return chalk.green - case Level.INFO: return (str: string) => str - case Level.WARNING: return chalk.hex('#FFA500') - case Level.ERROR: return chalk.red - } + switch (level) { + case Level.DEBUG: + return chalk.blueBright + case Level.TRACE: + return chalk.green + case Level.INFO: + return (str: string) => str + case Level.WARNING: + return chalk.hex('#FFA500') + case Level.ERROR: + return chalk.red + } } diff --git a/src/logger.ts b/src/logger.ts index 882e1cc..0dd3ab6 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -1,80 +1,81 @@ import {Level, type Options, type Writer} from './types' import {get_color} from './color' -export const writers = new Map +export const writers = new Map() export let options: Options = { - format: "[$time] $level $namespace :", - pad_level: true, - verbose: true, + format: '[$time] $level $namespace :', + pad_level: true, + verbose: true } export class Logger { - constructor( - private readonly _namespace: string - ) {} + constructor(private readonly _namespace: string) {} - public get namespace() : string { - return this._namespace - } + public get namespace(): string { + return this._namespace + } - public extend(sub_namespace: string) : Logger { - return new Logger(`${this.namespace}:${sub_namespace}`) - } + public extend(sub_namespace: string): Logger { + return new Logger(`${this.namespace}:${sub_namespace}`) + } - public debug(...data: any[]) : void { - log(data, Level.DEBUG, this._namespace) - } + public debug(...data: any[]): void { + log(data, Level.DEBUG, this._namespace) + } - public trace(...data: any[]) : void { - log(data, Level.TRACE, this._namespace) - } + public trace(...data: any[]): void { + log(data, Level.TRACE, this._namespace) + } - public info(...data: any[]) : void { - log(data, Level.INFO, this._namespace) - } + public info(...data: any[]): void { + log(data, Level.INFO, this._namespace) + } - public warn(...data: any[]) : void { - log(data, Level.WARNING, this._namespace) - } + public warn(...data: any[]): void { + log(data, Level.WARNING, this._namespace) + } - public error(...data: any[]) : void { - log(data, Level.ERROR, this._namespace) - } + public error(...data: any[]): void { + log(data, Level.ERROR, this._namespace) + } } -function log(message: any[], level: Level, namespace: string) : void { - if (writers.size === 0) { - if (options.verbose) console.log("No writer subscribed, discard message") - return - } - - // Format header of log - const head = options.format - .replace("$time", new Date().toISOString()) - .replace("$namespace", namespace) - - let lvl = get_string(level) - if (!options.pad_level) lvl = lvl.trimEnd() - - const head_color = head.replace('$level', get_color(level)(lvl)) - const head_bw = head.replace('$level', lvl) - - for (const [name, writer] of writers.entries()) { - const options = writer.options - if (options?.minLevel > level) { - if (options.verbose) console.log(`Writer's level is lower, discard message for ${name}`) - continue - } - writer.log(level, options?.with_color ? head_color : head_bw, ...message) +function log(message: any[], level: Level, namespace: string): void { + if (writers.size === 0) { + if (options.verbose) console.log('No writer subscribed, discard message') + return + } + + // Format header of log + const head = options.format.replace('$time', new Date().toISOString()).replace('$namespace', namespace) + + let lvl = get_string(level) + if (!options.pad_level) lvl = lvl.trimEnd() + + const head_color = head.replace('$level', get_color(level)(lvl)) + const head_bw = head.replace('$level', lvl) + + for (const [name, writer] of writers.entries()) { + const options = writer.options + if (options?.minLevel > level) { + if (options.verbose) console.log(`Writer's level is lower, discard message for ${name}`) + continue } + writer.log(level, options?.with_color ? head_color : head_bw, ...message) + } } -function get_string(level: Level) : string { - switch (level) { - case Level.DEBUG: return "DEBUG " - case Level.TRACE: return "TRACE " - case Level.INFO: return "INFO " - case Level.WARNING: return "WARNING" - case Level.ERROR: return "ERROR " - } +function get_string(level: Level): string { + switch (level) { + case Level.DEBUG: + return 'DEBUG ' + case Level.TRACE: + return 'TRACE ' + case Level.INFO: + return 'INFO ' + case Level.WARNING: + return 'WARNING' + case Level.ERROR: + return 'ERROR ' + } } diff --git a/src/types.ts b/src/types.ts index 2233e30..c11c170 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,27 +4,27 @@ * Format : can contain $time, $level and $namespace */ export type Options = { - format: string, - pad_level: boolean, - verbose: boolean, + format: string + pad_level: boolean + verbose: boolean } export enum Level { - TRACE = 0, - DEBUG = 1, - INFO = 2, - WARNING = 3, - ERROR = 4, + TRACE = 0, + DEBUG = 1, + INFO = 2, + WARNING = 3, + ERROR = 4 } export type WriterOptions = { - minLevel: Level, - with_color: boolean, - [key: string | number | symbol]: any, + minLevel: Level + with_color: boolean + [key: string | number | symbol]: any } export interface Writer { - log(level: Level, ...data: any[]) : void; - get options() : WriterOptions; - readonly _options: WriterOptions; + log(level: Level, ...data: any[]): void + get options(): WriterOptions + readonly _options: WriterOptions }