A transport for the LogLayer logging library that writes logs to files with automatic rotation based on size or time. Built on top of file-stream-rotator.
- Automatic log file rotation based on time (hourly, daily)
- Size-based rotation with support for KB, MB, and GB units
- Support for date patterns in filenames using numerical values
- Compression of rotated log files using gzip
- Maximum file count or age-based retention
- Automatic cleanup of old log files
- Batch processing of logs for improved performance
# npm
npm i loglayer @loglayer/transport-log-file-rotation serialize-error
# pnpm
pnpm add loglayer @loglayer/transport-log-file-rotation serialize-error
# yarn
yarn add loglayer @loglayer/transport-log-file-rotation serialize-error
import { LogLayer } from "loglayer";
import { LogFileRotationTransport } from "@loglayer/transport-log-file-rotation";
import { serializeError } from "serialize-error";
const logger = new LogLayer({
errorSerializer: serializeError,
transport: [
new LogFileRotationTransport({
filename: "./logs/app.log",
}),
],
});
logger.info("Application started");
Each instance of LogFileRotationTransport
must have a unique filename to prevent possible race conditions. If you try to create multiple transport instances with the same filename, an error will be thrown. If you need multiple loggers to write to the same file, they should share the same transport instance:
// Create a single transport instance
const fileTransport = new LogFileRotationTransport({
filename: "./logs/app-%DATE%.log",
dateFormat: "YMD",
frequency: "daily"
});
// Share it between multiple loggers
const logger1 = new LogLayer({
transport: [fileTransport]
});
const logger2 = new LogLayer({
transport: [fileTransport]
});
Child loggers do not have this problem as they inherit the transport instance from their parent logger.
For detailed documentation, including all configuration options and advanced features, visit the documentation.