@breadc/logger 0.9.7-beta.0 → 0.9.7

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.cjs CHANGED
@@ -1,7 +1,246 @@
1
1
  'use strict';
2
2
 
3
- function createLogger(options = {}) {
4
- return {};
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
+ }
5
238
  }
6
239
 
7
- exports.createLogger = createLogger;
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;
@@ -0,0 +1,96 @@
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 };
@@ -0,0 +1,96 @@
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 CHANGED
@@ -1,22 +1,96 @@
1
- type LogLevel = 'info' | 'warn' | 'error' | 'debug';
2
- interface LogObject {
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 {
3
17
  level?: LogLevel;
18
+ type?: LogType;
4
19
  tag?: string;
5
20
  date?: Date;
6
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;
7
32
  }
8
33
  interface Reporter {
9
- print: (log: LogObject) => void;
34
+ print: (log: LogObject, ctx: PrintContext) => void;
10
35
  }
36
+ /**
37
+ * @see https://nodejs.org/api/util.html#util_util_inspect_object_showhidden_depth_colors
38
+ */
11
39
  interface FormatOptions {
40
+ columns?: number;
41
+ date?: boolean;
42
+ colors?: boolean;
43
+ compact?: boolean | number;
44
+ [key: string]: unknown;
12
45
  }
13
- interface LoggerOptions {
14
- reporter: Reporter[];
15
- level: LogLevel;
16
- format: FormatOptions;
17
- stdout: NodeJS.WriteStream;
18
- stderr: NodeJS.WriteStream;
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;
19
75
  }
20
- declare function createLogger(options?: Partial<LoggerOptions>): {};
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>;
21
95
 
22
- export { createLogger };
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.mjs CHANGED
@@ -1,5 +1,238 @@
1
- function createLogger(options = {}) {
2
- return {};
1
+ import { hasTTY, isCI, isTest, isDebug } from 'std-env';
2
+ import { formatWithOptions } from 'node:util';
3
+
4
+ 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
19
+ };
20
+
21
+ const bracket = (text) => {
22
+ if (text) {
23
+ return `[${text}]`;
24
+ } else {
25
+ return void 0;
26
+ }
27
+ };
28
+
29
+ function writeStream(data, stream) {
30
+ const write = stream.__write || stream.write;
31
+ return write.call(stream, data);
32
+ }
33
+
34
+ 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
+ };
58
+ };
59
+
60
+ 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
+ };
84
+ };
85
+
86
+ const MockReporter = (reporter, history = []) => {
87
+ return {
88
+ history,
89
+ print(object, ctx) {
90
+ history.push({ output: reporter.formatLogObject(object, ctx), object });
91
+ }
92
+ };
93
+ };
94
+
95
+ const FormatReporter = (options) => {
96
+ const isFancy = options.fancy === true || options.fancy === void 0 && hasTTY;
97
+ return isFancy && !(isCI || isTest) ? FancyReporter() : BasicReporter();
98
+ };
99
+
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;
105
+ };
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
+
188
+ 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);
227
+ };
228
+ function getDefaultLogLevel() {
229
+ if (isDebug) {
230
+ return LogLevels.debug;
231
+ } else if (isTest) {
232
+ return LogLevels.warn;
233
+ } else {
234
+ return LogLevels.info;
235
+ }
3
236
  }
4
237
 
5
- export { createLogger };
238
+ 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-beta.0",
3
+ "version": "0.9.7",
4
4
  "description": "CLI logger for Breadc",
5
5
  "keywords": [
6
6
  "breadc",
@@ -34,9 +34,13 @@
34
34
  "files": [
35
35
  "dist"
36
36
  ],
37
+ "dependencies": {
38
+ "std-env": "^3.4.3",
39
+ "@breadc/color": "0.9.7"
40
+ },
37
41
  "devDependencies": {
38
- "@types/node": "^18.17.5",
39
- "vitest": "0.33.0"
42
+ "@types/node": "^20.8.7",
43
+ "vitest": "0.34.6"
40
44
  },
41
45
  "scripts": {
42
46
  "build": "unbuild",