How to use a file name generator function
masterIf a simple string is insufficient for your naming requirements, you can provide a function as the filename argument. This function is called to determine the name of the rotated files.
Function Signature: filename(time[, index])
time(Date):- By default: the time when the rotation job started.
- If both
options.intervalandoptions.intervalBoundaryare enabled: the start time of the rotation period. - If you return
null, the generator indicates that the not-rotated file name should be used.
index(number): The progressive index of rotation by size within the same rotation period.
Important: If both options.interval and options.size are used, the returned file name must be a function of both time and index.
If options.rotate (classical UNIX logrotate behavior) is enabled, the file name is only a function of index.
const pad = num => (num > 9 ? "" : "0") + num;
const generator = (time, index) => {
if (!time) return "file.log";
var month = time.getFullYear() + "" + pad(time.getMonth() + 1);
var day = pad(time.getDate());
var hour = pad(time.getHours());
var minute = pad(time.getMinutes());
return `${month}/${month}${day}-${hour}${minute}-${index}-file.log`;
};
const rfs = require("rotating-file-stream");
const stream = rfs.createStream(generator, {
size: "10M",
interval: "30m"
});