@breadc/logger 0.9.7-beta.0 → 1.0.0-beta.1
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/dist/index.d.mts +102 -0
- package/dist/index.mjs +216 -3
- package/package.json +8 -11
- package/dist/index.cjs +0 -7
- package/dist/index.d.ts +0 -22
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
//#region src/level.d.ts
|
|
2
|
+
type LogLevel = 0 | 1 | 2 | 3 | 4 | 5 | (number & {});
|
|
3
|
+
declare const LogLevels: Record<LogType, number>;
|
|
4
|
+
type LogType = 'silent' | 'fatal' | 'error' | 'warn' | 'log' | 'info' | 'success' | 'fail' | 'ready' | 'start' | 'box' | 'debug' | 'trace' | 'verbose';
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region src/types.d.ts
|
|
7
|
+
interface LoggerPlugin {}
|
|
8
|
+
interface LoggerOptions {
|
|
9
|
+
reporter: Reporter[];
|
|
10
|
+
level: LogLevel;
|
|
11
|
+
defaults: InputLogObject;
|
|
12
|
+
format: FormatOptions;
|
|
13
|
+
stdout?: NodeJS.WriteStream;
|
|
14
|
+
stderr?: NodeJS.WriteStream;
|
|
15
|
+
plugins: LoggerPlugin[];
|
|
16
|
+
}
|
|
17
|
+
interface InputLogObject {
|
|
18
|
+
level?: LogLevel;
|
|
19
|
+
type?: LogType;
|
|
20
|
+
tag?: string;
|
|
21
|
+
date?: Date;
|
|
22
|
+
message?: string;
|
|
23
|
+
args?: any[];
|
|
24
|
+
}
|
|
25
|
+
interface LogObject extends InputLogObject {
|
|
26
|
+
level: LogLevel;
|
|
27
|
+
type: LogType;
|
|
28
|
+
date: Date;
|
|
29
|
+
}
|
|
30
|
+
type InputLogItem = string | number | Omit<InputLogObject, 'level' | 'type'>;
|
|
31
|
+
interface PrintContext {
|
|
32
|
+
options: LoggerOptions;
|
|
33
|
+
}
|
|
34
|
+
interface Reporter {
|
|
35
|
+
print: (log: LogObject, ctx: PrintContext) => void;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* @see https://nodejs.org/api/util.html#util_util_inspect_object_showhidden_depth_colors
|
|
39
|
+
*/
|
|
40
|
+
interface FormatOptions {
|
|
41
|
+
columns?: number;
|
|
42
|
+
date?: boolean;
|
|
43
|
+
colors?: boolean;
|
|
44
|
+
compact?: boolean | number;
|
|
45
|
+
[key: string]: unknown;
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/logger.d.ts
|
|
49
|
+
declare class BreadcLogger<T extends {}> {
|
|
50
|
+
private _overrides;
|
|
51
|
+
readonly options: LoggerOptions;
|
|
52
|
+
constructor(options: LoggerOptions);
|
|
53
|
+
get level(): LogLevel;
|
|
54
|
+
set level(level: LogLevel);
|
|
55
|
+
get reporter(): Reporter[];
|
|
56
|
+
extend<U extends {}>(overrides: U): BreadcLogger<T & U> & T & U;
|
|
57
|
+
withDefaults(defaults: InputLogObject): BreadcLogger<T> & T;
|
|
58
|
+
withTag(tag: string): BreadcLogger<T> & T;
|
|
59
|
+
private shouldPrint;
|
|
60
|
+
print(defaults: InputLogObject, input: InputLogObject): void;
|
|
61
|
+
resolveInput(input: InputLogItem, args: any[]): Omit<InputLogObject, "level" | "type">;
|
|
62
|
+
log(input: InputLogItem, ...args: any[]): void;
|
|
63
|
+
}
|
|
64
|
+
type LogFn = (message: InputLogItem, ...args: any[]) => void;
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/reporters/types.d.ts
|
|
67
|
+
interface FormatReporterOptions {
|
|
68
|
+
prefix: string;
|
|
69
|
+
}
|
|
70
|
+
interface FormatReporter$1 extends Reporter {
|
|
71
|
+
formatArgs(opts: FormatOptions, message?: string, args?: any[]): string;
|
|
72
|
+
formatLogObject(obj: LogObject, ctx: PrintContext): string;
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region src/reporters/mock.d.ts
|
|
76
|
+
interface HistoryLog {
|
|
77
|
+
readonly output: string;
|
|
78
|
+
readonly object: LogObject;
|
|
79
|
+
}
|
|
80
|
+
declare const MockReporter: (reporter: FormatReporter$1, history?: HistoryLog[]) => Reporter & {
|
|
81
|
+
history: HistoryLog[];
|
|
82
|
+
};
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region src/reporters/basic.d.ts
|
|
85
|
+
interface BasicReporterOptions extends FormatReporterOptions {}
|
|
86
|
+
declare const BasicReporter: (options?: Partial<BasicReporterOptions>) => FormatReporter$1;
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/reporters/fancy.d.ts
|
|
89
|
+
interface FancyReporterOptions extends FormatReporterOptions {}
|
|
90
|
+
declare const FancyReporter: (options?: Partial<FancyReporterOptions>) => FormatReporter$1;
|
|
91
|
+
//#endregion
|
|
92
|
+
//#region src/reporters/index.d.ts
|
|
93
|
+
declare const FormatReporter: (options: Partial<FormatReporterOptions> & {
|
|
94
|
+
fancy?: boolean;
|
|
95
|
+
}) => FormatReporter$1;
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region src/index.d.ts
|
|
98
|
+
declare const Logger: (options?: Partial<LoggerOptions> & {
|
|
99
|
+
fancy?: boolean;
|
|
100
|
+
}) => BreadcLogger<Record<LogType, LogFn>> & Record<LogType, LogFn>;
|
|
101
|
+
//#endregion
|
|
102
|
+
export { BasicReporter, BasicReporterOptions, BreadcLogger, FancyReporter, FancyReporterOptions, FormatOptions, FormatReporter, InputLogItem, InputLogObject, LogFn, LogLevel, LogLevels, LogObject, LogType, Logger, LoggerOptions, LoggerPlugin, MockReporter, PrintContext, Reporter };
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,218 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { hasTTY, isCI, isDebug, isTest } from "std-env";
|
|
2
|
+
import { formatWithOptions } from "node:util";
|
|
3
|
+
|
|
4
|
+
//#region src/level.ts
|
|
5
|
+
const LogLevels = {
|
|
6
|
+
silent: Number.NEGATIVE_INFINITY,
|
|
7
|
+
fatal: 0,
|
|
8
|
+
error: 0,
|
|
9
|
+
warn: 1,
|
|
10
|
+
log: 2,
|
|
11
|
+
info: 3,
|
|
12
|
+
success: 3,
|
|
13
|
+
fail: 3,
|
|
14
|
+
ready: 3,
|
|
15
|
+
start: 3,
|
|
16
|
+
box: 3,
|
|
17
|
+
debug: 4,
|
|
18
|
+
trace: 5,
|
|
19
|
+
verbose: Number.POSITIVE_INFINITY
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/utils/format.ts
|
|
24
|
+
const bracket = (text) => {
|
|
25
|
+
if (text) return `[${text}]`;
|
|
26
|
+
else return;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/utils/stream.ts
|
|
31
|
+
function writeStream(data, stream) {
|
|
32
|
+
return (stream.__write || stream.write).call(stream, data);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/reporters/fancy.ts
|
|
37
|
+
const FancyReporter = (options = {}) => {
|
|
38
|
+
return {
|
|
39
|
+
formatArgs(opts, message, args = []) {
|
|
40
|
+
return formatWithOptions(opts, message, ...args);
|
|
41
|
+
},
|
|
42
|
+
formatLogObject(obj, ctx) {
|
|
43
|
+
const message = this.formatArgs(ctx.options.format, obj.message, obj.args);
|
|
44
|
+
return [
|
|
45
|
+
options.prefix,
|
|
46
|
+
bracket(obj.type === "log" ? void 0 : obj.type),
|
|
47
|
+
bracket(obj.tag),
|
|
48
|
+
message
|
|
49
|
+
].filter(Boolean).join(" ");
|
|
50
|
+
},
|
|
51
|
+
print(obj, ctx) {
|
|
52
|
+
writeStream(this.formatLogObject(obj, ctx), obj.level < LogLevels.log ? ctx.options.stderr || process.stderr : ctx.options.stdout || process.stdout);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/reporters/basic.ts
|
|
59
|
+
const BasicReporter = (options = {}) => {
|
|
60
|
+
return {
|
|
61
|
+
formatArgs(opts, message, args = []) {
|
|
62
|
+
return formatWithOptions(opts, message, ...args);
|
|
63
|
+
},
|
|
64
|
+
formatLogObject(obj, ctx) {
|
|
65
|
+
const message = this.formatArgs(ctx.options.format, obj.message, obj.args);
|
|
66
|
+
return [
|
|
67
|
+
options.prefix,
|
|
68
|
+
bracket(obj.type === "log" ? void 0 : obj.type),
|
|
69
|
+
bracket(obj.tag),
|
|
70
|
+
message
|
|
71
|
+
].filter(Boolean).join(" ");
|
|
72
|
+
},
|
|
73
|
+
print(obj, ctx) {
|
|
74
|
+
writeStream(this.formatLogObject(obj, ctx), obj.level < LogLevels.log ? ctx.options.stderr || process.stderr : ctx.options.stdout || process.stdout);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/reporters/mock.ts
|
|
81
|
+
const MockReporter = (reporter, history = []) => {
|
|
82
|
+
return {
|
|
83
|
+
history,
|
|
84
|
+
print(object, ctx) {
|
|
85
|
+
history.push({
|
|
86
|
+
output: reporter.formatLogObject(object, ctx),
|
|
87
|
+
object
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region src/reporters/index.ts
|
|
95
|
+
const FormatReporter = (options) => {
|
|
96
|
+
return (options.fancy === true || options.fancy === void 0 && hasTTY) && !(isCI || isTest) ? FancyReporter() : BasicReporter();
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
//#endregion
|
|
100
|
+
//#region src/logger.ts
|
|
101
|
+
var BreadcLogger = class BreadcLogger {
|
|
102
|
+
constructor(options) {
|
|
103
|
+
this._overrides = {};
|
|
104
|
+
this.options = options;
|
|
105
|
+
}
|
|
106
|
+
get level() {
|
|
107
|
+
return this.options.level;
|
|
108
|
+
}
|
|
109
|
+
set level(level) {
|
|
110
|
+
this.options.level = level;
|
|
111
|
+
}
|
|
112
|
+
get reporter() {
|
|
113
|
+
return this.options.reporter;
|
|
114
|
+
}
|
|
115
|
+
extend(overrides) {
|
|
116
|
+
const that = this;
|
|
117
|
+
Object.assign(that._overrides, overrides);
|
|
118
|
+
return Object.assign(that, overrides);
|
|
119
|
+
}
|
|
120
|
+
withDefaults(defaults) {
|
|
121
|
+
const ins = new BreadcLogger({
|
|
122
|
+
...this.options,
|
|
123
|
+
defaults: {
|
|
124
|
+
...this.options.defaults,
|
|
125
|
+
...defaults
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
ins._overrides = this._overrides;
|
|
129
|
+
return Object.assign(ins, this._overrides);
|
|
130
|
+
}
|
|
131
|
+
withTag(tag) {
|
|
132
|
+
return this.withDefaults({ tag: this.options.defaults.tag ? this.options.defaults.tag + ":" + tag : tag });
|
|
133
|
+
}
|
|
134
|
+
shouldPrint(obj) {
|
|
135
|
+
return obj.level <= this.level;
|
|
136
|
+
}
|
|
137
|
+
print(defaults, input) {
|
|
138
|
+
const date = /* @__PURE__ */ new Date();
|
|
139
|
+
const obj = {
|
|
140
|
+
level: LogLevels["log"],
|
|
141
|
+
type: "log",
|
|
142
|
+
...this.options.defaults,
|
|
143
|
+
...defaults,
|
|
144
|
+
...input,
|
|
145
|
+
date
|
|
146
|
+
};
|
|
147
|
+
if (this.shouldPrint(obj)) for (const reporter of this.options.reporter) reporter.print(obj, { options: this.options });
|
|
148
|
+
}
|
|
149
|
+
resolveInput(input, args) {
|
|
150
|
+
if (typeof input === "string") return {
|
|
151
|
+
message: input,
|
|
152
|
+
args
|
|
153
|
+
};
|
|
154
|
+
else if (typeof input === "number") return {
|
|
155
|
+
message: String(input),
|
|
156
|
+
args
|
|
157
|
+
};
|
|
158
|
+
else {
|
|
159
|
+
if ("level" in input) delete input["level"];
|
|
160
|
+
if ("type" in input) delete input["type"];
|
|
161
|
+
if (Array.isArray(input.args)) input.args.push(...args);
|
|
162
|
+
return input;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
log(input, ...args) {
|
|
166
|
+
const type = "log";
|
|
167
|
+
const defaults = {
|
|
168
|
+
type,
|
|
169
|
+
level: LogLevels[type]
|
|
170
|
+
};
|
|
171
|
+
this.print(defaults, this.resolveInput(input, args));
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
//#endregion
|
|
176
|
+
//#region src/index.ts
|
|
177
|
+
const Logger = (options = {}) => {
|
|
178
|
+
const level = getDefaultLogLevel();
|
|
179
|
+
const logger = new BreadcLogger({
|
|
180
|
+
reporter: options.reporter || [FormatReporter({ fancy: options.fancy })],
|
|
181
|
+
level,
|
|
182
|
+
defaults: {},
|
|
183
|
+
format: {},
|
|
184
|
+
stdout: process?.stdout,
|
|
185
|
+
stderr: process?.stderr,
|
|
186
|
+
plugins: [],
|
|
187
|
+
...options
|
|
188
|
+
});
|
|
189
|
+
const fns = Object.fromEntries([
|
|
190
|
+
"fatal",
|
|
191
|
+
"error",
|
|
192
|
+
"warn",
|
|
193
|
+
"info",
|
|
194
|
+
"fail",
|
|
195
|
+
"ready",
|
|
196
|
+
"box",
|
|
197
|
+
"start",
|
|
198
|
+
"success",
|
|
199
|
+
"debug",
|
|
200
|
+
"trace",
|
|
201
|
+
"verbose"
|
|
202
|
+
].map((type) => [type, function(input, ...args) {
|
|
203
|
+
const defaults = {
|
|
204
|
+
type,
|
|
205
|
+
level: LogLevels[type]
|
|
206
|
+
};
|
|
207
|
+
this.print(defaults, this.resolveInput(input, args));
|
|
208
|
+
}]));
|
|
209
|
+
return logger.extend(fns);
|
|
210
|
+
};
|
|
211
|
+
function getDefaultLogLevel() {
|
|
212
|
+
if (isDebug) return LogLevels.debug;
|
|
213
|
+
else if (isTest) return LogLevels.warn;
|
|
214
|
+
else return LogLevels.info;
|
|
3
215
|
}
|
|
4
216
|
|
|
5
|
-
|
|
217
|
+
//#endregion
|
|
218
|
+
export { BasicReporter, BreadcLogger, FancyReporter, FormatReporter, LogLevels, Logger, MockReporter };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@breadc/logger",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-beta.1",
|
|
4
4
|
"description": "CLI logger for Breadc",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"breadc",
|
|
@@ -23,24 +23,21 @@
|
|
|
23
23
|
"type": "module",
|
|
24
24
|
"exports": {
|
|
25
25
|
".": {
|
|
26
|
-
"
|
|
27
|
-
"import": "./dist/index.mjs"
|
|
28
|
-
"types": "./dist/index.d.ts"
|
|
26
|
+
"types": "./dist/index.d.mts",
|
|
27
|
+
"import": "./dist/index.mjs"
|
|
29
28
|
}
|
|
30
29
|
},
|
|
31
|
-
"main": "dist/index.cjs",
|
|
32
30
|
"module": "dist/index.mjs",
|
|
33
|
-
"types": "dist/index.d.
|
|
31
|
+
"types": "dist/index.d.mts",
|
|
34
32
|
"files": [
|
|
35
33
|
"dist"
|
|
36
34
|
],
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"std-env": "^3.10.0",
|
|
37
|
+
"@breadc/color": "1.0.0-beta.1"
|
|
40
38
|
},
|
|
41
39
|
"scripts": {
|
|
42
|
-
"build": "
|
|
43
|
-
"format": "prettier --write src/**/*.ts test/*.ts",
|
|
40
|
+
"build": "tsdown",
|
|
44
41
|
"test": "vitest",
|
|
45
42
|
"test:ci": "vitest --run",
|
|
46
43
|
"typecheck": "tsc --noEmit"
|
package/dist/index.cjs
DELETED
package/dist/index.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
type LogLevel = 'info' | 'warn' | 'error' | 'debug';
|
|
2
|
-
interface LogObject {
|
|
3
|
-
level?: LogLevel;
|
|
4
|
-
tag?: string;
|
|
5
|
-
date?: Date;
|
|
6
|
-
message?: string;
|
|
7
|
-
}
|
|
8
|
-
interface Reporter {
|
|
9
|
-
print: (log: LogObject) => void;
|
|
10
|
-
}
|
|
11
|
-
interface FormatOptions {
|
|
12
|
-
}
|
|
13
|
-
interface LoggerOptions {
|
|
14
|
-
reporter: Reporter[];
|
|
15
|
-
level: LogLevel;
|
|
16
|
-
format: FormatOptions;
|
|
17
|
-
stdout: NodeJS.WriteStream;
|
|
18
|
-
stderr: NodeJS.WriteStream;
|
|
19
|
-
}
|
|
20
|
-
declare function createLogger(options?: Partial<LoggerOptions>): {};
|
|
21
|
-
|
|
22
|
-
export { createLogger };
|