@vllnt/logger 0.1.2 → 0.1.3-canary.1b2dbd3
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 +89 -0
- package/README.md +144 -92
- package/dist/{chunk-5GDAMA7T.js → chunk-OROSOSC5.js} +69 -28
- package/dist/convex.d.ts +2 -2
- package/dist/convex.js +1 -1
- package/dist/{core-BKAGZb-m.d.ts → core-CzlOE0nJ.d.ts} +13 -6
- package/dist/index.d.ts +8 -3
- package/dist/index.js +1 -1
- package/dist/posthog.d.ts +6 -4
- package/dist/posthog.js +8 -1
- package/dist/testing.d.ts +3 -2
- package/docs/API.md +242 -0
- package/llms.txt +51 -0
- package/package.json +75 -28
- package/src/backend.ts +40 -0
- package/src/convex.ts +28 -0
- package/src/core.ts +309 -0
- package/src/index.ts +22 -0
- package/src/posthog.ts +43 -0
- package/src/testing.ts +22 -0
package/src/core.ts
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vllnt/logger — Core module.
|
|
3
|
+
*
|
|
4
|
+
* Pure functions, ZERO module-level side effects.
|
|
5
|
+
* Safe to import in any runtime (Node, Convex, browser, edge).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** The four log levels. Severity order: debug < info < warn < error. */
|
|
9
|
+
export type LogLevel = "debug" | "error" | "info" | "warn";
|
|
10
|
+
|
|
11
|
+
/** Structured log data payload */
|
|
12
|
+
export type LogData = Record<string, unknown>;
|
|
13
|
+
|
|
14
|
+
/** Formatted log entry */
|
|
15
|
+
export type LogEntry = {
|
|
16
|
+
event: string;
|
|
17
|
+
level: LogLevel;
|
|
18
|
+
timestamp: string;
|
|
19
|
+
} & Record<string, unknown>;
|
|
20
|
+
|
|
21
|
+
/** Logger interface — 4 level methods */
|
|
22
|
+
export type Logger = {
|
|
23
|
+
debug: (event: string, data?: LogData) => void;
|
|
24
|
+
error: (event: string, data?: LogData) => void;
|
|
25
|
+
info: (event: string, data?: LogData) => void;
|
|
26
|
+
warn: (event: string, data?: LogData) => void;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** Extended logger with timing helpers */
|
|
30
|
+
export type ExtendedLogger = Logger & {
|
|
31
|
+
withTiming: <T>(
|
|
32
|
+
label: string,
|
|
33
|
+
execute: () => Promise<T>,
|
|
34
|
+
extraData?: LogData,
|
|
35
|
+
) => Promise<T>;
|
|
36
|
+
withTimingSync: <T>(
|
|
37
|
+
label: string,
|
|
38
|
+
execute: () => T,
|
|
39
|
+
extraData?: LogData,
|
|
40
|
+
) => T;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** Output handler for log entries. Synchronous by design — async adapters must handle their own promises. */
|
|
44
|
+
export type LogOutput = (entry: LogEntry) => void;
|
|
45
|
+
|
|
46
|
+
/** Configuration for creating a logger */
|
|
47
|
+
export type LoggerConfig = {
|
|
48
|
+
/** Returns the current log level. Called on every log call, including calls its level filters out — keep it cheap. */
|
|
49
|
+
getLogLevel: () => LogLevel;
|
|
50
|
+
output: LogOutput;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const LOG_LEVEL_PRIORITY: Record<LogLevel, number> = {
|
|
54
|
+
debug: 0,
|
|
55
|
+
error: 3,
|
|
56
|
+
info: 1,
|
|
57
|
+
warn: 2,
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
function shouldLog(level: LogLevel, configuredLevel: LogLevel): boolean {
|
|
61
|
+
return LOG_LEVEL_PRIORITY[level] >= LOG_LEVEL_PRIORITY[configuredLevel];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Parse a log level string, returning default if invalid. */
|
|
65
|
+
export function parseLogLevel(
|
|
66
|
+
value: string | undefined,
|
|
67
|
+
defaultLevel: LogLevel = "info",
|
|
68
|
+
): LogLevel {
|
|
69
|
+
const normalized = value?.toLowerCase().trim();
|
|
70
|
+
if (
|
|
71
|
+
normalized === "debug" ||
|
|
72
|
+
normalized === "info" ||
|
|
73
|
+
normalized === "warn" ||
|
|
74
|
+
normalized === "error"
|
|
75
|
+
) {
|
|
76
|
+
return normalized;
|
|
77
|
+
}
|
|
78
|
+
return defaultLevel;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Format a log entry. User data spread FIRST, fixed fields LAST.
|
|
83
|
+
* This prevents user data from overwriting event/level/timestamp.
|
|
84
|
+
*/
|
|
85
|
+
export function formatLogEntry(
|
|
86
|
+
level: LogLevel,
|
|
87
|
+
event: string,
|
|
88
|
+
data?: LogData,
|
|
89
|
+
): LogEntry {
|
|
90
|
+
return {
|
|
91
|
+
...data,
|
|
92
|
+
event,
|
|
93
|
+
level,
|
|
94
|
+
timestamp: new Date().toISOString(),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Resolve the configured level; a throwing or invalid `getLogLevel` falls back to "info". */
|
|
99
|
+
function resolveLevel(config: LoggerConfig): LogLevel {
|
|
100
|
+
try {
|
|
101
|
+
return parseLogLevel(config.getLogLevel(), "info");
|
|
102
|
+
} catch {
|
|
103
|
+
return "info";
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Create a scoped logger with the given configuration.
|
|
109
|
+
*
|
|
110
|
+
* Log calls never throw: a failing `getLogLevel`, `output`, or data getter is
|
|
111
|
+
* swallowed so logging cannot break the host application.
|
|
112
|
+
*/
|
|
113
|
+
export function createLogger(scope: string, config: LoggerConfig): Logger {
|
|
114
|
+
const log = (level: LogLevel, event: string, data?: LogData): void => {
|
|
115
|
+
try {
|
|
116
|
+
if (shouldLog(level, resolveLevel(config))) {
|
|
117
|
+
config.output(formatLogEntry(level, `${scope}.${event}`, data));
|
|
118
|
+
}
|
|
119
|
+
} catch {
|
|
120
|
+
// Swallowed — logging failures must not propagate
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
debug: (event, data) => {
|
|
126
|
+
log("debug", event, data);
|
|
127
|
+
},
|
|
128
|
+
error: (event, data) => {
|
|
129
|
+
log("error", event, data);
|
|
130
|
+
},
|
|
131
|
+
info: (event, data) => {
|
|
132
|
+
log("info", event, data);
|
|
133
|
+
},
|
|
134
|
+
warn: (event, data) => {
|
|
135
|
+
log("warn", event, data);
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function formatError(error: unknown): string {
|
|
141
|
+
try {
|
|
142
|
+
return error instanceof Error ? error.message : String(error);
|
|
143
|
+
} catch {
|
|
144
|
+
return "[unprintable error]";
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Build the data and log it. If building throws (for example a throwing
|
|
150
|
+
* getter in `extraData`), drop the entry, as `createLogger` does.
|
|
151
|
+
*/
|
|
152
|
+
function logBuilt(
|
|
153
|
+
log: Logger["info"],
|
|
154
|
+
event: string,
|
|
155
|
+
build: () => LogData,
|
|
156
|
+
): void {
|
|
157
|
+
try {
|
|
158
|
+
log(event, build());
|
|
159
|
+
} catch {
|
|
160
|
+
// Dropped — building the data must not break the timed call
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function createWithTiming(base: Logger): ExtendedLogger["withTiming"] {
|
|
165
|
+
return async <T>(
|
|
166
|
+
label: string,
|
|
167
|
+
execute: () => Promise<T>,
|
|
168
|
+
extraData?: LogData,
|
|
169
|
+
): Promise<T> => {
|
|
170
|
+
const start = Date.now();
|
|
171
|
+
base.info(`${label}.start`, extraData);
|
|
172
|
+
try {
|
|
173
|
+
const result = await execute();
|
|
174
|
+
logBuilt(base.info, `${label}.complete`, () => ({
|
|
175
|
+
...extraData,
|
|
176
|
+
durationMs: Date.now() - start,
|
|
177
|
+
}));
|
|
178
|
+
return result;
|
|
179
|
+
} catch (error) {
|
|
180
|
+
logBuilt(base.error, `${label}.error`, () => ({
|
|
181
|
+
...extraData,
|
|
182
|
+
durationMs: Date.now() - start,
|
|
183
|
+
error: formatError(error),
|
|
184
|
+
}));
|
|
185
|
+
throw error;
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function createWithTimingSync(base: Logger): ExtendedLogger["withTimingSync"] {
|
|
191
|
+
return <T>(label: string, execute: () => T, extraData?: LogData): T => {
|
|
192
|
+
const start = Date.now();
|
|
193
|
+
base.info(`${label}.start`, extraData);
|
|
194
|
+
try {
|
|
195
|
+
const result = execute();
|
|
196
|
+
logBuilt(base.info, `${label}.complete`, () => ({
|
|
197
|
+
...extraData,
|
|
198
|
+
durationMs: Date.now() - start,
|
|
199
|
+
}));
|
|
200
|
+
return result;
|
|
201
|
+
} catch (error) {
|
|
202
|
+
logBuilt(base.error, `${label}.error`, () => ({
|
|
203
|
+
...extraData,
|
|
204
|
+
durationMs: Date.now() - start,
|
|
205
|
+
error: formatError(error),
|
|
206
|
+
}));
|
|
207
|
+
throw error;
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Create an extended logger with timing helpers.
|
|
214
|
+
*
|
|
215
|
+
* @remarks Caught errors use `error.message` verbatim.
|
|
216
|
+
* Callers handling sensitive errors should sanitize before throwing.
|
|
217
|
+
*/
|
|
218
|
+
export function createExtendedLogger(
|
|
219
|
+
scope: string,
|
|
220
|
+
config: LoggerConfig,
|
|
221
|
+
): ExtendedLogger {
|
|
222
|
+
const base = createLogger(scope, config);
|
|
223
|
+
return {
|
|
224
|
+
...base,
|
|
225
|
+
withTiming: createWithTiming(base),
|
|
226
|
+
withTimingSync: createWithTimingSync(base),
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* JSON.stringify replacer that keeps entries serializable. It turns BigInt
|
|
232
|
+
* into a string, Error into `{ name, message, stack }`, and circular
|
|
233
|
+
* references into "[Circular]". Repeated non-circular references stay intact.
|
|
234
|
+
*/
|
|
235
|
+
function createReplacer(): (
|
|
236
|
+
this: unknown,
|
|
237
|
+
key: string,
|
|
238
|
+
value: unknown,
|
|
239
|
+
) => unknown {
|
|
240
|
+
const ancestors: unknown[] = [];
|
|
241
|
+
return function replacer(this: unknown, _key: string, value: unknown) {
|
|
242
|
+
if (typeof value === "bigint") {
|
|
243
|
+
return value.toString();
|
|
244
|
+
}
|
|
245
|
+
const resolved =
|
|
246
|
+
value instanceof Error
|
|
247
|
+
? { message: value.message, name: value.name, stack: value.stack }
|
|
248
|
+
: value;
|
|
249
|
+
if (typeof resolved !== "object" || resolved === null) {
|
|
250
|
+
return resolved;
|
|
251
|
+
}
|
|
252
|
+
// Drop everything below the current holder: the rest is a finished sibling branch.
|
|
253
|
+
ancestors.splice(ancestors.indexOf(this) + 1);
|
|
254
|
+
if (ancestors.includes(resolved)) {
|
|
255
|
+
return "[Circular]";
|
|
256
|
+
}
|
|
257
|
+
ancestors.push(resolved);
|
|
258
|
+
return resolved;
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Default console output handler. Never throws. It handles BigInt, Error and
|
|
264
|
+
* circular values; anything still unserializable (throwing toJSON or getters)
|
|
265
|
+
* degrades to a minimal event/level line, and console failures disappear.
|
|
266
|
+
*/
|
|
267
|
+
export function consoleOutput(entry: LogEntry): void {
|
|
268
|
+
let message: string;
|
|
269
|
+
try {
|
|
270
|
+
message = JSON.stringify(entry, createReplacer());
|
|
271
|
+
} catch {
|
|
272
|
+
message = `[logger:serialize-error] event=${entry.event} level=${entry.level}`;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
try {
|
|
276
|
+
switch (entry.level) {
|
|
277
|
+
case "debug":
|
|
278
|
+
console.debug(message);
|
|
279
|
+
break;
|
|
280
|
+
case "error":
|
|
281
|
+
console.error(message);
|
|
282
|
+
break;
|
|
283
|
+
case "info":
|
|
284
|
+
console.info(message);
|
|
285
|
+
break;
|
|
286
|
+
case "warn":
|
|
287
|
+
console.warn(message);
|
|
288
|
+
break;
|
|
289
|
+
default:
|
|
290
|
+
console.info(message);
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
} catch {
|
|
294
|
+
// Swallowed — a broken console must not break the app
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** Compose outputs into a single LogOutput. Each output runs in isolation. */
|
|
299
|
+
export function composeOutputs(...outputs: LogOutput[]): LogOutput {
|
|
300
|
+
return (entry: LogEntry): void => {
|
|
301
|
+
outputs.forEach((output) => {
|
|
302
|
+
try {
|
|
303
|
+
output(entry);
|
|
304
|
+
} catch {
|
|
305
|
+
// Isolated — other outputs continue
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
};
|
|
309
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vllnt/logger — zero-dependency structured JSON logger for TypeScript.
|
|
3
|
+
*
|
|
4
|
+
* Core exports + Node.js preset (`createBackendLogger`).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export { createBackendLogger } from "./backend.js";
|
|
8
|
+
export {
|
|
9
|
+
composeOutputs,
|
|
10
|
+
consoleOutput,
|
|
11
|
+
createExtendedLogger,
|
|
12
|
+
createLogger,
|
|
13
|
+
type ExtendedLogger,
|
|
14
|
+
formatLogEntry,
|
|
15
|
+
type LogData,
|
|
16
|
+
type LogEntry,
|
|
17
|
+
type Logger,
|
|
18
|
+
type LoggerConfig,
|
|
19
|
+
type LogLevel,
|
|
20
|
+
type LogOutput,
|
|
21
|
+
parseLogLevel,
|
|
22
|
+
} from "./core.js";
|
package/src/posthog.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vllnt/logger — PostHog output adapter.
|
|
3
|
+
*
|
|
4
|
+
* Wraps a PostHog-style capture function as a LogOutput.
|
|
5
|
+
* No PostHog SDK dependency — consumer passes their own capture fn.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { LogEntry, LogOutput } from "./core.js";
|
|
9
|
+
|
|
10
|
+
/** PostHog-compatible capture function signature */
|
|
11
|
+
export type PostHogCapture = (
|
|
12
|
+
event: string,
|
|
13
|
+
properties?: Record<string, unknown>,
|
|
14
|
+
) => unknown;
|
|
15
|
+
|
|
16
|
+
function isThenable(value: unknown): value is PromiseLike<unknown> {
|
|
17
|
+
return (
|
|
18
|
+
typeof value === "object" &&
|
|
19
|
+
value !== null &&
|
|
20
|
+
typeof (value as { then?: unknown }).then === "function"
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Create a LogOutput that forwards entries to PostHog.
|
|
26
|
+
* Swallows errors from the capture function, including rejected promises it
|
|
27
|
+
* returns — logging must not break the app.
|
|
28
|
+
*/
|
|
29
|
+
export function createPostHogOutput(capture: PostHogCapture): LogOutput {
|
|
30
|
+
return (entry: LogEntry): void => {
|
|
31
|
+
try {
|
|
32
|
+
const { event, ...properties } = entry;
|
|
33
|
+
const result = capture(event, properties);
|
|
34
|
+
if (isThenable(result)) {
|
|
35
|
+
Promise.resolve(result).catch(() => {
|
|
36
|
+
// Swallowed — async capture failures must not become unhandled rejections
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
} catch {
|
|
40
|
+
// Swallowed — logging failures must not propagate
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
package/src/testing.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vllnt/logger — Test utilities.
|
|
3
|
+
*
|
|
4
|
+
* Provides a test output that captures log entries for assertion.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { LogEntry, LogOutput } from "./core.js";
|
|
8
|
+
|
|
9
|
+
/** Result of createTestOutput — output function + captured entries */
|
|
10
|
+
export type TestOutput = {
|
|
11
|
+
entries: readonly LogEntry[];
|
|
12
|
+
output: LogOutput;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/** Create a test output that captures entries into an array for assertion. */
|
|
16
|
+
export function createTestOutput(): TestOutput {
|
|
17
|
+
const entries: LogEntry[] = [];
|
|
18
|
+
const output: LogOutput = (entry: LogEntry): void => {
|
|
19
|
+
entries.push(entry);
|
|
20
|
+
};
|
|
21
|
+
return { entries, output };
|
|
22
|
+
}
|