From c8957c781cef675286c070c9524c7cd36bc81547 Mon Sep 17 00:00:00 2001 From: Chris Thompson Date: Thu, 3 Dec 2015 16:28:52 -0700 Subject: [PATCH] Add `restart` function for targets to support log rotation --- lib/Bristol.js | 11 +++++++++++ lib/targets/file.js | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/lib/Bristol.js b/lib/Bristol.js index 3044d23..fa1e0da 100644 --- a/lib/Bristol.js +++ b/lib/Bristol.js @@ -61,6 +61,7 @@ Bristol.prototype.addTarget = function(target, options) { target = require('./targets/' + target); var targetObj = { log: target.bind(target, options || {}), + restart: target.restart || function () {}, formatter: DEFAULT_FORMATTER.bind(this, [{}]), blacklist: {}, whitelist: {}, @@ -361,5 +362,15 @@ Bristol.prototype._transform = function(elem) { return result || elem; }; +/** + * Calls restart on all targets; for example, to support closing and reopening + * files for log rotation + */ +Bristol.prototype.restart = function () { + this._targets.forEach(function(target) { + target.restart(); + }); +} + module.exports = new Bristol(); module.exports.Bristol = Bristol; diff --git a/lib/targets/file.js b/lib/targets/file.js index 6f69d20..fa591b7 100644 --- a/lib/targets/file.js +++ b/lib/targets/file.js @@ -41,4 +41,11 @@ function log(options, severity, date, message) { out.write(message + "\n"); } +log.restart = function () { + for (key in streams) { + streams[key].end(); + } + streams = {}; +} + module.exports = log;