From 5c9c583ba49e5679551a3e00e1d56a642309a378 Mon Sep 17 00:00:00 2001 From: Shigma Date: Fri, 27 Oct 2023 01:13:47 +0800 Subject: [PATCH] feat: support target-aware levels and timestamp --- index.d.ts | 3 ++- package.json | 2 +- src/shared.ts | 19 ++++++++++++------- tests/index.spec.ts | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/index.d.ts b/index.d.ts index 18bdd2f..590a74f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -38,6 +38,8 @@ declare namespace Logger { label?: LabelStyle record?(record: Record): void print?(text: string): void + levels?: LevelConfig + timestamp?: number } } @@ -53,7 +55,6 @@ declare class Logger { static readonly DEBUG = 3 // global config - static timestamp: number static colors: number[] static instances: Record static targets: Logger.Target[] diff --git a/package.json b/package.json index c705eaa..694f97a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "reggol", "description": "Logger for professionals", - "version": "1.6.1", + "version": "1.6.2", "sideEffects": false, "main": "lib/node.js", "types": "index.d.ts", diff --git a/src/shared.ts b/src/shared.ts index aff74a4..465588e 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -55,6 +55,8 @@ namespace Logger { maxLength?: number record?(record: Record): void print?(text: string): void + levels?: Logger.LevelConfig + timestamp?: number } } @@ -71,7 +73,6 @@ class Logger { // global config static id = 0 - static timestamp = 0 static targets: Logger.Target[] = [{ colors: stdout && stdout.level, print(text) { @@ -123,8 +124,8 @@ class Logger { output += prefix + space + label.padEnd(padLength) + space } output += record.content.replace(/\n/g, '\n' + ' '.repeat(indent)) - if (target.showDiff) { - const diff = Logger.timestamp && record.timestamp - Logger.timestamp + if (target.showDiff && target.timestamp) { + const diff = record.timestamp - target.timestamp output += Logger.color(target, code, ' +' + Time.format(diff)) } return output @@ -157,10 +158,10 @@ class Logger { } } - if (this.level < level) return const id = ++Logger.id const timestamp = Date.now() for (const target of Logger.targets) { + if (this.getLevel(target) < level) continue const content = this.format(target, ...args) const record: Logger.Record = { id, type, level, name: this.name, meta: this.meta, content, timestamp } if (target.record) { @@ -169,8 +170,8 @@ class Logger { const { print = console.log } = target print(Logger.render(target, record)) } + target.timestamp = timestamp } - Logger.timestamp = timestamp } } @@ -206,15 +207,19 @@ class Logger { }).join('\n') } - get level() { + getLevel(target?: Logger.Target) { const paths = this.name.split(':') - let config: Logger.Level = Logger.levels + let config: Logger.Level = target?.levels || Logger.levels do { config = config[paths.shift()!] ?? config['base'] } while (paths.length && typeof config === 'object') return config as number } + get level() { + return this.getLevel() + } + set level(value) { const paths = this.name.split(':') let config = Logger.levels diff --git a/tests/index.spec.ts b/tests/index.spec.ts index 7043032..fa8da9d 100644 --- a/tests/index.spec.ts +++ b/tests/index.spec.ts @@ -2,7 +2,7 @@ import { install, InstalledClock } from '@sinonjs/fake-timers' import { expect } from 'chai' import Logger from '../src/node' -describe('Logger API', () => { +describe('Reggol', () => { let logger: Logger let data: string let clock: InstalledClock