Skip to content

Commit

Permalink
feat: add meta property
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 26, 2023
1 parent 0816ecd commit b45abe1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ declare namespace Logger {

export interface Record {
id: number
meta: any
name: string
type: Logger.Type
level: number
Expand Down Expand Up @@ -67,7 +68,7 @@ declare class Logger {

private code: number

constructor(name: string)
constructor(name: string, meta?: any)

extend(namespace: string): Logger
}
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "reggol",
"description": "Logger for professionals",
"version": "1.5.1",
"version": "1.6.0",
"sideEffects": false,
"main": "lib/node.js",
"types": "index.d.ts",
Expand All @@ -19,6 +19,7 @@
},
"files": [
"lib",
"src",
"index.d.ts"
],
"repository": {
Expand All @@ -32,8 +33,8 @@
"author": "Shigma <shigma10826@gmail.com>",
"license": "MIT",
"scripts": {
"build:node": "esbuild src/node.ts --outfile=lib/node.cjs --bundle --sourcemap --platform=node --external:cosmokit --external:supports-color --target=es2019",
"build:browser": "esbuild src/browser.ts --outfile=lib/browser.mjs --bundle --sourcemap --platform=browser --external:cosmokit --external:supports-color --target=esnext",
"build:node": "esbuild src/node.ts --outfile=lib/node.cjs --bundle --sourcemap --sources-content=false --platform=node --external:cosmokit --external:supports-color --target=es2019",
"build:browser": "esbuild src/browser.ts --outfile=lib/browser.mjs --bundle --sourcemap --sources-content=false --platform=browser --external:cosmokit --external:supports-color --target=esnext",
"build": "yarn build:node && yarn build:browser && yarn tsc -b",
"test": "mocha -r esbuild-register tests/*.spec.ts",
"test:text": "shx rm -rf coverage && c8 -r text yarn test",
Expand All @@ -58,7 +59,7 @@
},
"dependencies": {
"supports-color": "^8.1.1",
"cosmokit": "^1.4.5",
"cosmokit": "^1.5.1",
"object-inspect": "^1.12.3"
}
}
7 changes: 4 additions & 3 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace Logger {

export interface Record {
id: number
meta: any
name: string
type: Logger.Type
level: number
Expand Down Expand Up @@ -130,7 +131,7 @@ class Logger {
return output
}

constructor(public name: string) {
constructor(public name: string, public meta?: any) {
if (name in Logger.instances) return Logger.instances[name]

Logger.instances[name] = this
Expand Down Expand Up @@ -165,7 +166,7 @@ class Logger {
const timestamp = Date.now()
for (const target of Logger.targets) {
const content = this.format(target, ...args)
const record: Logger.Record = { id, type, level, name: this.name, content, timestamp }
const record: Logger.Record = { id, type, level, name: this.name, meta: this.meta, content, timestamp }
if (target.record) {
target.record(record)
} else {
Expand Down Expand Up @@ -203,7 +204,7 @@ class Logger {
format += ' ' + arg
}

const { maxLength = 1024 } = target
const { maxLength = 10240 } = target
return format.split(/\r?\n/g).map(line => {
return line.slice(0, maxLength) + (line.length > maxLength ? '...' : '')
}).join('\n')
Expand Down

0 comments on commit b45abe1

Please sign in to comment.