diff --git a/index.ts b/index.ts index b7bf294..9b799e4 100644 --- a/index.ts +++ b/index.ts @@ -1,7 +1,5 @@ -export { Level, type Options } from 'types' -export * as Writer from 'writer' -export {set_options, get_options, set_writer, get_writer, remove_writer, Logger} from 'index' - -import {Logger} from 'index' +import {Level, LevelFilter, type WriterOptions, type Writer} from 'types' +import {Logger, writers, options} from 'logger' export default (namespace: string) => new Logger(namespace) +export {Level, LevelFilter, type WriterOptions, type Writer, writers, options} diff --git a/src/color.ts b/src/color.ts new file mode 100644 index 0000000..87a8a9b --- /dev/null +++ b/src/color.ts @@ -0,0 +1,14 @@ +import {Chalk} from 'chalk' +import {Level} from 'types' + +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 + } +} diff --git a/src/index.ts b/src/logger.ts similarity index 72% rename from src/index.ts rename to src/logger.ts index 139d109..a24ab51 100644 --- a/src/index.ts +++ b/src/logger.ts @@ -1,28 +1,11 @@ -import {Level, level_to_string, type Options} from 'types' -import {Writer} from 'writer' +import {Level, type Options, type Writer} from 'types' +import {get_color} from 'color' export const writers = new Map export let options: Options = { format: "[$time] $level $namespace :", } -export function set_options(opt: Partial) { - options = {...options, ...opt} -} -export function get_options() : Readonly { - return options -} - -export function set_writer(name: string, writer: Writer) { - writers.set(name, writer) -} -export function get_writer(name: string) : Readonly | undefined { - return writers.get(name) -} -export function remove_writer(name: string) : boolean { - return writers.delete(name) -} - export class Logger { private readonly _namespace: string @@ -73,3 +56,22 @@ function log(message: any[], level: Level, namespace: string) : void { writer.log(level, writer.options.with_color ? head_color : head_bw, ...message) } } + +function level_to_string(level: Level, with_color: boolean) : string { + const str = get_string(level) + + if (!with_color) return str + + const color = get_color(level) + return color(str) +} + +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 " + } +} \ No newline at end of file diff --git a/src/types.ts b/src/types.ts index fdaf5f9..fbfe0ef 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,3 @@ -import { Chalk } from 'chalk' - export type Options = { format: string, } @@ -27,33 +25,19 @@ export type WriterOptions = { [key: string | number | symbol]: any, } -export function level_to_string(level: Level, with_color: boolean) : string { - const str = get_string(level) +export abstract class Writer { + protected readonly _options: WriterOptions; + protected constructor(options: WriterOptions) { + this._options = options + } - if (!with_color) return str + public log(level: Level, ...data: any[]) : void { + if (level < this.options.minLevel) return + this.write(level, ...data) + } + protected abstract write(level: Level, ...data: any[]) : void; - const color = get_color(level) - return color(str) -} - -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 " - } -} - -const chalk = new Chalk({ level: 2 }) // 256 colors - -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 + public get options() : WriterOptions { + return this._options } } diff --git a/src/writer/console.ts b/src/writer/console.ts deleted file mode 100644 index 309a6c6..0000000 --- a/src/writer/console.ts +++ /dev/null @@ -1,18 +0,0 @@ -import {Writer} from 'writer' -import {Level} from 'types' - -export class Console extends Writer { - protected write(level: Level, ...data: any[]) : void { - Console.get_logger(level)(...data) - } - - private static get_logger(level: Level) { - switch (level) { - case Level.DEBUG: return console.debug - case Level.TRACE: return console.debug - case Level.INFO: return console.log - case Level.WARNING: return console.warn - case Level.ERROR: return console.error - } - } -} diff --git a/src/writer/counter.ts b/src/writer/counter.ts deleted file mode 100644 index 11ff0b4..0000000 --- a/src/writer/counter.ts +++ /dev/null @@ -1,30 +0,0 @@ -import {Writer} from 'writer' -import {Level} from 'types' - -export class Counter extends Writer { - private readonly calls = { - debug: 0, - trace: 0, - info: 0, - warn: 0, - error: 0, - } - - public constructor() { - super({minLevel: Level.DEBUG, with_color: false}); - } - - protected write(level: Level, ...data: any[]): void { - switch (level) { - case Level.DEBUG: this.calls.debug++; break - case Level.TRACE: this.calls.trace++; break - case Level.INFO: this.calls.info++; break - case Level.WARNING: this.calls.warn++; break - case Level.ERROR: this.calls.error++; break - } - } - - public get count() { - return this.calls - } -} diff --git a/src/writer/file.ts b/src/writer/file.ts deleted file mode 100644 index aa154f8..0000000 --- a/src/writer/file.ts +++ /dev/null @@ -1,43 +0,0 @@ -import {Writer} from 'writer' -import {type WriterOptions, Level} from 'types' - -export class File extends Writer { - public constructor(options: WriterOptions & { path: string }) { - const {path, ...rest} = options - super(rest) - } - - protected write(level: Level, ...data: any[]) : void { - // TODO - } -} - -export class DailyFile extends Writer { - private _writer: File - private readonly _folder: string - private now: Date = new Date() - - public constructor(options: WriterOptions & {folder: string}) { - const {folder, ...rest} = options - - super(rest) - this._folder = folder - - this._writer = DailyFile.start(this.options, this._folder, this.now) - } - - private static start(options: WriterOptions, folder: string, now: Date) : File { - const date = now.toISOString().split('T')[0] - const path = `${folder}/${date}.log` - return new File({...options, path}) - } - - protected write(level: Level, ...data: any[]): void { - if (new Date().getDay() !== this.now.getDay()) { - this.now = new Date() - this._writer = DailyFile.start(this.options, this._folder, this.now) - } - - return this._writer.log(level, data) - } -} diff --git a/src/writer/index.ts b/src/writer/index.ts deleted file mode 100644 index ff32f5a..0000000 --- a/src/writer/index.ts +++ /dev/null @@ -1,22 +0,0 @@ -export {Console} from 'writer/console' -export {File} from 'writer/file' -export {Counter} from 'writer/counter' - -import {Level, type WriterOptions} from 'types' - -export abstract class Writer { - protected readonly _options: WriterOptions; - protected constructor(options: WriterOptions) { - this._options = options - } - - public log(level: Level, ...data: any[]) : void { - if (level < this.options.minLevel) return - this.write(level, ...data) - } - protected abstract write(level: Level, ...data: any[]) : void; - - public get options() : WriterOptions { - return this._options - } -} \ No newline at end of file