Skip to content

Commit

Permalink
feat: support target-aware levels and timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 26, 2023
1 parent b62c896 commit 5c9c583
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ declare namespace Logger {
label?: LabelStyle
record?(record: Record): void
print?(text: string): void
levels?: LevelConfig
timestamp?: number
}
}

Expand All @@ -53,7 +55,6 @@ declare class Logger {
static readonly DEBUG = 3

// global config
static timestamp: number
static colors: number[]
static instances: Record<string, Logger>
static targets: Logger.Target[]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
19 changes: 12 additions & 7 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ namespace Logger {
maxLength?: number
record?(record: Record): void
print?(text: string): void
levels?: Logger.LevelConfig
timestamp?: number
}
}

Expand All @@ -71,7 +73,6 @@ class Logger {

// global config
static id = 0
static timestamp = 0
static targets: Logger.Target[] = [{
colors: stdout && stdout.level,
print(text) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -169,8 +170,8 @@ class Logger {
const { print = console.log } = target
print(Logger.render(target, record))
}
target.timestamp = timestamp
}
Logger.timestamp = timestamp
}
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5c9c583

Please sign in to comment.