@momentumcms/logger 0.0.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/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/index.cjs +288 -0
- package/package.json +29 -0
- package/src/index.d.ts +12 -0
- package/src/lib/ansi-colors.d.ts +28 -0
- package/src/lib/formatters.d.ts +25 -0
- package/src/lib/log-level.d.ts +10 -0
- package/src/lib/logger-config.types.d.ts +28 -0
- package/src/lib/logger-singleton.d.ts +30 -0
- package/src/lib/logger.d.ts +45 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present Momentum CMS Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.cjs
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// libs/logger/src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
ANSI: () => ANSI,
|
|
24
|
+
LOG_LEVEL_VALUES: () => LOG_LEVEL_VALUES,
|
|
25
|
+
MomentumLogger: () => MomentumLogger,
|
|
26
|
+
colorize: () => colorize,
|
|
27
|
+
createLogger: () => createLogger,
|
|
28
|
+
getMomentumLogger: () => getMomentumLogger,
|
|
29
|
+
initializeMomentumLogger: () => initializeMomentumLogger,
|
|
30
|
+
jsonFormatter: () => jsonFormatter,
|
|
31
|
+
prettyFormatter: () => prettyFormatter,
|
|
32
|
+
resetMomentumLogger: () => resetMomentumLogger,
|
|
33
|
+
resolveLoggingConfig: () => resolveLoggingConfig,
|
|
34
|
+
shouldLog: () => shouldLog,
|
|
35
|
+
supportsColor: () => supportsColor
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(src_exports);
|
|
38
|
+
|
|
39
|
+
// libs/logger/src/lib/log-level.ts
|
|
40
|
+
var LOG_LEVEL_VALUES = {
|
|
41
|
+
debug: 0,
|
|
42
|
+
info: 1,
|
|
43
|
+
warn: 2,
|
|
44
|
+
error: 3,
|
|
45
|
+
fatal: 4,
|
|
46
|
+
silent: 5
|
|
47
|
+
};
|
|
48
|
+
function shouldLog(messageLevel, configuredLevel) {
|
|
49
|
+
return LOG_LEVEL_VALUES[messageLevel] >= LOG_LEVEL_VALUES[configuredLevel];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// libs/logger/src/lib/ansi-colors.ts
|
|
53
|
+
var ANSI = {
|
|
54
|
+
reset: "\x1B[0m",
|
|
55
|
+
bold: "\x1B[1m",
|
|
56
|
+
dim: "\x1B[2m",
|
|
57
|
+
// Foreground colors
|
|
58
|
+
red: "\x1B[31m",
|
|
59
|
+
green: "\x1B[32m",
|
|
60
|
+
yellow: "\x1B[33m",
|
|
61
|
+
blue: "\x1B[34m",
|
|
62
|
+
magenta: "\x1B[35m",
|
|
63
|
+
cyan: "\x1B[36m",
|
|
64
|
+
white: "\x1B[37m",
|
|
65
|
+
gray: "\x1B[90m",
|
|
66
|
+
// Background colors
|
|
67
|
+
bgRed: "\x1B[41m",
|
|
68
|
+
bgYellow: "\x1B[43m"
|
|
69
|
+
};
|
|
70
|
+
function colorize(text, ...codes) {
|
|
71
|
+
if (codes.length === 0)
|
|
72
|
+
return text;
|
|
73
|
+
return `${codes.join("")}${text}${ANSI.reset}`;
|
|
74
|
+
}
|
|
75
|
+
function supportsColor() {
|
|
76
|
+
if (process.env["FORCE_COLOR"] === "1")
|
|
77
|
+
return true;
|
|
78
|
+
if (process.env["NO_COLOR"] !== void 0)
|
|
79
|
+
return false;
|
|
80
|
+
if (process.env["TERM"] === "dumb")
|
|
81
|
+
return false;
|
|
82
|
+
return process.stdout.isTTY === true;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// libs/logger/src/lib/formatters.ts
|
|
86
|
+
var LEVEL_COLORS = {
|
|
87
|
+
debug: [ANSI.dim, ANSI.gray],
|
|
88
|
+
info: [ANSI.cyan],
|
|
89
|
+
warn: [ANSI.yellow],
|
|
90
|
+
error: [ANSI.red],
|
|
91
|
+
fatal: [ANSI.bold, ANSI.white, ANSI.bgRed]
|
|
92
|
+
};
|
|
93
|
+
function padLevel(level) {
|
|
94
|
+
return level.toUpperCase().padEnd(5);
|
|
95
|
+
}
|
|
96
|
+
function formatTimestamp(date) {
|
|
97
|
+
const y = date.getFullYear();
|
|
98
|
+
const mo = String(date.getMonth() + 1).padStart(2, "0");
|
|
99
|
+
const d = String(date.getDate()).padStart(2, "0");
|
|
100
|
+
const h = String(date.getHours()).padStart(2, "0");
|
|
101
|
+
const mi = String(date.getMinutes()).padStart(2, "0");
|
|
102
|
+
const s = String(date.getSeconds()).padStart(2, "0");
|
|
103
|
+
const ms = String(date.getMilliseconds()).padStart(3, "0");
|
|
104
|
+
return `${y}-${mo}-${d} ${h}:${mi}:${s}.${ms}`;
|
|
105
|
+
}
|
|
106
|
+
function formatData(data) {
|
|
107
|
+
const entries = Object.entries(data);
|
|
108
|
+
if (entries.length === 0)
|
|
109
|
+
return "";
|
|
110
|
+
return " " + entries.map(([k, v]) => `${k}=${typeof v === "string" ? v : JSON.stringify(v)}`).join(" ");
|
|
111
|
+
}
|
|
112
|
+
function prettyFormatter(entry) {
|
|
113
|
+
const useColor = supportsColor();
|
|
114
|
+
const level = entry.level;
|
|
115
|
+
const ts = formatTimestamp(entry.timestamp);
|
|
116
|
+
const levelStr = padLevel(entry.level);
|
|
117
|
+
const ctx = `[${entry.context}]`;
|
|
118
|
+
const msg = entry.message;
|
|
119
|
+
const enrichmentStr = entry.enrichments ? formatData(entry.enrichments) : "";
|
|
120
|
+
const dataStr = entry.data ? formatData(entry.data) : "";
|
|
121
|
+
const extra = `${enrichmentStr}${dataStr}`;
|
|
122
|
+
if (useColor) {
|
|
123
|
+
const colors = LEVEL_COLORS[level];
|
|
124
|
+
const coloredLevel = colorize(levelStr, ...colors);
|
|
125
|
+
const coloredCtx = colorize(ctx, ANSI.magenta);
|
|
126
|
+
const coloredTs = colorize(ts, ANSI.gray);
|
|
127
|
+
return `${coloredTs} ${coloredLevel} ${coloredCtx} ${msg}${extra}
|
|
128
|
+
`;
|
|
129
|
+
}
|
|
130
|
+
return `${ts} ${levelStr} ${ctx} ${msg}${extra}
|
|
131
|
+
`;
|
|
132
|
+
}
|
|
133
|
+
function jsonFormatter(entry) {
|
|
134
|
+
const output = {
|
|
135
|
+
timestamp: entry.timestamp.toISOString(),
|
|
136
|
+
level: entry.level,
|
|
137
|
+
context: entry.context,
|
|
138
|
+
message: entry.message
|
|
139
|
+
};
|
|
140
|
+
if (entry.enrichments && Object.keys(entry.enrichments).length > 0) {
|
|
141
|
+
Object.assign(output, entry.enrichments);
|
|
142
|
+
}
|
|
143
|
+
if (entry.data && Object.keys(entry.data).length > 0) {
|
|
144
|
+
output["data"] = entry.data;
|
|
145
|
+
}
|
|
146
|
+
return JSON.stringify(output) + "\n";
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// libs/logger/src/lib/logger-config.types.ts
|
|
150
|
+
function resolveLoggingConfig(config) {
|
|
151
|
+
return {
|
|
152
|
+
level: config?.level ?? "info",
|
|
153
|
+
format: config?.format ?? "pretty",
|
|
154
|
+
timestamps: config?.timestamps ?? true,
|
|
155
|
+
output: config?.output ?? ((msg) => {
|
|
156
|
+
process.stdout.write(msg);
|
|
157
|
+
}),
|
|
158
|
+
errorOutput: config?.errorOutput ?? ((msg) => {
|
|
159
|
+
process.stderr.write(msg);
|
|
160
|
+
})
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// libs/logger/src/lib/logger.ts
|
|
165
|
+
var ERROR_LEVELS = /* @__PURE__ */ new Set(["warn", "error", "fatal"]);
|
|
166
|
+
var MomentumLogger = class _MomentumLogger {
|
|
167
|
+
static {
|
|
168
|
+
this.enrichers = [];
|
|
169
|
+
}
|
|
170
|
+
constructor(context, config) {
|
|
171
|
+
this.context = context;
|
|
172
|
+
this.config = isResolvedConfig(config) ? config : resolveLoggingConfig(config);
|
|
173
|
+
this.formatter = this.config.format === "json" ? jsonFormatter : prettyFormatter;
|
|
174
|
+
}
|
|
175
|
+
debug(message, data) {
|
|
176
|
+
this.log("debug", message, data);
|
|
177
|
+
}
|
|
178
|
+
info(message, data) {
|
|
179
|
+
this.log("info", message, data);
|
|
180
|
+
}
|
|
181
|
+
warn(message, data) {
|
|
182
|
+
this.log("warn", message, data);
|
|
183
|
+
}
|
|
184
|
+
error(message, data) {
|
|
185
|
+
this.log("error", message, data);
|
|
186
|
+
}
|
|
187
|
+
fatal(message, data) {
|
|
188
|
+
this.log("fatal", message, data);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Creates a child logger with a sub-context.
|
|
192
|
+
* e.g., `Momentum:DB` → `Momentum:DB:Migrate`
|
|
193
|
+
*/
|
|
194
|
+
child(subContext) {
|
|
195
|
+
return new _MomentumLogger(`${this.context}:${subContext}`, this.config);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Registers a global enricher that adds extra fields to all log entries.
|
|
199
|
+
*/
|
|
200
|
+
static registerEnricher(enricher) {
|
|
201
|
+
_MomentumLogger.enrichers.push(enricher);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Removes a previously registered enricher.
|
|
205
|
+
*/
|
|
206
|
+
static removeEnricher(enricher) {
|
|
207
|
+
const index = _MomentumLogger.enrichers.indexOf(enricher);
|
|
208
|
+
if (index >= 0) {
|
|
209
|
+
_MomentumLogger.enrichers.splice(index, 1);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Clears all registered enrichers. Primarily for testing.
|
|
214
|
+
*/
|
|
215
|
+
static clearEnrichers() {
|
|
216
|
+
_MomentumLogger.enrichers.length = 0;
|
|
217
|
+
}
|
|
218
|
+
log(level, message, data) {
|
|
219
|
+
if (!shouldLog(level, this.config.level))
|
|
220
|
+
return;
|
|
221
|
+
const enrichments = this.collectEnrichments();
|
|
222
|
+
const entry = {
|
|
223
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
224
|
+
level,
|
|
225
|
+
context: this.context,
|
|
226
|
+
message,
|
|
227
|
+
data,
|
|
228
|
+
enrichments: Object.keys(enrichments).length > 0 ? enrichments : void 0
|
|
229
|
+
};
|
|
230
|
+
const formatted = this.formatter(entry);
|
|
231
|
+
if (ERROR_LEVELS.has(level)) {
|
|
232
|
+
this.config.errorOutput(formatted);
|
|
233
|
+
} else {
|
|
234
|
+
this.config.output(formatted);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
collectEnrichments() {
|
|
238
|
+
const result = {};
|
|
239
|
+
for (const enricher of _MomentumLogger.enrichers) {
|
|
240
|
+
Object.assign(result, enricher.enrich());
|
|
241
|
+
}
|
|
242
|
+
return result;
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
function isResolvedConfig(config) {
|
|
246
|
+
if (!config)
|
|
247
|
+
return false;
|
|
248
|
+
return typeof config.level === "string" && typeof config.format === "string" && typeof config.timestamps === "boolean" && // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- type guard narrows union
|
|
249
|
+
typeof config.output === "function" && // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- type guard narrows union
|
|
250
|
+
typeof config.errorOutput === "function";
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// libs/logger/src/lib/logger-singleton.ts
|
|
254
|
+
var loggerInstance = null;
|
|
255
|
+
var ROOT_CONTEXT = "Momentum";
|
|
256
|
+
function initializeMomentumLogger(config) {
|
|
257
|
+
loggerInstance = new MomentumLogger(ROOT_CONTEXT, config);
|
|
258
|
+
return loggerInstance;
|
|
259
|
+
}
|
|
260
|
+
function getMomentumLogger() {
|
|
261
|
+
if (!loggerInstance) {
|
|
262
|
+
loggerInstance = new MomentumLogger(ROOT_CONTEXT);
|
|
263
|
+
}
|
|
264
|
+
return loggerInstance;
|
|
265
|
+
}
|
|
266
|
+
function createLogger(context) {
|
|
267
|
+
return getMomentumLogger().child(context);
|
|
268
|
+
}
|
|
269
|
+
function resetMomentumLogger() {
|
|
270
|
+
loggerInstance = null;
|
|
271
|
+
MomentumLogger.clearEnrichers();
|
|
272
|
+
}
|
|
273
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
274
|
+
0 && (module.exports = {
|
|
275
|
+
ANSI,
|
|
276
|
+
LOG_LEVEL_VALUES,
|
|
277
|
+
MomentumLogger,
|
|
278
|
+
colorize,
|
|
279
|
+
createLogger,
|
|
280
|
+
getMomentumLogger,
|
|
281
|
+
initializeMomentumLogger,
|
|
282
|
+
jsonFormatter,
|
|
283
|
+
prettyFormatter,
|
|
284
|
+
resetMomentumLogger,
|
|
285
|
+
resolveLoggingConfig,
|
|
286
|
+
shouldLog,
|
|
287
|
+
supportsColor
|
|
288
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@momentumcms/logger",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Structured logging for Momentum CMS",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Momentum CMS Contributors",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/momentum-cms/momentum-cms.git",
|
|
10
|
+
"directory": "libs/logger"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/momentum-cms/momentum-cms#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/momentum-cms/momentum-cms/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"cms",
|
|
18
|
+
"momentum-cms",
|
|
19
|
+
"logging",
|
|
20
|
+
"logger"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=18"
|
|
24
|
+
},
|
|
25
|
+
"type": "commonjs",
|
|
26
|
+
"main": "./index.cjs",
|
|
27
|
+
"types": "./src/index.d.ts",
|
|
28
|
+
"dependencies": {}
|
|
29
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @momentumcms/logger
|
|
3
|
+
*
|
|
4
|
+
* Colorful, configurable logging engine for Momentum CMS.
|
|
5
|
+
* Built from scratch with ANSI color support, log levels, and child loggers.
|
|
6
|
+
*/
|
|
7
|
+
export { MomentumLogger, type LogEnricher } from './lib/logger';
|
|
8
|
+
export { initializeMomentumLogger, getMomentumLogger, resetMomentumLogger, createLogger, } from './lib/logger-singleton';
|
|
9
|
+
export { type LogLevel, shouldLog, LOG_LEVEL_VALUES } from './lib/log-level';
|
|
10
|
+
export { type LoggingConfig, type LogFormat, type ResolvedLoggingConfig, resolveLoggingConfig, } from './lib/logger-config.types';
|
|
11
|
+
export { type LogEntry, type LogFormatter, prettyFormatter, jsonFormatter } from './lib/formatters';
|
|
12
|
+
export { ANSI, colorize, supportsColor } from './lib/ansi-colors';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ANSI escape codes for colorful terminal output.
|
|
3
|
+
* Built from scratch — no external dependencies.
|
|
4
|
+
*/
|
|
5
|
+
export declare const ANSI: {
|
|
6
|
+
readonly reset: "\u001B[0m";
|
|
7
|
+
readonly bold: "\u001B[1m";
|
|
8
|
+
readonly dim: "\u001B[2m";
|
|
9
|
+
readonly red: "\u001B[31m";
|
|
10
|
+
readonly green: "\u001B[32m";
|
|
11
|
+
readonly yellow: "\u001B[33m";
|
|
12
|
+
readonly blue: "\u001B[34m";
|
|
13
|
+
readonly magenta: "\u001B[35m";
|
|
14
|
+
readonly cyan: "\u001B[36m";
|
|
15
|
+
readonly white: "\u001B[37m";
|
|
16
|
+
readonly gray: "\u001B[90m";
|
|
17
|
+
readonly bgRed: "\u001B[41m";
|
|
18
|
+
readonly bgYellow: "\u001B[43m";
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Wraps text with ANSI escape codes and a reset suffix.
|
|
22
|
+
*/
|
|
23
|
+
export declare function colorize(text: string, ...codes: string[]): string;
|
|
24
|
+
/**
|
|
25
|
+
* Checks if the current environment supports color output.
|
|
26
|
+
* Respects NO_COLOR and FORCE_COLOR environment variables.
|
|
27
|
+
*/
|
|
28
|
+
export declare function supportsColor(): boolean;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Log entry formatters for pretty (colorful) and JSON output.
|
|
3
|
+
*/
|
|
4
|
+
import type { LogLevel } from './log-level';
|
|
5
|
+
export interface LogEntry {
|
|
6
|
+
timestamp: Date;
|
|
7
|
+
level: LogLevel;
|
|
8
|
+
context: string;
|
|
9
|
+
message: string;
|
|
10
|
+
data?: Record<string, unknown>;
|
|
11
|
+
enrichments?: Record<string, unknown>;
|
|
12
|
+
}
|
|
13
|
+
export type LogFormatter = (entry: LogEntry) => string;
|
|
14
|
+
/**
|
|
15
|
+
* Pretty formatter with colorful aligned output.
|
|
16
|
+
*
|
|
17
|
+
* Output: `2026-02-07 10:23:45.123 INFO [Momentum:DB] Message here`
|
|
18
|
+
*/
|
|
19
|
+
export declare function prettyFormatter(entry: LogEntry): string;
|
|
20
|
+
/**
|
|
21
|
+
* JSON formatter for structured logging.
|
|
22
|
+
*
|
|
23
|
+
* Output: `{"timestamp":"...","level":"info","context":"Momentum:DB","message":"..."}`
|
|
24
|
+
*/
|
|
25
|
+
export declare function jsonFormatter(entry: LogEntry): string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Log level definitions and utilities.
|
|
3
|
+
*/
|
|
4
|
+
export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'fatal' | 'silent';
|
|
5
|
+
export declare const LOG_LEVEL_VALUES: Readonly<Record<LogLevel, number>>;
|
|
6
|
+
/**
|
|
7
|
+
* Determines if a message at the given level should be logged
|
|
8
|
+
* based on the configured minimum level.
|
|
9
|
+
*/
|
|
10
|
+
export declare function shouldLog(messageLevel: LogLevel, configuredLevel: LogLevel): boolean;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logger configuration types.
|
|
3
|
+
*/
|
|
4
|
+
import type { LogLevel } from './log-level';
|
|
5
|
+
export type LogFormat = 'pretty' | 'json';
|
|
6
|
+
export interface LoggingConfig {
|
|
7
|
+
/** Minimum log level. @default 'info' */
|
|
8
|
+
level?: LogLevel;
|
|
9
|
+
/** Output format. @default 'pretty' */
|
|
10
|
+
format?: LogFormat;
|
|
11
|
+
/** Whether to include timestamps. @default true */
|
|
12
|
+
timestamps?: boolean;
|
|
13
|
+
/** Custom output function for debug/info (defaults to process.stdout.write) */
|
|
14
|
+
output?: (message: string) => void;
|
|
15
|
+
/** Custom error output function for warn/error/fatal (defaults to process.stderr.write) */
|
|
16
|
+
errorOutput?: (message: string) => void;
|
|
17
|
+
}
|
|
18
|
+
export interface ResolvedLoggingConfig {
|
|
19
|
+
level: LogLevel;
|
|
20
|
+
format: LogFormat;
|
|
21
|
+
timestamps: boolean;
|
|
22
|
+
output: (message: string) => void;
|
|
23
|
+
errorOutput: (message: string) => void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Resolves logging config with defaults applied.
|
|
27
|
+
*/
|
|
28
|
+
export declare function resolveLoggingConfig(config?: LoggingConfig): ResolvedLoggingConfig;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logger singleton management.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors the MomentumAPI singleton pattern from momentum-api.ts.
|
|
5
|
+
*/
|
|
6
|
+
import type { LoggingConfig } from './logger-config.types';
|
|
7
|
+
import { MomentumLogger } from './logger';
|
|
8
|
+
/**
|
|
9
|
+
* Initialize the global Momentum logger.
|
|
10
|
+
* Should be called once during server startup.
|
|
11
|
+
*
|
|
12
|
+
* @returns The root logger instance
|
|
13
|
+
*/
|
|
14
|
+
export declare function initializeMomentumLogger(config?: LoggingConfig): MomentumLogger;
|
|
15
|
+
/**
|
|
16
|
+
* Get the initialized root logger.
|
|
17
|
+
* Falls back to a default logger if not initialized (for use before init).
|
|
18
|
+
*/
|
|
19
|
+
export declare function getMomentumLogger(): MomentumLogger;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a child logger with a named context.
|
|
22
|
+
* e.g., `createLogger('DB')` → logger with context `Momentum:DB`
|
|
23
|
+
*
|
|
24
|
+
* This is the primary way to get a logger in library code.
|
|
25
|
+
*/
|
|
26
|
+
export declare function createLogger(context: string): MomentumLogger;
|
|
27
|
+
/**
|
|
28
|
+
* Reset the logger singleton. Primarily for testing.
|
|
29
|
+
*/
|
|
30
|
+
export declare function resetMomentumLogger(): void;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MomentumLogger — the core logging class.
|
|
3
|
+
*
|
|
4
|
+
* Supports log levels, child loggers, and enrichers.
|
|
5
|
+
* Uses process.stdout/stderr directly (ESLint no-console compliant).
|
|
6
|
+
*/
|
|
7
|
+
import type { LoggingConfig, ResolvedLoggingConfig } from './logger-config.types';
|
|
8
|
+
/**
|
|
9
|
+
* Interface for enriching log entries with additional context.
|
|
10
|
+
* Used by plugins (e.g., OTel) to inject trace IDs into log output.
|
|
11
|
+
*/
|
|
12
|
+
export interface LogEnricher {
|
|
13
|
+
enrich(): Record<string, unknown>;
|
|
14
|
+
}
|
|
15
|
+
export declare class MomentumLogger {
|
|
16
|
+
private static enrichers;
|
|
17
|
+
readonly context: string;
|
|
18
|
+
private readonly config;
|
|
19
|
+
private readonly formatter;
|
|
20
|
+
constructor(context: string, config?: LoggingConfig | ResolvedLoggingConfig);
|
|
21
|
+
debug(message: string, data?: Record<string, unknown>): void;
|
|
22
|
+
info(message: string, data?: Record<string, unknown>): void;
|
|
23
|
+
warn(message: string, data?: Record<string, unknown>): void;
|
|
24
|
+
error(message: string, data?: Record<string, unknown>): void;
|
|
25
|
+
fatal(message: string, data?: Record<string, unknown>): void;
|
|
26
|
+
/**
|
|
27
|
+
* Creates a child logger with a sub-context.
|
|
28
|
+
* e.g., `Momentum:DB` → `Momentum:DB:Migrate`
|
|
29
|
+
*/
|
|
30
|
+
child(subContext: string): MomentumLogger;
|
|
31
|
+
/**
|
|
32
|
+
* Registers a global enricher that adds extra fields to all log entries.
|
|
33
|
+
*/
|
|
34
|
+
static registerEnricher(enricher: LogEnricher): void;
|
|
35
|
+
/**
|
|
36
|
+
* Removes a previously registered enricher.
|
|
37
|
+
*/
|
|
38
|
+
static removeEnricher(enricher: LogEnricher): void;
|
|
39
|
+
/**
|
|
40
|
+
* Clears all registered enrichers. Primarily for testing.
|
|
41
|
+
*/
|
|
42
|
+
static clearEnrichers(): void;
|
|
43
|
+
private log;
|
|
44
|
+
private collectEnrichments;
|
|
45
|
+
}
|