Writing stream with file rotating based on timestamp
npm install file-timestamp-stream




This module creates
stream.Writable
to a file which is automatically rotated based on current time and uses
strftime template for file names.
This module requires ES2021 with Node >= 16.
``shell`
npm install file-timestamp-stream
_Additionally for Typescript:_
`shell`
npm install -D @types/node
_Example:_
`js`
import FileTimestampStream from "file-timestamp-stream"
// or
import {FileTimestampStream} from "file-timestamp-stream"
- flags is a string with'a'
flags
for the opened stream (default: )fs
- is a custom fs module (optional)path
- is a template for new filenames (default: 'out.log')
_Example:_
Basic path based on strftime parameters:
`js`
const stream = new FileTimestampStream({
path: "%Y-%m-%dT%H.log",
flags: "a",
})
This method can be overridden in the subclass.
The method generates a filename for new files. By default, it returns a new
filename based on path and current time.
_Example:_
`ts
import strftime from "ultra-strftime"
class MyFileTimestampStream extends FileTimestampStream {
/* count how many files has been created /
counter = 0
// for pure Javascript explicit constructor is necessary
// constructor (options) { super(options); this.counter = 0 }
protected newFilename(): string {
const filename = strftime(this.path)
if (filename !== this.currentFilename) this.counter++
return filename
}
}
const stream = new MyFileTimestampStream({
path: "%Y-%m-%dT%H:%M.log",
})
`
Readonly public properties based on contructor's options:
- flagsfs
- path
-
Protected properties for a custom subclass:
- currentFilename contains the last opened filenamestream` contains current
-
fs.WriteStream
object
A path can contain strftime specifiers.
This stream has to be closed to free streams and timers.
Copyright (c) 2017-2024 Piotr Roszatycki