* Shutdown all log appenders. This will first disable all writing to appenders * and then call the shutdown function each appender. * * @param {shutdownCallback} [callback] - The callback to be invoked once all appenders have * shutdown. If an error occurs, the callback will be given the error
(callback = () => {})
| 103 | * as the first argument. |
| 104 | */ |
| 105 | function shutdown(callback = () => {}) { |
| 106 | if (typeof callback !== 'function') { |
| 107 | throw new TypeError('Invalid callback passed to shutdown'); |
| 108 | } |
| 109 | debug('Shutdown called. Disabling all log writing.'); |
| 110 | // First, disable all writing to appenders. This prevents appenders from |
| 111 | // not being able to be drained because of run-away log writes. |
| 112 | enabled = false; |
| 113 | |
| 114 | // Clone out to maintain a reference |
| 115 | const appendersToCheck = Array.from(appenders.values()); |
| 116 | |
| 117 | // Reset immediately to prevent leaks |
| 118 | appenders.init(); |
| 119 | categories.init(); |
| 120 | |
| 121 | // Count the number of shutdown functions |
| 122 | const shutdownFunctions = appendersToCheck.reduce( |
| 123 | (accum, next) => (next.shutdown ? accum + 1 : accum), |
| 124 | 0 |
| 125 | ); |
| 126 | if (shutdownFunctions === 0) { |
| 127 | debug('No appenders with shutdown functions found.'); |
| 128 | callback(); |
| 129 | } |
| 130 | |
| 131 | let completed = 0; |
| 132 | let error; |
| 133 | debug(`Found ${shutdownFunctions} appenders with shutdown functions.`); |
| 134 | function complete(err) { |
| 135 | error = error || err; |
| 136 | completed += 1; |
| 137 | debug(`Appender shutdowns complete: ${completed} / ${shutdownFunctions}`); |
| 138 | if (completed >= shutdownFunctions) { |
| 139 | debug('All shutdown functions completed.'); |
| 140 | callback(error); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Call each of the shutdown functions |
| 145 | appendersToCheck |
| 146 | .filter((a) => a.shutdown) |
| 147 | .forEach((a) => a.shutdown(complete)); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Get a logger instance. |