UNPKG

log4js-appender-cloudwatch

Version:
88 lines (87 loc) 3.52 kB
import { CloudWatchLogs, type CreateLogStreamRequest, type InputLogEvent } from "@aws-sdk/client-cloudwatch-logs"; import type { RegionInputConfig } from "@smithy/config-resolver/dist-types/regionConfig/resolveRegionConfig"; import type { AwsCredentialIdentity } from "@smithy/types/dist-types/identity/awsCredentialIdentity"; import type log4js from "log4js"; export interface Config extends CreateLogStreamRequest, Pick<RegionInputConfig, "region">, Pick<AwsCredentialIdentity, "accessKeyId" | "secretAccessKey" | "sessionToken"> { /** * defaults to http://npm.im/log4js-layout-json */ layout?: log4js.Layout; /** * Maximum number of log events to include in a single batch when sending. * Once the batch size is reached, it will be sent to CloudWatch. */ batchSize: number; /** * Maximum time (in milliseconds) to wait before sending a batch of logs, * regardless of the batch size. If the timeout is reached before the batch * size is met, the logs will be sent. */ bufferTimeout: number; /** * required policy: * - logs:CreateLogGroup * - logs:CreateLogStream */ createResources?: boolean; } declare module "log4js" { interface Appenders { CloudwatchAppender: { type: "log4js-appender-cloudwatch"; } & Config; } } export declare class LogBuffer { private _config; private _onReleaseCallback; private _timer; private _logs; constructor(_config: Config, _onReleaseCallback: (logs: Array<InputLogEvent>) => void); /** * Pushes a log message to the internal log buffer. * * @param message - The log message to be pushed. If it's an * object, it will be converted to a JSON string. * f * @param timestamp - The timestamp of the log * message. If not provided, the current timestamp will be used. */ push(message: string, timestamp?: number): void; /** * Releases the logs and clears the timer. */ release(): void; } export declare class CloudwatchAppender { private _config; private _layout; private _logEventBuffer; private _cloudwatchClient; constructor(_config: Config, _layout: log4js.LayoutFunction, _logEventBuffer: LogBuffer, _cloudwatchClient: CloudWatchLogs); /** * Creates log groups and streams in CloudWatch if they don't exist. * If resources already exist, the creation requests are silently ignored. */ private createLogGroups; /** * Verifies that the configured log groups and streams exist in CloudWatch. * * @throws {ConfigError} If log group/stream doesn't exist or credentials are invalid */ private initializeLogGroups; /** * Returns the appender function that will be used by log4js. * * The function processes logging events by formatting them using the configured layout * and buffering them for batch processing. * * @returns {log4js.AppenderFunction} The function that will handle logging events */ appenderFunction(): log4js.AppenderFunction; } export declare class ConfigError extends Error { constructor(msg: string, cause?: unknown); } export declare function createLogEventHandler(cloudwatchClient: CloudWatchLogs, config: Config): LogBuffer["_onReleaseCallback"]; export declare function configure(config: Config, layouts: log4js.LayoutsParam, _findAppender: () => log4js.AppenderFunction, _levels: log4js.Levels): log4js.AppenderFunction;