bare-file-logger 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +29 -1
  2. package/index.js +44 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -23,10 +23,38 @@ options = {
23
23
  // Maximum allowed byte size of the log file. This is a hint and not a hard
24
24
  // limit; the logger will do its best to keep the file size within the limit,
25
25
  // but provides no guarantees.
26
- maxSize: 0
26
+ maxSize: 0,
27
+
28
+ // A function called when the file size reaches `maxSize`. It receives the
29
+ // current file path and should return a new path to rename the file to, or a
30
+ // falsy value to do nothing. After a successful rename, a new empty log file
31
+ // is opened at the original path.
32
+ rotate: null,
33
+
34
+ // The interval in milliseconds at which the file size is checked against
35
+ // `maxSize` for rotation. Only active when both `maxSize` and `rotate` are
36
+ // set. The check starts after the first write.
37
+ rotateInterval: 2000
27
38
  }
28
39
  ```
29
40
 
41
+ #### Rotation
42
+
43
+ ```js
44
+ const log = new FileLog('app.log', {
45
+ maxSize: 1024 * 1024, // 1 MB
46
+ rotate(filePath) {
47
+ return filePath + '.' + Date.now()
48
+ }
49
+ })
50
+ ```
51
+
52
+ When the log file reaches `maxSize`, the `rotate` function is called. If it returns a path, the current file is renamed to that path and a fresh log file is opened. If it returns a falsy value, no action is taken.
53
+
54
+ #### `log.on('rotate', (path, dest) => {})`
55
+
56
+ Emitted after a successful rotation. `path` is the original log file path (now empty and ready for new writes) and `dest` is the path the previous log was renamed to.
57
+
30
58
  ## License
31
59
 
32
60
  Apache-2.0
package/index.js CHANGED
@@ -1,14 +1,19 @@
1
1
  const Log = require('bare-logger')
2
+ const EventEmitter = require('bare-events')
2
3
  const fs = require('bare-fs')
3
4
 
4
- module.exports = class FileLog extends Log {
5
+ class FileLog extends EventEmitter {
5
6
  constructor(path, opts = {}) {
6
- const { maxSize = 0 } = opts
7
+ const { maxSize = 0, rotate = null, rotateInterval = 2000 } = opts
7
8
 
8
- super({ colors: false })
9
+ super()
9
10
 
11
+ this.colors = false
10
12
  this._path = path
11
13
  this._maxSize = maxSize
14
+ this._rotate = rotate
15
+ this._rotateInterval = rotateInterval
16
+ this._interval = null
12
17
  this._fd = fs.openSync(this._path, 'a+')
13
18
 
14
19
  if (this._maxSize > 0 && fs.fstatSync(this._fd).size > this._maxSize) {
@@ -16,11 +21,39 @@ module.exports = class FileLog extends Log {
16
21
  }
17
22
  }
18
23
 
24
+ _checkRotate() {
25
+ if (this._fd === -1) return
26
+ if (this._maxSize <= 0 || this._rotate === null) return
27
+
28
+ const size = fs.fstatSync(this._fd).size
29
+
30
+ if (size >= this._maxSize) {
31
+ const dest = this._rotate(this._path)
32
+
33
+ if (dest) {
34
+ fs.closeSync(this._fd)
35
+ fs.renameSync(this._path, dest)
36
+ this._fd = fs.openSync(this._path, 'a+')
37
+ this.emit('rotate', this._path, dest)
38
+ }
39
+ }
40
+ }
41
+
42
+ _startRotateCheck() {
43
+ if (this._interval !== null) return
44
+ if (this._maxSize <= 0 || this._rotate === null) return
45
+
46
+ this._interval = setInterval(() => this._checkRotate(), this._rotateInterval)
47
+ this._interval.unref()
48
+ }
49
+
19
50
  append(label, ...data) {
20
51
  fs.writeSync(
21
52
  this._fd,
22
53
  label.padEnd(5, ' ') + ' ' + new Date().toISOString() + ' ' + this.format(...data) + '\n'
23
54
  )
55
+
56
+ this._startRotateCheck()
24
57
  }
25
58
 
26
59
  debug(...data) {
@@ -51,6 +84,10 @@ module.exports = class FileLog extends Log {
51
84
 
52
85
  close() {
53
86
  if (this._fd === -1) return
87
+ if (this._interval !== null) {
88
+ clearInterval(this._interval)
89
+ this._interval = null
90
+ }
54
91
  fs.closeSync(this._fd)
55
92
  this._fd = -1
56
93
  }
@@ -59,3 +96,7 @@ module.exports = class FileLog extends Log {
59
96
  this.close()
60
97
  }
61
98
  }
99
+
100
+ FileLog.prototype.format = Log.prototype.format
101
+
102
+ module.exports = FileLog
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-file-logger",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "File system logger for Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",