UNPKG

@vitaly-yosef/node-smart-logger

Version:

Universal logger for Node.js applications with support for both ESM and CommonJS. It provides advanced features, such as structured logging in JSON format, integration with AWS CloudWatch Logs, and contextual logging.

88 lines (87 loc) 3.5 kB
import * as winston from 'winston'; import 'winston-cloudwatch'; import { Request, Response, NextFunction } from 'express'; import { shutdownLogger } from './graceful-shutdown'; interface LoggerContext { traceId?: string; requestId?: string; operationId?: string; deviceId?: string; userId?: string; [key: string]: any; } interface ExtendedLogger extends winston.Logger { setContext: (context: LoggerContext) => void; getContext: () => LoggerContext; clearContext: () => void; generateTraceId: () => string; withOperationContext: (contextData?: LoggerContext, callback?: () => void) => any; shutdown: () => Promise<void>; } interface LoggerOptions { [key: string]: any; } interface HttpLoggerOptions { format?: string; logOnlyAuthErrors?: boolean; skipLogging?: boolean; } /** * Generates a unique identifier for tracking requests * @returns {string} Unique identifier */ declare function generateTraceId(): string; /** * Sets the context for the current request or operation. * @param {LoggerContext} context - Object with context data. */ declare function setContext(context: LoggerContext): void; /** * Gets the current logging context. * @returns {LoggerContext} Current logging context */ declare function getContext(): LoggerContext; /** * Clears the context for the current request */ declare function clearContext(): void; /** * @param {string} service * @param {string|null} customLogDir * @param {LoggerOptions} options - Additional options for the logger * @returns {ExtendedLogger} */ declare function createLoggerFunction(service: string, customLogDir?: string | null, options?: LoggerOptions): ExtendedLogger; /** * Creates middleware for logging HTTP requests. * Using Morgan for logging HTTP-requests, sends output to the provided Winston-logger. * @param {ExtendedLogger} loggerInstance - Instance of Winston logger, built by createLogger. * @param {HttpLoggerOptions} [options={}] - Options. * @returns {Function} - Morgan middleware. */ declare function createHttpLoggerMiddleware(loggerInstance: ExtendedLogger, options?: HttpLoggerOptions): (req: Request, res: Response, next: NextFunction) => void; /** * Creates middleware for error handling and logging * @param {ExtendedLogger} loggerInstance - Instance of Winston logger * @returns {Function} - Error handling middleware */ declare function createErrorLoggerMiddleware(loggerInstance: ExtendedLogger): (err: Error, req: Request, res: Response, next: NextFunction) => void; declare const loggerLibrary: { createLogger: typeof createLoggerFunction; createHttpLoggerMiddleware: typeof createHttpLoggerMiddleware; createErrorLoggerMiddleware: typeof createErrorLoggerMiddleware; setContext: typeof setContext; getContext: typeof getContext; clearContext: typeof clearContext; generateTraceId: typeof generateTraceId; shutdownLogger: typeof shutdownLogger; }; export declare const createLogger: typeof createLoggerFunction; export declare const createHttpLogger: typeof createHttpLoggerMiddleware; export declare const createErrorLogger: typeof createErrorLoggerMiddleware; export declare const getLoggerContext: typeof getContext; export declare const setLoggerContext: typeof setContext; export declare const clearLoggerContext: typeof clearContext; export declare const generateLoggerTraceId: typeof generateTraceId; export declare const shutdown: typeof shutdownLogger; export default loggerLibrary;