UNPKG

@loglayer/transport

Version:

Base transport used to implement logging transports for loglayer.

125 lines (119 loc) 4.09 kB
import { LogLevel, LogLayerTransport, LogLayerTransportParams } from '@loglayer/shared'; export { LogLayerTransport, LogLayerTransportParams, LogLevel } from '@loglayer/shared'; declare const LogLevelPriority: Record<LogLevel, number>; /** * Logging methods that are common to logging libraries */ interface LoggerLibrary { info(...data: any[]): void; warn(...data: any[]): void; error(...data: any[]): void; trace?: (...data: any[]) => void; debug(...data: any[]): void; fatal?: (...data: any[]) => void; } interface LoggerlessTransportConfig { /** * A user-defined identifier for the transport */ id?: string; /** * If false, the transport will not send logs to the logger. * Default is true. */ enabled?: boolean; /** * If true, the transport will log to the console for debugging purposes */ consoleDebug?: boolean; /** * Minimum log level to process. Defaults to "trace". */ level?: "trace" | "debug" | "info" | "warn" | "error" | "fatal"; } interface LogLayerTransportConfig<LogLibrary> extends Omit<LoggerlessTransportConfig, "level"> { /** * The logging library instance to use for logging */ logger: LogLibrary; } /** * For implementing libraries that are logging libraries that generally have an interface of: * info(), warn(), error(), debug(), trace(), etc. * * @see {@link https://loglayer.dev/transports/creating-transports.html | Creating Transports} */ declare abstract class BaseTransport<LogLibrary> implements LogLayerTransport<LogLibrary> { /** * An identifier for the transport. If not defined, a random one will be generated. */ id?: string; /** * Instance of the logger library */ protected logger: LogLibrary; /** * If false, the transport will not send logs to the logger. */ enabled: boolean; /** * If true, the transport will log to the console for debugging purposes */ protected consoleDebug?: boolean; constructor(config: LogLayerTransportConfig<LogLibrary>); /** * LogLayer calls this to send logs to the transport */ _sendToLogger(params: LogLayerTransportParams): void; /** * Returns the logger instance attached to the transport */ getLoggerInstance(): LogLibrary; /** * Sends the log data to the logger for transport */ abstract shipToLogger(params: LogLayerTransportParams): any[]; } /** * Shows what the logging library output looks like under a transport * @param label A name for the test * @param logLayerInstance The loglayer instance to use for logging */ declare function testTransportOutput(label: string, logLayerInstance: any): void; /** * For implementing libraries that aren't logging libraries * * @see {@link https://loglayer.dev/transports/creating-transports.html | Creating Transports} */ declare abstract class LoggerlessTransport implements LogLayerTransport { /** * An identifier for the transport. If not defined, a random one will be generated. */ id?: string; /** * If false, the transport will not send logs to the logger. */ enabled: boolean; /** * Minimum log level to process. Defaults to "trace". */ level?: LogLevel | "trace" | "debug" | "info" | "warn" | "error" | "fatal"; /** * If true, the transport will log to the console for debugging purposes */ protected consoleDebug?: boolean; constructor(config: LoggerlessTransportConfig); /** * LogLayer calls this to send logs to the transport */ _sendToLogger(params: LogLayerTransportParams): void; /** * Returns the logger instance attached to the transport */ getLoggerInstance(): void; /** * Sends the log data to the logger for transport */ abstract shipToLogger(params: LogLayerTransportParams): any[]; } export { BaseTransport, type LogLayerTransportConfig, LogLevelPriority, type LoggerLibrary, LoggerlessTransport, type LoggerlessTransportConfig, testTransportOutput };