A comprehensive, multi-transport logging library for Node.js applications providing flexible and extensible logging capabilities.
—
Core functionality for creating and configuring logger instances with custom transports, formats, and levels.
Creates a new Logger instance with custom configuration options.
/**
* Creates a new Logger instance with custom configuration
* @param options - Configuration options for the logger
* @returns A new Logger instance
*/
function createLogger(options?: LoggerOptions): Logger;
interface LoggerOptions {
/** Custom logging levels configuration */
levels?: object;
/** Suppress all logging output */
silent?: boolean;
/** Log message formatting configuration */
format?: Format;
/** Minimum logging level */
level?: string;
/** Controls process exit behavior on error */
exitOnError?: Function | boolean;
/** Default metadata to include with all log messages */
defaultMeta?: any;
/** Transport or array of transports for log output */
transports?: Transport[] | Transport;
/** Automatically handle uncaught exceptions */
handleExceptions?: boolean;
/** Automatically handle unhandled promise rejections */
handleRejections?: boolean;
/** Transports specifically for exception handling */
exceptionHandlers?: any;
/** Transports specifically for rejection handling */
rejectionHandlers?: any;
}Usage Examples:
const winston = require('winston');
// Basic logger
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.Console()
]
});
// Advanced logger with custom configuration
const advancedLogger = winston.createLogger({
level: 'debug',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.json()
),
defaultMeta: { service: 'api' },
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' }),
new winston.transports.Console({
format: winston.format.simple()
})
],
handleExceptions: true,
handleRejections: true,
exitOnError: false
});Core Logger class providing logging functionality and transport management.
/**
* Core Logger class extending Node.js Transform stream
*/
class Logger extends Transform {
constructor(options?: LoggerOptions);
/** Current logging level */
level: string;
/** Silent mode flag */
silent: boolean;
/** Log formatting configuration */
format: Format;
/** Logging levels configuration */
levels: object;
/** Array of configured transports */
transports: Transport[];
/** Exception handler instance */
exceptions: ExceptionHandler;
/** Rejection handler instance */
rejections: RejectionHandler;
/** Profiler instances map */
profilers: object;
/** Exit on error behavior */
exitOnError: Function | boolean;
/** Default metadata for all logs */
defaultMeta?: any;
}Primary methods for logging messages at different levels.
/**
* Core logging method
* @param level - Log level string
* @param message - Log message
* @param meta - Additional metadata
* @returns Logger instance for chaining
*/
log(level: string, message: string, ...meta: any[]): Logger;
/**
* Log at error level
* @param message - Error message
* @param meta - Additional metadata
* @returns Logger instance for chaining
*/
error(message: string, ...meta: any[]): Logger;
/**
* Log at warn level
* @param message - Warning message
* @param meta - Additional metadata
* @returns Logger instance for chaining
*/
warn(message: string, ...meta: any[]): Logger;
/**
* Log at info level
* @param message - Info message
* @param meta - Additional metadata
* @returns Logger instance for chaining
*/
info(message: string, ...meta: any[]): Logger;
/**
* Log at http level
* @param message - HTTP-related message
* @param meta - Additional metadata
* @returns Logger instance for chaining
*/
http(message: string, ...meta: any[]): Logger;
/**
* Log at verbose level
* @param message - Verbose message
* @param meta - Additional metadata
* @returns Logger instance for chaining
*/
verbose(message: string, ...meta: any[]): Logger;
/**
* Log at debug level
* @param message - Debug message
* @param meta - Additional metadata
* @returns Logger instance for chaining
*/
debug(message: string, ...meta: any[]): Logger;
/**
* Log at silly level
* @param message - Debug message
* @param meta - Additional metadata
* @returns Logger instance for chaining
*/
silly(message: string, ...meta: any[]): Logger;Methods for adding, removing, and managing log transports.
/**
* Add a transport to this logger
* @param transport - Transport instance to add
* @returns Logger instance for chaining
*/
add(transport: Transport): Logger;
/**
* Remove a transport from this logger
* @param transport - Transport instance to remove
* @returns Logger instance for chaining
*/
remove(transport: Transport): Logger;
/**
* Remove all transports from this logger
* @returns Logger instance for chaining
*/
clear(): Logger;
/**
* Close all transports and clean up resources
* @returns Logger instance for chaining
*/
close(): Logger;Methods for reconfiguring loggers and creating child logger instances.
/**
* Reconfigure this logger with new options
* @param options - New configuration options
*/
configure(options: LoggerOptions): void;
/**
* Create a child logger with additional default metadata
* @param defaultMeta - Additional metadata to include in all logs
* @returns Child logger instance
*/
child(defaultMeta: object): Logger;Methods to check if specific log levels are enabled.
/**
* Check if a specific log level is enabled
* @param level - Log level to check
* @returns True if level is enabled
*/
isLevelEnabled(level: string): boolean;
/**
* Check if error level is enabled
* @returns True if error level is enabled
*/
isErrorEnabled(): boolean;
/**
* Check if warn level is enabled
* @returns True if warn level is enabled
*/
isWarnEnabled(): boolean;
/**
* Check if info level is enabled
* @returns True if info level is enabled
*/
isInfoEnabled(): boolean;
/**
* Check if verbose level is enabled
* @returns True if verbose level is enabled
*/
isVerboseEnabled(): boolean;
/**
* Check if debug level is enabled
* @returns True if debug level is enabled
*/
isDebugEnabled(): boolean;
/**
* Check if silly level is enabled
* @returns True if silly level is enabled
*/
isSillyEnabled(): boolean;Methods for querying historical logs and creating live streams of log data.
/**
* Query historical log data from transports that support querying
* @param options - Query configuration options
* @param callback - Optional callback function for results
* @returns Query results or promise
*/
query(options?: QueryOptions, callback?: (err: Error, results: any) => void): any;
/**
* Create a readable stream of log messages
* @param options - Stream configuration options
* @returns Readable stream of log messages
*/
stream(options?: StreamOptions): ReadableStream;
interface QueryOptions {
/** Number of rows to return */
rows?: number;
/** Limit number of results */
limit?: number;
/** Starting index */
start?: number;
/** Start date filter */
from?: Date;
/** End date filter */
until?: Date;
/** Sort order ('asc' or 'desc') */
order?: 'asc' | 'desc';
/** Fields to include in results */
fields?: any;
}
interface StreamOptions {
/** Starting point for stream (-1 for tail) */
start?: number;
/** Additional stream options */
[key: string]: any;
}Methods for configuring automatic exception and rejection handling at the logger level.
/**
* Configure logger to handle uncaught exceptions
* @param transports - Transport instances for exception logging
*/
handleExceptions(...transports: Transport[]): void;
/**
* Stop handling uncaught exceptions
* @param transports - Transport instances to remove from exception handling
*/
unhandleExceptions(...transports: Transport[]): void;
/**
* Configure logger to handle unhandled promise rejections
* @param transports - Transport instances for rejection logging
*/
handleRejections(...transports: Transport[]): void;
/**
* Stop handling unhandled promise rejections
* @param transports - Transport instances to remove from rejection handling
*/
unhandleRejections(...transports: Transport[]): void;Usage Examples:
const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
transports: [new winston.transports.Console()]
});
// Basic logging
logger.error('Something went wrong');
logger.warn('This is a warning');
logger.info('Information message');
// Check if levels are enabled
if (logger.isDebugEnabled()) {
logger.debug('Debug information');
}
// Add transport
logger.add(new winston.transports.File({ filename: 'app.log' }));
// Create child logger
const childLogger = logger.child({ requestId: '12345' });
childLogger.info('This will include requestId in metadata');
// Reconfigure logger
logger.configure({
level: 'debug',
format: winston.format.json()
});Install with Tessl CLI
npx tessl i tessl/npm-winston