@egi/smart-log 1.0.0
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/LICENSE +21 -0
- package/README.md +206 -0
- package/package.json +29 -0
- package/smart-error.d.ts +22 -0
- package/smart-error.js +123 -0
- package/smart-log-api.d.ts +4 -0
- package/smart-log-api.js +21 -0
- package/smart-log-date-utils.d.ts +24 -0
- package/smart-log-date-utils.js +129 -0
- package/smart-log.d.ts +362 -0
- package/smart-log.js +516 -0
- package/smart-tools.d.ts +64 -0
- package/smart-tools.js +349 -0
- package/tsconfig.pro.tsbuildinfo +1 -0
package/smart-log.d.ts
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { DateTime } from "luxon";
|
|
2
|
+
export declare enum SmartSeverityLevel {
|
|
3
|
+
None = 0,
|
|
4
|
+
Fatal = 1,
|
|
5
|
+
Error = 2,
|
|
6
|
+
Warning = 3,
|
|
7
|
+
Info = 4,
|
|
8
|
+
Verbose = 5,
|
|
9
|
+
Debug = 6,
|
|
10
|
+
All = 9
|
|
11
|
+
}
|
|
12
|
+
export declare enum SmartLocation {
|
|
13
|
+
App = "APP",
|
|
14
|
+
Frontend = "FRONTEND",
|
|
15
|
+
Backend = "BACKEND",
|
|
16
|
+
Database = "DATABASE",
|
|
17
|
+
UpgradeManager = "UPGRADE-MANAGER"
|
|
18
|
+
}
|
|
19
|
+
export declare enum SmartLogFormatElement {
|
|
20
|
+
Severity = 0,
|
|
21
|
+
Type = 1,
|
|
22
|
+
Timestamp = 2,
|
|
23
|
+
Message = 3,
|
|
24
|
+
Location = 4
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Represents a single log entry. Passed to `ISmartLogDbWriter.write()`, `outputHook`, and `formatHook`.
|
|
28
|
+
*/
|
|
29
|
+
export interface LogMessage {
|
|
30
|
+
/** Additional data associated with the log entry (e.g. an Error object or debug metadata). */
|
|
31
|
+
data: unknown;
|
|
32
|
+
/** Caller location string, e.g. `"src/app.ts:42 (doSomething)"`. */
|
|
33
|
+
location: string;
|
|
34
|
+
/** The main log message text. */
|
|
35
|
+
message: string;
|
|
36
|
+
/** Severity level of the entry. */
|
|
37
|
+
severityLevel: SmartSeverityLevel;
|
|
38
|
+
/** Timestamp of the entry. */
|
|
39
|
+
timestamp: string | number | Date | DateTime;
|
|
40
|
+
/** Location type / category of the entry. */
|
|
41
|
+
type: SmartLocation;
|
|
42
|
+
/** User identifier stamped on the entry, taken from `SmartLogOptions.user`. */
|
|
43
|
+
user?: string | number;
|
|
44
|
+
/** When `true`, console output is suppressed for this entry. */
|
|
45
|
+
skipConsole?: boolean;
|
|
46
|
+
/** When `true`, the db writer is skipped for this entry. */
|
|
47
|
+
skipDatabase?: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Defines the format and structure of log messages for a `SmartLog` instance.
|
|
51
|
+
*/
|
|
52
|
+
export interface SmartLogFormat {
|
|
53
|
+
/**
|
|
54
|
+
* A string prepended to every log message. Example: `"[APP]"`.
|
|
55
|
+
*/
|
|
56
|
+
leading: string;
|
|
57
|
+
/**
|
|
58
|
+
* When `true`, the caller's location is included in the log message.
|
|
59
|
+
*/
|
|
60
|
+
location: boolean;
|
|
61
|
+
/**
|
|
62
|
+
* When `true`, severity codes and type characters are lowercased. Defaults to `false`.
|
|
63
|
+
*/
|
|
64
|
+
lowerCase: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Separator string placed between format elements (except before the message). Example: `" | "`.
|
|
67
|
+
*/
|
|
68
|
+
separator: string;
|
|
69
|
+
/**
|
|
70
|
+
* Ordered list of elements that make up the log line. Elements may be:
|
|
71
|
+
* - `SmartLogFormatElement` enum values
|
|
72
|
+
* - Fixed strings
|
|
73
|
+
* - `() => string` functions evaluated at log time
|
|
74
|
+
*/
|
|
75
|
+
sequence: (string | SmartLogFormatElement | (() => string))[];
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Per-message options passed to logging methods such as `info()`, `error()`, etc.
|
|
79
|
+
*/
|
|
80
|
+
export interface SmartMessageOptions {
|
|
81
|
+
/** Additional data to attach to the log entry. */
|
|
82
|
+
data?: unknown;
|
|
83
|
+
/**
|
|
84
|
+
* Overrides the auto-detected caller location for this message.
|
|
85
|
+
* Example: `"api.ts:42"`.
|
|
86
|
+
*/
|
|
87
|
+
location?: string;
|
|
88
|
+
/** When `true`, suppresses console output for this message only. */
|
|
89
|
+
skipConsole?: boolean;
|
|
90
|
+
/** When `true`, skips the db writer for this message only. */
|
|
91
|
+
skipDatabase?: boolean;
|
|
92
|
+
/** Overrides the current timestamp for this entry. */
|
|
93
|
+
timestamp?: string | number | Date | DateTime;
|
|
94
|
+
/**
|
|
95
|
+
* Location type for this message. Accepts a `SmartLocation` value or a custom string.
|
|
96
|
+
* Defaults to `SmartLocation.App`.
|
|
97
|
+
*/
|
|
98
|
+
type?: string;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Implement this interface to persist log entries to a store (e.g. a database table).
|
|
102
|
+
* Pass an instance via `SmartLogOptions.dbWriter` or `SmartLog.setDbWriter()`.
|
|
103
|
+
* Both sync and async implementations are supported.
|
|
104
|
+
*/
|
|
105
|
+
export interface ISmartLogDbWriter {
|
|
106
|
+
write(entry: LogMessage): Promise<void> | void;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Configuration options for a `SmartLog` instance.
|
|
110
|
+
*/
|
|
111
|
+
export interface SmartLogOptions extends Partial<SmartLogFormat> {
|
|
112
|
+
/**
|
|
113
|
+
* Persists log entries to a store. Both sync and async writers are supported.
|
|
114
|
+
* Pass `null` or omit to disable db logging.
|
|
115
|
+
*/
|
|
116
|
+
dbWriter?: ISmartLogDbWriter;
|
|
117
|
+
/**
|
|
118
|
+
* When `true`, routes console output to `console.error`, `console.warn`,
|
|
119
|
+
* `console.info`, or `console.debug` based on severity level.
|
|
120
|
+
* Defaults to `false` (all output goes through `console.log`).
|
|
121
|
+
*/
|
|
122
|
+
logChannels?: boolean;
|
|
123
|
+
/**
|
|
124
|
+
* Minimum severity level for processing log messages. Messages above this level are
|
|
125
|
+
* silently dropped. Defaults to `SmartSeverityLevel.Info`.
|
|
126
|
+
*/
|
|
127
|
+
logLevel?: SmartSeverityLevel;
|
|
128
|
+
/**
|
|
129
|
+
* A hook called after formatting, before console output. Use this for side effects
|
|
130
|
+
* such as writing to a file, sending to a webhook, or emitting events.
|
|
131
|
+
*
|
|
132
|
+
* - Receives the formatted text and the raw `LogMessage`.
|
|
133
|
+
* - Return `false` (sync or async) to suppress console output for this entry.
|
|
134
|
+
* - Use `formatHook` instead if you only need to transform the text.
|
|
135
|
+
*/
|
|
136
|
+
outputHook?: (text: string, message: LogMessage) => false | void | Promise<false | void>;
|
|
137
|
+
/**
|
|
138
|
+
* A custom function that provides an `Error` for stack trace inspection.
|
|
139
|
+
* Useful when the automatic caller detection resolves to the wrong frame,
|
|
140
|
+
* e.g. inside a wrapper class.
|
|
141
|
+
*/
|
|
142
|
+
stackProvider?: () => Error;
|
|
143
|
+
/**
|
|
144
|
+
* User ID or username stamped on every log entry. Defaults to the OS username.
|
|
145
|
+
*/
|
|
146
|
+
user?: string | number;
|
|
147
|
+
/**
|
|
148
|
+
* Transforms the formatted log text before it is written to the console.
|
|
149
|
+
* Must return the (modified) string. Called after `outputHook`.
|
|
150
|
+
* Use `outputHook` instead if you need side effects or want to suppress output.
|
|
151
|
+
*/
|
|
152
|
+
formatHook?: (text: string, message: LogMessage) => string;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* A logging utility with severity-level control, flexible formatting, stack trace
|
|
156
|
+
* integration, and optional persistence via `ISmartLogDbWriter`.
|
|
157
|
+
*
|
|
158
|
+
* A default singleton `smartLog` is exported for convenience. Create additional
|
|
159
|
+
* instances for separate concerns (e.g. per-module loggers with different levels).
|
|
160
|
+
*
|
|
161
|
+
* ```typescript
|
|
162
|
+
* import { smartLog, SmartLog, SmartSeverityLevel } from "@egi/smart-log";
|
|
163
|
+
*
|
|
164
|
+
* smartLog.info("Application started");
|
|
165
|
+
*
|
|
166
|
+
* const verbose = new SmartLog({ logLevel: SmartSeverityLevel.Debug });
|
|
167
|
+
* verbose.debug("detailed trace");
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
export declare class SmartLog {
|
|
171
|
+
private static severityLevelToCode;
|
|
172
|
+
private dbWriter;
|
|
173
|
+
private includeLocation;
|
|
174
|
+
private isLogging;
|
|
175
|
+
private logFifo;
|
|
176
|
+
private logFormat;
|
|
177
|
+
private logLevel;
|
|
178
|
+
private outputHook;
|
|
179
|
+
private readonly projectRoot;
|
|
180
|
+
private stackProvider;
|
|
181
|
+
private timezone;
|
|
182
|
+
private useLogChannels;
|
|
183
|
+
private userId;
|
|
184
|
+
private formatHook;
|
|
185
|
+
constructor();
|
|
186
|
+
constructor(options: SmartLogOptions);
|
|
187
|
+
/**
|
|
188
|
+
* Overrides the stack provider used for automatic caller location detection.
|
|
189
|
+
* Useful when `SmartLog` is wrapped in another class and the default stack
|
|
190
|
+
* resolution points to the wrapper instead of the actual call site.
|
|
191
|
+
*
|
|
192
|
+
* @param stackProvider - A function that returns a fresh `Error` at the call site.
|
|
193
|
+
*/
|
|
194
|
+
setStackProvider(stackProvider: () => Error): void;
|
|
195
|
+
/**
|
|
196
|
+
* Sets a text transformation hook. Called with the fully formatted log line before
|
|
197
|
+
* console output; must return the (modified) string.
|
|
198
|
+
*
|
|
199
|
+
* Use `setOutputHook()` instead if you need side effects or want to suppress output.
|
|
200
|
+
*
|
|
201
|
+
* @param hook - Function receiving `(text, message)` and returning the transformed text.
|
|
202
|
+
*/
|
|
203
|
+
setFormatHook(hook: (text: string, message: LogMessage) => string): void;
|
|
204
|
+
/**
|
|
205
|
+
* Sets a general-purpose output hook. Called after formatting and before console output.
|
|
206
|
+
* Use this for side effects such as writing to a file, calling a webhook, or emitting events.
|
|
207
|
+
*
|
|
208
|
+
* Return `false` (or resolve a Promise to `false`) to suppress console output for the entry.
|
|
209
|
+
* Any other return value (including `void`) leaves console output enabled.
|
|
210
|
+
*
|
|
211
|
+
* @param hook - Function receiving `(text, message)`. May return `false | void`
|
|
212
|
+
* or a `Promise` of the same.
|
|
213
|
+
*/
|
|
214
|
+
setOutputHook(hook: (text: string, message: LogMessage) => false | void | Promise<false | void>): void;
|
|
215
|
+
/**
|
|
216
|
+
* Configures the log format. Any property omitted in `format` retains its current value.
|
|
217
|
+
* The default sequence is: `Severity - Type - Timestamp: Message [Location]`.
|
|
218
|
+
*
|
|
219
|
+
* @param format - Partial `SmartLogFormat` to merge into the current format.
|
|
220
|
+
*/
|
|
221
|
+
setLogFormat(format?: Partial<SmartLogFormat>): void;
|
|
222
|
+
/**
|
|
223
|
+
* Returns the current minimum log severity level.
|
|
224
|
+
*/
|
|
225
|
+
getLogLevel(): SmartSeverityLevel;
|
|
226
|
+
/**
|
|
227
|
+
* Sets the minimum log severity level. Messages with a higher level number are dropped.
|
|
228
|
+
*
|
|
229
|
+
* @param level - The new minimum level.
|
|
230
|
+
*/
|
|
231
|
+
setLogLevel(level: SmartSeverityLevel): void;
|
|
232
|
+
/**
|
|
233
|
+
* Returns `true` if a db writer is currently set.
|
|
234
|
+
*/
|
|
235
|
+
hasDbWriter(): boolean;
|
|
236
|
+
/**
|
|
237
|
+
* Sets or replaces the db writer. Pass `null` to disable db logging.
|
|
238
|
+
*
|
|
239
|
+
* @param dbWriter - An `ISmartLogDbWriter` implementation, or `null` to clear.
|
|
240
|
+
*/
|
|
241
|
+
setDbWriter(dbWriter: ISmartLogDbWriter | null): void;
|
|
242
|
+
/**
|
|
243
|
+
* Sets the timezone used when formatting timestamps in log output.
|
|
244
|
+
* Accepts any IANA timezone identifier (e.g. `"UTC"`, `"Europe/Berlin"`).
|
|
245
|
+
*
|
|
246
|
+
* @param timezone - IANA timezone string.
|
|
247
|
+
*/
|
|
248
|
+
setTimezone(timezone: string): void;
|
|
249
|
+
/**
|
|
250
|
+
* Controls whether the caller's source location is appended to each log line.
|
|
251
|
+
*
|
|
252
|
+
* @param includeLocation - `true` to include location, `false` to omit.
|
|
253
|
+
*/
|
|
254
|
+
setIncludeLocation(includeLocation: boolean): void;
|
|
255
|
+
/**
|
|
256
|
+
* Returns `true` if caller location is currently included in log output.
|
|
257
|
+
*/
|
|
258
|
+
getIncludeLocation(): boolean;
|
|
259
|
+
/**
|
|
260
|
+
* Sets the user identifier stamped on every subsequent log entry.
|
|
261
|
+
*
|
|
262
|
+
* @param userId - A string or numeric user identifier.
|
|
263
|
+
*/
|
|
264
|
+
setUserId(userId: string | number): void;
|
|
265
|
+
/**
|
|
266
|
+
* Logs a message at `Debug` level.
|
|
267
|
+
* Accepts a string, an `Error`, or a string with `SmartMessageOptions`.
|
|
268
|
+
*/
|
|
269
|
+
debug(error: Error): void;
|
|
270
|
+
debug(message: string): void;
|
|
271
|
+
debug(message: string, error: Error): void;
|
|
272
|
+
debug(message: string | Error, options: SmartMessageOptions): void;
|
|
273
|
+
/**
|
|
274
|
+
* Logs a message at `Error` level.
|
|
275
|
+
* Accepts a string, an `Error`, or a string with `SmartMessageOptions`.
|
|
276
|
+
*/
|
|
277
|
+
error(error: Error): void;
|
|
278
|
+
error(message: string): void;
|
|
279
|
+
error(message: string, error: Error): void;
|
|
280
|
+
error(message: string | Error, options: SmartMessageOptions): void;
|
|
281
|
+
/**
|
|
282
|
+
* Logs a message at `Fatal` level.
|
|
283
|
+
* Accepts a string, an `Error`, or a string with `SmartMessageOptions`.
|
|
284
|
+
*/
|
|
285
|
+
fatal(error: Error): void;
|
|
286
|
+
fatal(message: string): void;
|
|
287
|
+
fatal(message: string, error: Error): void;
|
|
288
|
+
fatal(message: string | Error, options: SmartMessageOptions): void;
|
|
289
|
+
/**
|
|
290
|
+
* Logs a message at `Info` level.
|
|
291
|
+
* Accepts a string, an `Error`, or a string with `SmartMessageOptions`.
|
|
292
|
+
*/
|
|
293
|
+
info(error: Error): void;
|
|
294
|
+
info(message: string): void;
|
|
295
|
+
info(message: string, error: Error): void;
|
|
296
|
+
info(message: string | Error, options: SmartMessageOptions): void;
|
|
297
|
+
/**
|
|
298
|
+
* Logs a message at `Verbose` level.
|
|
299
|
+
* Accepts a string, an `Error`, or a string with `SmartMessageOptions`.
|
|
300
|
+
*/
|
|
301
|
+
verbose(error: Error): void;
|
|
302
|
+
verbose(message: string): void;
|
|
303
|
+
verbose(message: string, error: Error): void;
|
|
304
|
+
verbose(message: string | Error, options: SmartMessageOptions): void;
|
|
305
|
+
/**
|
|
306
|
+
* Logs a message at `Warning` level.
|
|
307
|
+
* Accepts a string, an `Error`, or a string with `SmartMessageOptions`.
|
|
308
|
+
*/
|
|
309
|
+
warning(error: Error): void;
|
|
310
|
+
warning(message: string): void;
|
|
311
|
+
warning(message: string, error: Error): void;
|
|
312
|
+
warning(message: string | Error, options: SmartMessageOptions): void;
|
|
313
|
+
/**
|
|
314
|
+
* Writes a message with no severity level (always output regardless of `logLevel`).
|
|
315
|
+
* Accepts a string, an `Error`, or a string with `SmartMessageOptions`.
|
|
316
|
+
*/
|
|
317
|
+
write(error: Error): void;
|
|
318
|
+
write(message: string): void;
|
|
319
|
+
write(message: string, error: Error): void;
|
|
320
|
+
write(message: string | Error, options: SmartMessageOptions): void;
|
|
321
|
+
/**
|
|
322
|
+
* Core logging method. Normalises the arguments, resolves the caller location,
|
|
323
|
+
* and dispatches to `log()`.
|
|
324
|
+
*
|
|
325
|
+
* @param severityLevel - Severity of the message, or `null` for unseveritied `write()` calls.
|
|
326
|
+
* @param messageOrError - The message string or an `Error` whose `.message` is used.
|
|
327
|
+
* @param optionsOrError - Per-message options, or an `Error` stored as `data`.
|
|
328
|
+
*/
|
|
329
|
+
writeLog(severityLevel: SmartSeverityLevel, messageOrError: string | Error, optionsOrError: SmartMessageOptions | Error): void;
|
|
330
|
+
/**
|
|
331
|
+
* Dispatches a fully-formed `LogMessage`. Filters by `logLevel`, formats the text,
|
|
332
|
+
* runs `outputHook` and `formatHook`, writes to the console (unless suppressed),
|
|
333
|
+
* and forwards to the db writer.
|
|
334
|
+
*
|
|
335
|
+
* Calls during an in-progress async db write are queued and replayed in order.
|
|
336
|
+
*
|
|
337
|
+
* @param messageObj - The complete log entry to process.
|
|
338
|
+
*/
|
|
339
|
+
log(messageObj: LogMessage): void;
|
|
340
|
+
/**
|
|
341
|
+
* Formats a `LogMessage` into a string using the current log format sequence.
|
|
342
|
+
* Optionally accepts a custom `sequence` to override the instance format for this call.
|
|
343
|
+
*
|
|
344
|
+
* @param message - The log entry to format.
|
|
345
|
+
* @param sequence - Optional sequence override; uses the instance format when omitted.
|
|
346
|
+
* @returns The formatted log line string.
|
|
347
|
+
*/
|
|
348
|
+
formatMessage(message: LogMessage, sequence?: (string | SmartLogFormatElement | (() => string))[]): string;
|
|
349
|
+
/**
|
|
350
|
+
* Returns the single-letter severity code for a given `SmartSeverityLevel`.
|
|
351
|
+
* Unknown levels fall back to the `Info` code.
|
|
352
|
+
*
|
|
353
|
+
* @param level - The severity level to look up.
|
|
354
|
+
* @returns A single uppercase letter: `F`, `E`, `W`, `I`, `D`, etc.
|
|
355
|
+
*/
|
|
356
|
+
severityCodeFromLevel(level: SmartSeverityLevel): string;
|
|
357
|
+
private writeToConsole;
|
|
358
|
+
private printToConsole;
|
|
359
|
+
private stringifyData;
|
|
360
|
+
private nextStackEntry;
|
|
361
|
+
}
|
|
362
|
+
export declare const smartLog: SmartLog;
|