3.0.0 • Published 12 months ago

file-timestamp-stream v3.0.0

Weekly downloads
151
License
MIT
Repository
github
Last release
12 months ago

file-timestamp-stream

GitHub CI Trunk Check Coverage Status npm

This module creates stream.Writable to a file which is automatically rotated based on current time and uses strftime template for file names.

Requirements

This module requires ES2021 with Node >= 16.

Installation

npm install file-timestamp-stream

Additionally for Typescript:

npm install -D @types/node

Usage

Example:

import FileTimestampStream from "file-timestamp-stream"
// or
import {FileTimestampStream} from "file-timestamp-stream"

Options

  • flags is a string with flags for the opened stream (default: 'a')
  • fs is a custom fs module (optional)
  • path is a template for new filenames (default: 'out.log')

Example:

Basic path based on strftime parameters:

const stream = new FileTimestampStream({
  path: "%Y-%m-%dT%H.log",
  flags: "a",
})

newFilename

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:

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",
})

Properties

Readonly public properties based on contructor's options:

  • flags
  • fs
  • path

Protected properties for a custom subclass:

  • currentFilename contains the last opened filename
  • stream contains current fs.WriteStream object

Path template format

A path can contain strftime specifiers.

Warning

This stream has to be closed to free streams and timers.

License

Copyright (c) 2017-2024 Piotr Roszatycki piotr.roszatycki@gmail.com

MIT

3.0.0

12 months ago

2.2.4

6 years ago

2.2.3

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.4

6 years ago

2.1.3

6 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.0

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.6.0

7 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.0

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago