@innei/pretty-logger-core 0.3.3 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/consola/consola.ts +15 -15
- package/consola/constants.ts +0 -1
- package/consola/index.ts +3 -3
- package/consola/reporters/basic.ts +2 -2
- package/consola/reporters/browser.ts +1 -0
- package/consola/reporters/fancy.ts +8 -8
- package/consola/reporters/file.ts +7 -6
- package/consola/reporters/logger.ts +4 -4
- package/consola/reporters/subscriber.ts +4 -5
- package/consola/shared.ts +3 -4
- package/consola/utils/string.ts +6 -1
- package/consola/utils.ts +3 -3
- package/consola.instance.ts +3 -3
- package/dist/index.d.mts +153 -137
- package/dist/index.d.ts +153 -137
- package/dist/index.js +821 -1064
- package/dist/index.mjs +817 -1022
- package/index.ts +1 -1
- package/package.json +10 -10
- package/tool.util.ts +7 -16
- package/{tsup.config.ts → tsdown.config.ts} +1 -1
package/consola/consola.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { defu } from 'defu'
|
|
2
|
+
|
|
2
3
|
import type { LogLevel, LogType } from './constants'
|
|
4
|
+
import { LogTypes } from './constants'
|
|
3
5
|
import type {
|
|
4
6
|
ConsolaOptions,
|
|
5
7
|
ConsolaReporter,
|
|
6
8
|
InputLogObject,
|
|
7
9
|
LogObject,
|
|
8
10
|
} from './types'
|
|
9
|
-
|
|
10
|
-
import { LogTypes } from './constants'
|
|
11
11
|
import { isLogObj } from './utils/log'
|
|
12
12
|
|
|
13
13
|
let paused = false
|
|
@@ -30,13 +30,13 @@ export class Consola {
|
|
|
30
30
|
// Options
|
|
31
31
|
const types = options.types || LogTypes
|
|
32
32
|
this.options = defu(
|
|
33
|
-
|
|
33
|
+
({
|
|
34
34
|
...options,
|
|
35
35
|
defaults: { ...options.defaults },
|
|
36
36
|
level: _normalizeLogLevel(options.level, types),
|
|
37
37
|
reporters: [...(options.reporters || [])],
|
|
38
|
-
},
|
|
39
|
-
|
|
38
|
+
} as ConsolaOptions),
|
|
39
|
+
({
|
|
40
40
|
types: LogTypes,
|
|
41
41
|
throttle: 1000,
|
|
42
42
|
throttleMin: 5,
|
|
@@ -45,7 +45,7 @@ export class Consola {
|
|
|
45
45
|
colors: false,
|
|
46
46
|
compact: true,
|
|
47
47
|
},
|
|
48
|
-
},
|
|
48
|
+
} as Partial<ConsolaOptions>),
|
|
49
49
|
)
|
|
50
50
|
|
|
51
51
|
// Create logger functions for current instance
|
|
@@ -125,7 +125,7 @@ export class Consola {
|
|
|
125
125
|
removeReporter(reporter: ConsolaReporter) {
|
|
126
126
|
if (reporter) {
|
|
127
127
|
const i = this.options.reporters.indexOf(reporter)
|
|
128
|
-
if (i
|
|
128
|
+
if (i !== -1) {
|
|
129
129
|
return this.options.reporters.splice(i, 1)
|
|
130
130
|
}
|
|
131
131
|
} else {
|
|
@@ -153,13 +153,13 @@ export class Consola {
|
|
|
153
153
|
for (const type in this.options.types) {
|
|
154
154
|
// Backup original value
|
|
155
155
|
if (!(console as any)[`__${type}`]) {
|
|
156
|
-
|
|
157
|
-
;(console as any)[`__${type}`] = (console as any)[type]
|
|
156
|
+
|
|
157
|
+
;(console as any)[`__${type}`] = (console as any)[type]
|
|
158
158
|
}
|
|
159
159
|
// Override
|
|
160
160
|
;(console as any)[type] = (this as unknown as ConsolaInstance)[
|
|
161
161
|
type as LogType
|
|
162
|
-
].raw
|
|
162
|
+
].raw
|
|
163
163
|
}
|
|
164
164
|
}
|
|
165
165
|
|
|
@@ -167,9 +167,9 @@ export class Consola {
|
|
|
167
167
|
for (const type in this.options.types) {
|
|
168
168
|
// Restore if backup is available
|
|
169
169
|
if ((console as any)[`__${type}`]) {
|
|
170
|
-
|
|
171
|
-
;(console as any)[type] = (console as any)[`__${type}`]
|
|
172
|
-
delete (console as any)[`__${type}`]
|
|
170
|
+
|
|
171
|
+
;(console as any)[type] = (console as any)[`__${type}`]
|
|
172
|
+
delete (console as any)[`__${type}`]
|
|
173
173
|
}
|
|
174
174
|
}
|
|
175
175
|
}
|
|
@@ -277,7 +277,7 @@ export class Consola {
|
|
|
277
277
|
|
|
278
278
|
// Aliases
|
|
279
279
|
if (logObj.message) {
|
|
280
|
-
|
|
280
|
+
|
|
281
281
|
logObj.args!.unshift(logObj.message)
|
|
282
282
|
delete logObj.message
|
|
283
283
|
}
|
|
@@ -285,7 +285,7 @@ export class Consola {
|
|
|
285
285
|
if (!Array.isArray(logObj.additional)) {
|
|
286
286
|
logObj.additional = logObj.additional.split('\n')
|
|
287
287
|
}
|
|
288
|
-
|
|
288
|
+
|
|
289
289
|
logObj.args!.push(`\n${logObj.additional.join('\n')}`)
|
|
290
290
|
delete logObj.additional
|
|
291
291
|
}
|
package/consola/constants.ts
CHANGED
package/consola/index.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { isCI, isDebug, isTest } from 'std-env'
|
|
2
|
-
import type { ConsolaInstance } from './consola'
|
|
3
|
-
import type { LogLevel } from './constants'
|
|
4
|
-
import type { ConsolaOptions } from './types'
|
|
5
2
|
|
|
3
|
+
import type { ConsolaInstance } from './consola'
|
|
6
4
|
import { createConsola as _createConsola } from './consola'
|
|
5
|
+
import type { LogLevel } from './constants'
|
|
7
6
|
import { LogLevels } from './constants'
|
|
8
7
|
import { BasicReporter } from './reporters/basic'
|
|
9
8
|
import { FancyReporter } from './reporters/fancy'
|
|
9
|
+
import type { ConsolaOptions } from './types'
|
|
10
10
|
|
|
11
11
|
export * from './shared'
|
|
12
12
|
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { formatWithOptions } from 'node:util'
|
|
2
|
+
|
|
2
3
|
import type {
|
|
3
4
|
ConsolaOptions,
|
|
4
5
|
ConsolaReporter,
|
|
5
6
|
FormatOptions,
|
|
6
7
|
LogObject,
|
|
7
8
|
} from '../types'
|
|
8
|
-
|
|
9
9
|
import { parseStack } from '../utils/error'
|
|
10
10
|
import { writeStream } from '../utils/stream'
|
|
11
11
|
|
|
12
12
|
const bracket = (x: string) => (x ? `[${x}]` : '')
|
|
13
13
|
|
|
14
14
|
export class BasicReporter implements ConsolaReporter {
|
|
15
|
-
formatStack(stack: string,
|
|
15
|
+
formatStack(stack: string, _opts: FormatOptions) {
|
|
16
16
|
return ` ${parseStack(stack).join('\n ')}`
|
|
17
17
|
}
|
|
18
18
|
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import _stringWidth from 'string-width'
|
|
2
|
+
|
|
2
3
|
import type { LogLevel, LogType } from '../constants'
|
|
3
4
|
import type { FormatOptions, LogObject } from '../types'
|
|
4
|
-
import type { BoxOpts } from '../utils/box'
|
|
5
|
-
|
|
6
5
|
import { stripAnsi } from '../utils'
|
|
6
|
+
import type { BoxOpts } from '../utils/box'
|
|
7
7
|
import { box } from '../utils/box'
|
|
8
8
|
import { colors } from '../utils/color'
|
|
9
9
|
import { parseStack } from '../utils/error'
|
|
10
10
|
import { isUnicodeSupported } from '../utils/tester'
|
|
11
11
|
import { BasicReporter } from './basic'
|
|
12
12
|
|
|
13
|
-
export const TYPE_COLOR_MAP:
|
|
13
|
+
export const TYPE_COLOR_MAP: Partial<Record<LogType, string>> = {
|
|
14
14
|
info: 'cyan',
|
|
15
15
|
fail: 'red',
|
|
16
16
|
success: 'green',
|
|
@@ -18,14 +18,14 @@ export const TYPE_COLOR_MAP: { [k in LogType]?: string } = {
|
|
|
18
18
|
start: 'magenta',
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
export const LEVEL_COLOR_MAP:
|
|
21
|
+
export const LEVEL_COLOR_MAP: Partial<Record<LogLevel, string>> = {
|
|
22
22
|
0: 'red',
|
|
23
23
|
1: 'yellow',
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
const unicode = isUnicodeSupported()
|
|
27
27
|
const s = (c: string, fallback: string) => (unicode ? c : fallback)
|
|
28
|
-
const TYPE_ICONS:
|
|
28
|
+
const TYPE_ICONS: Partial<Record<LogType, string>> = {
|
|
29
29
|
error: s('✖', '×'),
|
|
30
30
|
fatal: s('✖', '×'),
|
|
31
31
|
ready: s('✔', '√'),
|
|
@@ -59,7 +59,7 @@ export class FancyReporter extends BasicReporter {
|
|
|
59
59
|
.join('\n')}`
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
formatType(logObj: LogObject, isBadge: boolean,
|
|
62
|
+
formatType(logObj: LogObject, isBadge: boolean, _opts: FormatOptions) {
|
|
63
63
|
const typeColor =
|
|
64
64
|
(TYPE_COLOR_MAP as any)[logObj.type] ||
|
|
65
65
|
(LEVEL_COLOR_MAP as any)[logObj.level] ||
|
|
@@ -134,9 +134,9 @@ function characterFormat(str: string) {
|
|
|
134
134
|
return (
|
|
135
135
|
str
|
|
136
136
|
// highlight backticks
|
|
137
|
-
.
|
|
137
|
+
.replaceAll(/`([^`]+)`/g, (_, m) => colors.cyan(m))
|
|
138
138
|
// underline underscores
|
|
139
|
-
.
|
|
139
|
+
.replaceAll(/\s+_([^_]+)_\s+/g, (_, m) => ` ${colors.underline(m)} `)
|
|
140
140
|
)
|
|
141
141
|
}
|
|
142
142
|
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import type { WriteStream } from 'node:fs'
|
|
2
|
+
import { createWriteStream } from 'node:fs'
|
|
3
|
+
import * as fs from 'node:fs'
|
|
4
|
+
import { dirname } from 'node:path'
|
|
5
|
+
|
|
4
6
|
import { CronJob } from 'cron'
|
|
5
|
-
import type { WriteStream } from 'fs'
|
|
6
|
-
import type { ConsolaOptions, LogObject } from '../types'
|
|
7
7
|
|
|
8
8
|
import { getLogFilePath } from '../../tool.util'
|
|
9
|
+
import type { ConsolaOptions, LogObject } from '../types'
|
|
9
10
|
import { writeStream } from '../utils/stream'
|
|
10
11
|
import { LoggerReporter } from './logger'
|
|
11
12
|
|
|
@@ -113,7 +114,7 @@ export class FileReporter extends LoggerReporter {
|
|
|
113
114
|
}
|
|
114
115
|
}
|
|
115
116
|
|
|
116
|
-
|
|
117
|
+
function createLoggerFileIfNotExist (path: string) {
|
|
117
118
|
const dirPath = dirname(path)
|
|
118
119
|
|
|
119
120
|
if (!fs.existsSync(dirPath)) {
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
2
|
import picocolors from 'picocolors'
|
|
3
3
|
import { isDevelopment } from 'std-env'
|
|
4
|
-
import type { FormatOptions, LogObject } from '../types'
|
|
5
4
|
|
|
6
5
|
import { getShortTime } from '../../tool.util'
|
|
6
|
+
import type { FormatOptions, LogObject } from '../types'
|
|
7
7
|
import { FancyReporter } from './fancy'
|
|
8
8
|
|
|
9
9
|
export class LoggerReporter extends FancyReporter {
|
|
10
10
|
private latestLogTime: number = Date.now()
|
|
11
11
|
public formatDate(date: Date, opts: FormatOptions): string {
|
|
12
|
-
const isInVirtualTerminal =
|
|
12
|
+
const isInVirtualTerminal = opts.columns === undefined
|
|
13
13
|
if (isDevelopment) {
|
|
14
14
|
const now = Date.now()
|
|
15
15
|
const delta = now - this.latestLogTime
|
|
@@ -21,7 +21,7 @@ export class LoggerReporter extends FancyReporter {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
public formatLogObj(logObj: LogObject, opts: FormatOptions): string {
|
|
24
|
-
const isInVirtualTerminal =
|
|
24
|
+
const isInVirtualTerminal = opts.columns === undefined
|
|
25
25
|
return isInVirtualTerminal
|
|
26
26
|
? `${picocolors.gray(getShortTime(new Date()))} ${super
|
|
27
27
|
.formatLogObj(logObj, opts)
|
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import EventEmitter from 'events'
|
|
1
|
+
import EventEmitter from 'node:events'
|
|
2
|
+
|
|
2
3
|
import type { ConsolaInstance } from '../consola'
|
|
3
4
|
import type { ConsolaOptions, LogObject, WrappedConsola } from '../types'
|
|
4
|
-
|
|
5
5
|
import { LoggerReporter } from './logger'
|
|
6
6
|
|
|
7
|
-
export
|
|
8
|
-
consola: ConsolaInstance,
|
|
9
|
-
): WrappedConsola => {
|
|
7
|
+
export function wrapperSubscribers(consola: ConsolaInstance): WrappedConsola {
|
|
10
8
|
Object.assign(consola, {
|
|
11
9
|
onData: (handler: (data: string) => any) =>
|
|
12
10
|
SubscriberReporter.subscriber.on('log', handler),
|
|
@@ -20,6 +18,7 @@ export const wrapperSubscribers = (
|
|
|
20
18
|
}
|
|
21
19
|
|
|
22
20
|
export class SubscriberReporter extends LoggerReporter {
|
|
21
|
+
// eslint-disable-next-line unicorn/prefer-event-target
|
|
23
22
|
static subscriber = new EventEmitter()
|
|
24
23
|
log(logObj: LogObject, ctx: { options: ConsolaOptions }) {
|
|
25
24
|
const line = super.formatLogObj(logObj, ctx)
|
package/consola/shared.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
export { LogLevels, LogTypes } from './constants'
|
|
2
|
-
export { Consola } from './consola'
|
|
3
|
-
|
|
4
|
-
export type * from './types'
|
|
5
1
|
export type { ConsolaInstance } from './consola'
|
|
2
|
+
export { Consola } from './consola'
|
|
6
3
|
export type { LogLevel, LogType } from './constants'
|
|
4
|
+
export { LogLevels, LogTypes } from './constants'
|
|
5
|
+
export type * from './types'
|
package/consola/utils/string.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
+
/* eslint-disable regexp/no-useless-escape */
|
|
2
|
+
/* eslint-disable regexp/prefer-w */
|
|
3
|
+
/* eslint-disable regexp/no-useless-quantifier */
|
|
4
|
+
/* eslint-disable regexp/no-trivially-nested-quantifier */
|
|
5
|
+
/* eslint-disable regexp/no-useless-non-capturing-group */
|
|
1
6
|
const ansiRegex = [
|
|
2
7
|
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
|
|
3
8
|
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))',
|
|
4
9
|
].join('|')
|
|
5
10
|
|
|
6
11
|
export function stripAnsi(text: string) {
|
|
7
|
-
return text.
|
|
12
|
+
return text.replaceAll(new RegExp(ansiRegex, 'g'), '')
|
|
8
13
|
}
|
|
9
14
|
|
|
10
15
|
export function centerAlign(str: string, len: number, space = ' ') {
|
package/consola/utils.ts
CHANGED
package/consola.instance.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { isDevelopment } from 'std-env'
|
|
2
|
-
import type { ConsolaOptions, ConsolaReporter } from './consola'
|
|
3
|
-
import type { FileReporterConfig } from './consola/reporters/file'
|
|
4
2
|
|
|
3
|
+
import type { ConsolaOptions, ConsolaReporter } from './consola'
|
|
5
4
|
import { createConsola, LogLevels } from './consola'
|
|
6
5
|
import { FancyReporter } from './consola/reporters/fancy'
|
|
6
|
+
import type { FileReporterConfig } from './consola/reporters/file'
|
|
7
7
|
import { FileReporter } from './consola/reporters/file'
|
|
8
8
|
import {
|
|
9
9
|
SubscriberReporter,
|
|
@@ -14,7 +14,7 @@ export interface LoggerConsolaOptions extends Partial<ConsolaOptions> {
|
|
|
14
14
|
writeToFile?: FileReporterConfig
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
export
|
|
17
|
+
export function createLoggerConsola (options?: LoggerConsolaOptions) {
|
|
18
18
|
const reporters: ConsolaReporter[] = [
|
|
19
19
|
new FancyReporter(),
|
|
20
20
|
new SubscriberReporter(),
|