logger->ts

This commit is contained in:
Alex Holliday committed 2026-01-20 22:56:14 +00:00
1 parent c6ac48b4ff
commit cc9850c975
2 files changed
+57 -56

No files matched your search

@@ -1,6 +1,7 @@
import { logger } from "../../utils/logger.js"; import type { NextFunction, Request, Response } from "express";
import { logger } from "@/utils/logger.js";
const handleErrors = (error, req, res, next) => { const handleErrors = (error: any, req: Request, res: Response, next: NextFunction) => {
const status = error.status || 500; const status = error.status || 500;
const message = error.message || "Server error"; const message = error.message || "Server error";
const service = error.service || "unknownService"; const service = error.service || "unknownService";
@@ -10,7 +11,7 @@ const handleErrors = (error, req, res, next) => {
method: error.method, method: error.method,
stack: error.stack, stack: error.stack,
}); });
res.error({ res.status(status).json({
status, status,
msg: message, msg: message,
}); });
@@ -1,46 +1,76 @@
import { createLogger, format, transports } from "winston"; import { createLogger, format, transports, Logger as WinstonLogger } from "winston";
import type { Logform } from "winston";
import dotenv from "dotenv"; import dotenv from "dotenv";
dotenv.config(); dotenv.config();
const SERVICE_NAME = "Logger"; const SERVICE_NAME = "Logger";
interface LogConfig {
message: string;
service?: string;
method?: string;
details?: Record<string, unknown>;
stack?: string;
}
interface LogEntry extends LogConfig {
level: string;
timestamp: string;
}
interface EnvSettings {
logLevel?: string;
}
class Logger { class Logger {
static SERVICE_NAME = SERVICE_NAME; static SERVICE_NAME = SERVICE_NAME;
constructor({ envSettings }) {
private logger: WinstonLogger;
private envSettings: EnvSettings;
private logCache: LogEntry[];
private maxCacheSize: number;
constructor({ envSettings }: { envSettings: EnvSettings }) {
this.envSettings = envSettings; this.envSettings = envSettings;
this.logCache = []; this.logCache = [];
this.maxCacheSize = 1000; this.maxCacheSize = 1000;
const consoleFormat = format.printf(({ level, message, service, method, details, timestamp, stack }) => { const consoleFormat = format.printf((info: Logform.TransformableInfo) => {
if (message instanceof Object) { const { level, service, method, details, timestamp, stack } = info;
message = JSON.stringify(message, null, 2); let message = info.message as string;
let formattedMessage: string = message;
let formattedDetails: string | undefined;
if (typeof message === "object" && message !== null) {
formattedMessage = JSON.stringify(message, null, 2);
} }
if (details instanceof Object) { if (typeof details === "object" && details !== null) {
details = JSON.stringify(details, null, 2); formattedDetails = JSON.stringify(details, null, 2);
} }
let msg = `${timestamp} ${level}:`; let msg = `${timestamp} ${level}:`;
service && (msg += ` [${service}]`); service && (msg += ` [${service}]`);
method && (msg += `(${method})`); method && (msg += `(${method})`);
message && (msg += ` ${message}`); formattedMessage && (msg += ` ${formattedMessage}`);
details && (msg += ` (details: ${details})`); formattedDetails && (msg += ` (details: ${formattedDetails})`);
if (typeof stack !== "undefined") { if (typeof stack === "string") {
const stackTrace = stack const stackTrace = stack
?.split("\n") .split("\n")
.slice(1) // Remove first line (error message) .slice(1) // Remove first line (error message)
.map((line) => { .map((line: string) => {
const match = line.match(/at\s+(.+?)\s+\((.+?):(\d+):(\d+)\)/); const match = line.match(/at\s+(.+?)\s+\((.+?):(\d+):(\d+)\)/);
if (match) { if (match) {
return { return {
function: match[1], function: match[1],
file: match[2], file: match[2],
line: parseInt(match[3]), line: parseInt(match[3] ?? "0", 10),
column: parseInt(match[4]), column: parseInt(match[4] ?? "0", 10),
}; };
} }
return line.trim(); return line.trim();
}); });
stack && (msg += ` (stack: ${JSON.stringify(stackTrace, null, 2)})`); msg += ` (stack: ${JSON.stringify(stackTrace, null, 2)})`;
} }
return msg; return msg;
@@ -67,62 +97,31 @@ class Logger {
return Logger.SERVICE_NAME; return Logger.SERVICE_NAME;
} }
/** info(config: LogConfig) {
* Logs an informational message.
* @param {Object} config - The configuration object.
* @param {string} config.message - The message to log.
* @param {string} config.service - The service name.
* @param {string} config.method - The method name.
* @param {Object} config.details - Additional details.
*/
info(config) {
const logEntry = this.buildLogEntry("info", config); const logEntry = this.buildLogEntry("info", config);
this.cacheLog(logEntry); this.cacheLog(logEntry);
this.logger.info(logEntry); this.logger.info(logEntry);
} }
/** warn(config: LogConfig) {
* Logs a warning message.
* @param {Object} config - The configuration object.
* @param {string} config.message - The message to log.
* @param {string} config.service - The service name.
* @param {string} config.method - The method name.
* @param {Object} config.details - Additional details.
*/
warn(config) {
const logEntry = this.buildLogEntry("warn", config); const logEntry = this.buildLogEntry("warn", config);
this.cacheLog(logEntry); this.cacheLog(logEntry);
this.logger.warn(logEntry); this.logger.warn(logEntry);
} }
/** error(config: LogConfig) {
* Logs an error message.
* @param {Object} config - The configuration object.
* @param {string} config.message - The message to log.
* @param {string} config.service - The service name.
* @param {string} config.method - The method name.
* @param {Object} config.details - Additional details.
*/
error(config) {
const logEntry = this.buildLogEntry("error", config); const logEntry = this.buildLogEntry("error", config);
this.cacheLog(logEntry); this.cacheLog(logEntry);
this.logger.error(logEntry); this.logger.error(logEntry);
} }
/**
* Logs a debug message. debug(config: LogConfig) {
* @param {Object} config - The configuration object.
* @param {string} config.message - The message to log.
* @param {string} config.service - The service name.
* @param {string} config.method - The method name.
* @param {Object} config.details - Additional details.
*/
debug(config) {
const logEntry = this.buildLogEntry("debug", config); const logEntry = this.buildLogEntry("debug", config);
this.cacheLog(logEntry); this.cacheLog(logEntry);
this.logger.debug(logEntry); this.logger.debug(logEntry);
} }
cacheLog(entry) { cacheLog(entry: LogEntry) {
this.logCache.push(entry); this.logCache.push(entry);
if (this.logCache.length > this.maxCacheSize) { if (this.logCache.length > this.maxCacheSize) {
this.logCache.shift(); this.logCache.shift();
@@ -133,7 +132,7 @@ class Logger {
return this.logCache; return this.logCache;
} }
buildLogEntry(level, config) { buildLogEntry(level: string, config: LogConfig): LogEntry {
return { return {
level, level,
message: config.message, message: config.message,
@@ -151,3 +150,4 @@ export default Logger;
// Legacy logger // Legacy logger
const logger = new Logger({ envSettings: { logLevel: "debug" } }); const logger = new Logger({ envSettings: { logLevel: "debug" } });
export { logger }; export { logger };
export type { LogConfig, LogEntry };