@tryghost/bunyan-rotating-filestream 0.0.14 → 0.0.15

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.
package/README.md CHANGED
@@ -37,6 +37,21 @@ const log = bunyan.createLogger({
37
37
 
38
38
  Other options include `startNewFile` to always open a new file on start-up.
39
39
 
40
+ ### Flushing
41
+
42
+ Writes are queued and written asynchronously, so a process that exits right
43
+ after logging can lose the last lines. `flush()` drains everything written so
44
+ far to disk and leaves the stream open:
45
+
46
+ ```js
47
+ log.error('fatal boot error');
48
+ await stream.flush();
49
+ process.exit(1);
50
+ ```
51
+
52
+ Use `end()` instead when shutting the stream down for good — it also drains the
53
+ queue, but closes the file handles and stops rotation.
54
+
40
55
  ## Develop
41
56
 
42
57
  This is a mono repository, managed with [Nx](https://nx.dev).
package/index.js CHANGED
@@ -61,6 +61,22 @@ class RotatingFileStream {
61
61
  this._queue.push(data);
62
62
  }
63
63
 
64
+ /**
65
+ * Force everything written into this stream so far out to disk, without
66
+ * closing it. The stream stays writable afterwards, unlike `end()`.
67
+ * @returns {Promise<void>}
68
+ */
69
+ async flush() {
70
+ if (this._closed) {
71
+ return;
72
+ }
73
+ await this._initialised;
74
+ // A rotation in progress owns the file handle - wait for it to hand
75
+ // the new one to the queue before draining
76
+ await this._rotating;
77
+ await this._queue.flush();
78
+ }
79
+
64
80
  async end() {
65
81
  // Block new rotations immediately - the threshold trigger has no timer
66
82
  // to shut down and can still fire while the queue drains below
package/lib/writeQueue.js CHANGED
@@ -36,6 +36,28 @@ class WriteQueue extends EventEmitter {
36
36
  this._writing = this._write(currentEventCount);
37
37
  }
38
38
 
39
+ /**
40
+ * Drain everything queued so far, leaving the queue writable afterwards.
41
+ *
42
+ * Writes are asynchronous, so a process that exits right after logging
43
+ * (e.g. on a fatal boot error) can lose the lines still sitting in the
44
+ * queue. Unlike `shutdown()` this does not pause the queue, so it is safe
45
+ * to call at any point in the process lifetime.
46
+ * @returns {Promise<void>}
47
+ */
48
+ async flush() {
49
+ // The in-flight write owns the events it already spliced off the
50
+ // queue, so wait for it before queueing another write
51
+ await this._writing;
52
+
53
+ // A single write only takes the events present when it started, and
54
+ // anything pushed while it ran is not picked up until the next push
55
+ while (!this._paused && this._events.length) {
56
+ this.flushToDisk();
57
+ await this._writing;
58
+ }
59
+ }
60
+
39
61
  async pause() {
40
62
  this._paused = true;
41
63
  await this._writing;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/bunyan-rotating-filestream",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "license": "MIT",
5
5
  "author": "Ghost Foundation",
6
6
  "repository": {