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:
saravanakumardb1 2026-02-12 15:35:27 -08:00
parent 8930aa5af7
commit fbcb39d52c
5 changed files with 137 additions and 0 deletions

View 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"
}
}

View File

@ -0,0 +1,2 @@
export { createLogger } from "./logger.js";
export type { Logger, LoggerConfig, LogLevel, LogPayload } from "./types.js";

View 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);
}
},
};
}

View 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;
}

View File

@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}