@unified-llm/core
Version:
Unified LLM interface (in-memory).
45 lines • 1.4 kB
JavaScript
export function toModelSafeError(err) {
if (err instanceof Error) {
return { name: err.name, message: err.message };
}
return { name: "Error", message: String(err) };
}
export const NOOP_LOGGER = {
debug: () => { },
info: () => { },
warn: () => { },
error: () => { },
child: () => NOOP_LOGGER,
};
/**
* Runs an async task while measuring duration and logging success or failure.
* Logs include duration_ms and, on failure, a normalized error object.
*/
export async function logTimed(logger, clock, event, meta, fn, level = "info") {
const start = clock.nowMs();
try {
const result = await fn();
const end = clock.nowMs();
const durationMs = Math.max(0, end - start);
logger[level](event, {
...meta,
ok: true,
duration_ms: durationMs,
...(clock.nowEpochMs ? { timestamp_epoch_ms: clock.nowEpochMs() } : {}),
});
return result;
}
catch (err) {
const end = clock.nowMs();
const durationMs = Math.max(0, end - start);
logger.error(event, {
...meta,
ok: false,
duration_ms: durationMs,
error: toModelSafeError(err),
...(clock.nowEpochMs ? { timestamp_epoch_ms: clock.nowEpochMs() } : {}),
});
throw err;
}
}
//# sourceMappingURL=logging.js.map