feat(logger): add @bytelyst/logger shared package
createLogger(config) factory for structured logging across dashboards and services. - Dev: pretty-prints to stderr/stdout - Prod: JSON structured output for log ingestion (Loki, Datadog, etc.) - Exports: createLogger, Logger, LoggerConfig, LogLevel, LogPayload
This commit is contained in:
parent
8930aa5af7
commit
fbcb39d52c
19
packages/logger/package.json
Normal file
19
packages/logger/package.json
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"name": "@bytelyst/logger",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"description": "Structured logger factory for Next.js dashboards and Node.js services",
|
||||||
|
"type": "module",
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"import": "./dist/index.js"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"main": "./dist/index.js",
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"files": ["dist"],
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc",
|
||||||
|
"test": "vitest run"
|
||||||
|
}
|
||||||
|
}
|
||||||
2
packages/logger/src/index.ts
Normal file
2
packages/logger/src/index.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export { createLogger } from "./logger.js";
|
||||||
|
export type { Logger, LoggerConfig, LogLevel, LogPayload } from "./types.js";
|
||||||
83
packages/logger/src/logger.ts
Normal file
83
packages/logger/src/logger.ts
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
import type { LogLevel, LogPayload, LoggerConfig, Logger } from "./types.js";
|
||||||
|
|
||||||
|
function formatPayload(
|
||||||
|
service: string,
|
||||||
|
level: LogLevel,
|
||||||
|
message: string,
|
||||||
|
error?: unknown,
|
||||||
|
extra?: Record<string, unknown>,
|
||||||
|
): LogPayload {
|
||||||
|
const payload: LogPayload = {
|
||||||
|
level,
|
||||||
|
message,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
service,
|
||||||
|
...extra,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (error instanceof Error) {
|
||||||
|
payload.error = error.message;
|
||||||
|
payload.stack = error.stack;
|
||||||
|
} else if (error !== undefined) {
|
||||||
|
payload.error = String(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
function emit(payload: LogPayload, isDev: boolean): void {
|
||||||
|
if (isDev) {
|
||||||
|
const { level, message, error: err, stack: _stack, ...rest } = payload;
|
||||||
|
const prefix = `[${level.toUpperCase()}]`;
|
||||||
|
const extras = Object.keys(rest).length > 2 ? ` ${JSON.stringify(rest)}` : "";
|
||||||
|
const errStr = err ? ` — ${err}` : "";
|
||||||
|
|
||||||
|
if (level === "error" || level === "warn") {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.error(`${prefix} ${message}${errStr}${extras}`);
|
||||||
|
} else {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log(`${prefix} ${message}${extras}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const out = JSON.stringify(payload);
|
||||||
|
if (payload.level === "error" || payload.level === "warn") {
|
||||||
|
process.stderr.write(out + "\n");
|
||||||
|
} else {
|
||||||
|
process.stdout.write(out + "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a structured logger for a service or dashboard.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* import { createLogger } from "@bytelyst/logger";
|
||||||
|
* const log = createLogger({ service: "admin-dashboard" });
|
||||||
|
* log.error("Login failed", error, { userId });
|
||||||
|
* log.info("Seed complete", { containers: 8 });
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
export function createLogger(config: LoggerConfig): Logger {
|
||||||
|
const { service } = config;
|
||||||
|
const isDev = config.isDev ?? process.env.NODE_ENV !== "production";
|
||||||
|
|
||||||
|
return {
|
||||||
|
error(message: string, error?: unknown, extra?: Record<string, unknown>): void {
|
||||||
|
emit(formatPayload(service, "error", message, error, extra), isDev);
|
||||||
|
},
|
||||||
|
warn(message: string, extra?: Record<string, unknown>): void {
|
||||||
|
emit(formatPayload(service, "warn", message, undefined, extra), isDev);
|
||||||
|
},
|
||||||
|
info(message: string, extra?: Record<string, unknown>): void {
|
||||||
|
emit(formatPayload(service, "info", message, undefined, extra), isDev);
|
||||||
|
},
|
||||||
|
debug(message: string, extra?: Record<string, unknown>): void {
|
||||||
|
if (isDev) {
|
||||||
|
emit(formatPayload(service, "debug", message, undefined, extra), isDev);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
25
packages/logger/src/types.ts
Normal file
25
packages/logger/src/types.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
export type LogLevel = "info" | "warn" | "error" | "debug";
|
||||||
|
|
||||||
|
export interface LogPayload {
|
||||||
|
level: LogLevel;
|
||||||
|
message: string;
|
||||||
|
timestamp: string;
|
||||||
|
service: string;
|
||||||
|
error?: string;
|
||||||
|
stack?: string;
|
||||||
|
[key: string]: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LoggerConfig {
|
||||||
|
/** Service name included in every log entry (e.g. "admin-dashboard", "billing-service") */
|
||||||
|
service: string;
|
||||||
|
/** Override NODE_ENV detection for dev/prod mode. Default: reads process.env.NODE_ENV */
|
||||||
|
isDev?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Logger {
|
||||||
|
error(message: string, error?: unknown, extra?: Record<string, unknown>): void;
|
||||||
|
warn(message: string, extra?: Record<string, unknown>): void;
|
||||||
|
info(message: string, extra?: Record<string, unknown>): void;
|
||||||
|
debug(message: string, extra?: Record<string, unknown>): void;
|
||||||
|
}
|
||||||
8
packages/logger/tsconfig.json
Normal file
8
packages/logger/tsconfig.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"extends": "../../tsconfig.base.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": "dist",
|
||||||
|
"rootDir": "src"
|
||||||
|
},
|
||||||
|
"include": ["src"]
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user