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