rotating-file-stream-4js
v4.0.0
Published
log4js appender backed by rotating-file-stream.
Maintainers
Readme
rotating-file-stream-4js
Description
log4js appender that writes through rotating-file-stream.
Usage
import log4js from "log4js";
log4js.configure({
appenders: {
out: {
type: "rotating-file-stream-4js",
rfs: {
filename: "file.log",
options: { size: "10M", interval: "1d", compress: "gzip" },
onStream: stream => {
stream.on("rotated", console.log);
}
}
}
},
categories: {
default: { appenders: ["out"], level: "info" }
}
});
const log = log4js.getLogger();
log.info("hello");Installation
With npm:
$ npm install --save rotating-file-stream-4jsTable of contents
- Configuration
- Using the same stream for more appenders
- Compatibility
- TypeScript
- License
- Bugs
- ChangeLog
- Donating
Configuration
The appender accepts the usual log4js appender fields, plus a required rfs object.
{
type: "rotating-file-stream-4js",
layout: { type: "basic" },
timezoneOffset: 0,
rfs: {
filename: "file.log",
options: { size: "10M" },
onStream: stream => {
stream.on("rotated", console.log);
}
}
}The module object can also be passed as type (useful with bundlers):
import rotatingFileStream4js from "rotating-file-stream-4js";
log4js.configure({
appenders: {
out: {
type: rotatingFileStream4js,
rfs: {
filename: "file.log",
options: { size: "10M" },
onStream: stream => {
stream.on("rotated", console.log);
}
}
}
},
categories: { default: { appenders: ["out"], level: "info" } }
});rfs
Required when the appender creates the stream (type: "rotating-file-stream-4js"). Ignored when the
appender comes from shareStream.
filename<string>|<Function>: First argument ofcreateStream(filename[, options]). Required.options<Object>: Second argument ofcreateStream(filename[, options]). Same shape as rotating-file-streamOptions.onStream<Function>: Called synchronously with the opened stream before any log line is written, so listeners can be attached.
Using the same stream for more appenders
With the standard configuration each appender opens its own stream. When several appenders must write
through one shared stream, use shareStream: it returns a log4js appender module bound to that stream,
and each appender entry can still set its own layout and timezoneOffset.
import log4js from "log4js";
import { createStream, shareStream } from "rotating-file-stream-4js";
const stream = createStream("file.log", { size: "10M", interval: "1d" });
const shared = shareStream(stream);
stream.on("rotated", console.log);
log4js.configure({
appenders: {
app: {
type: shared,
layout: { type: "pattern", pattern: "APP %m" }
},
access: {
type: shared,
layout: { type: "pattern", pattern: "ACCESS %m" }
}
},
categories: {
default: { appenders: ["app"], level: "info" },
access: { appenders: ["access"], level: "info" }
}
});Compatibility
Requires Node.js v22 and a current log4js release.
TypeScript
TypeScript types are distributed with the package itself. Everything exported by
rotating-file-stream is re-exported, so a single import is enough.
License
Bugs
Do not hesitate to report any bug or inconsistency @github.
ChangeLog
Donating
If you find useful this package, please consider the opportunity to donate some satoshis to this bitcoin address: 12p1p5q7sK75tPyuesZmssiMYr4TKzpSCN
