From 5f0cab8cc2a51843a82da92557f1b1b358510458 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Thu, 20 Oct 2016 23:31:40 +0100 Subject: [PATCH] Add log.removeHandler function --- red/runtime/log.js | 6 ++++ test/red/runtime/log_spec.js | 67 +++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/red/runtime/log.js b/red/runtime/log.js index 21a32c005..160b18dd6 100644 --- a/red/runtime/log.js +++ b/red/runtime/log.js @@ -114,6 +114,12 @@ var log = module.exports = { addHandler: function(func) { logHandlers.push(func); }, + removeHandler: function(func) { + var index = logHandlers.indexOf(func); + if (index > -1) { + logHandlers.splice(index,1); + } + }, log: function(msg) { msg.timestamp = Date.now(); logHandlers.forEach(function(handler) { diff --git a/test/red/runtime/log_spec.js b/test/red/runtime/log_spec.js index c4b1faeb1..1f1537cc7 100644 --- a/test/red/runtime/log_spec.js +++ b/test/red/runtime/log_spec.js @@ -16,7 +16,6 @@ var should = require("should"); var sinon = require("sinon"); var util = require("util"); - var log = require("../../../red/runtime/log"); describe("runtime/log", function() { @@ -156,4 +155,70 @@ describe("runtime/log", function() { sinon.assert.neverCalledWithMatch(util.log,"[trace] This is a trace"); sinon.assert.neverCalledWithMatch(util.log,"[metric] "); }); + + it('add a custom log handler directly', function() { + var settings = {}; + log.init(settings); + + var logEvents = []; + var loggerOne = { + emit: function(event,msg) { + logEvents.push({logger:1,msg:msg}); + } + }; + var loggerTwo = { + emit: function(event,msg) { + logEvents.push({logger:2,msg:msg}); + } + }; + log.addHandler(loggerOne); + log.addHandler(loggerTwo); + + log.error("This is an error"); + log.warn("This is a warn"); + log.info("This is an info"); + log.debug("This is a debug"); + log.trace("This is a trace"); + log.log({level:log.METRIC,msg:"testMetric"}); + + logEvents.filter(function(evt) { return evt.logger === 1}).should.have.lengthOf(6); + logEvents.filter(function(evt) { return evt.logger === 2}).should.have.lengthOf(6); + }); + + it('remove a custom log handler directly', function() { + var settings = {}; + log.init(settings); + + var logEvents = []; + var loggerOne = { + emit: function(event,msg) { + logEvents.push({logger:1,msg:msg}); + } + }; + var loggerTwo = { + emit: function(event,msg) { + logEvents.push({logger:2,msg:msg}); + } + }; + log.addHandler(loggerOne); + log.addHandler(loggerTwo); + + log.info("This is an info"); + logEvents.filter(function(evt) { return evt.logger === 1}).should.have.lengthOf(1); + logEvents.filter(function(evt) { return evt.logger === 2}).should.have.lengthOf(1); + + + log.removeHandler(loggerTwo); + log.info("This is an info"); + logEvents.filter(function(evt) { return evt.logger === 1}).should.have.lengthOf(2); + logEvents.filter(function(evt) { return evt.logger === 2}).should.have.lengthOf(1); + + log.removeHandler(loggerOne); + log.info("This is an info"); + logEvents.filter(function(evt) { return evt.logger === 1}).should.have.lengthOf(2); + logEvents.filter(function(evt) { return evt.logger === 2}).should.have.lengthOf(1); + + + }); + });