mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Merge pull request #576 from Belphemur/function-logger
Adding an Object Node to the sandbox of a function node
This commit is contained in:
		| @@ -196,8 +196,14 @@ | ||||
|                                 '</span>'; | ||||
|                 // NOTE: relying on function error to have a "type" that all other msgs don't | ||||
|                 if (o.hasOwnProperty("type") && (o.type === "function")) { | ||||
|                     msg.className = 'debug-message debug-message-level-20'; | ||||
|                     msg.innerHTML += '<span class="debug-message-topic">[function] : (error)</span>'; | ||||
|                     var errorLvlType = 'error'; | ||||
|                     var errorLvl = 20; | ||||
|                     if (o.hasOwnProperty("level") && o.level === 30) { | ||||
|                         errorLvl = 30; | ||||
|                         errorLvlType = 'warn' | ||||
|                     } | ||||
|                     msg.className = 'debug-message debug-message-level-' + errorLvl; | ||||
|                     msg.innerHTML += '<span class="debug-message-topic">[function] : (' + errorLvlType + ')</span>'; | ||||
|                 } else { | ||||
|                     msg.innerHTML += '<span class="debug-message-topic">'+(o.topic?topic+' : ':'')+ | ||||
|                                     (o.property?'[msg.'+property+']':'[msg]')+" : "+typ+'</span>'; | ||||
|   | ||||
| @@ -39,6 +39,13 @@ | ||||
|     <p>The message is passed in as a JavaScript object called <code>msg</code>.</p> | ||||
|     <p>By convention it will have a <code>msg.payload</code> property containing | ||||
|        the body of the message.</p> | ||||
|     <p>If your node need to log anything use the <code>node</code> object: | ||||
|         <ul> | ||||
|             <li>Info: <code>node.log(msg);</code></li> | ||||
|             <li>Warning: <code>node.warn(msg);</code></li> | ||||
|             <li>Error: <code>node.error(msg);</code></li> | ||||
|         </ul> | ||||
|     </p> | ||||
|     <p>The function should return the messages it wants to pass on to the next nodes | ||||
|     in the flow. It can return:</p> | ||||
|     <ul> | ||||
|   | ||||
| @@ -21,6 +21,7 @@ module.exports = function(RED) { | ||||
|  | ||||
|     function FunctionNode(n) { | ||||
|         RED.nodes.createNode(this,n); | ||||
|         var node = this; | ||||
|         this.name = n.name; | ||||
|         this.func = n.func; | ||||
|         var functionText = "var results = null; results = (function(msg){\n"+this.func+"\n})(msg);"; | ||||
| @@ -29,6 +30,17 @@ module.exports = function(RED) { | ||||
|             console:console, | ||||
|             util:util, | ||||
|             Buffer:Buffer, | ||||
|             node: { | ||||
|                 log : function() { | ||||
|                     node.log.apply(node, arguments); | ||||
|                 }, | ||||
|                 error: function(){ | ||||
|                     node.error.apply(node, arguments); | ||||
|                 }, | ||||
|                 warn: function() { | ||||
|                     node.warn.apply(node, arguments); | ||||
|                 } | ||||
|             }, | ||||
|             context: { | ||||
|                 global:RED.settings.functionGlobalContext || {} | ||||
|             } | ||||
|   | ||||
| @@ -23,7 +23,7 @@ describe('function node', function() { | ||||
|     before(function(done) { | ||||
|         helper.startServer(done); | ||||
|     }); | ||||
|      | ||||
|  | ||||
|     afterEach(function() { | ||||
|         helper.unload(); | ||||
|     }); | ||||
| @@ -153,5 +153,73 @@ describe('function node', function() { | ||||
|             } | ||||
|         }); | ||||
|     }); | ||||
|     describe('Logger', function () { | ||||
|         it('should log an Info Message', function (done) { | ||||
|             var flow = [{id: "n1", type: "function", wires: [["n2"]], func: "node.log('test');"}]; | ||||
|             helper.load(functionNode, flow, function () { | ||||
|                 var n1 = helper.getNode("n1"); | ||||
|                 n1.receive({payload: "foo", topic: "bar"}); | ||||
|                 try { | ||||
|                     helper.log().called.should.be.true; | ||||
|                     var logEvents = helper.log().args.filter(function (evt) { | ||||
|                         return evt[0].type == "function"; | ||||
|                     }); | ||||
|                     logEvents.should.have.length(1); | ||||
|                     var msg = logEvents[0][0]; | ||||
|                     msg.should.have.property('level', helper.log().INFO); | ||||
|                     msg.should.have.property('id', 'n1'); | ||||
|                     msg.should.have.property('type', 'function'); | ||||
|                     msg.should.have.property('msg', 'test'); | ||||
|                     done(); | ||||
|                 } catch (err) { | ||||
|                     done(err); | ||||
|                 } | ||||
|             }); | ||||
|         }); | ||||
|         it('should log a Warning Message', function (done) { | ||||
|             var flow = [{id: "n1", type: "function", wires: [["n2"]], func: "node.warn('test');"}]; | ||||
|             helper.load(functionNode, flow, function () { | ||||
|                 var n1 = helper.getNode("n1"); | ||||
|                 n1.receive({payload: "foo", topic: "bar"}); | ||||
|                 try { | ||||
|                     helper.log().called.should.be.true; | ||||
|                     var logEvents = helper.log().args.filter(function (evt) { | ||||
|                         return evt[0].type == "function"; | ||||
|                     }); | ||||
|                     logEvents.should.have.length(1); | ||||
|                     var msg = logEvents[0][0]; | ||||
|                     msg.should.have.property('level', helper.log().WARN); | ||||
|                     msg.should.have.property('id', 'n1'); | ||||
|                     msg.should.have.property('type', 'function'); | ||||
|                     msg.should.have.property('msg', 'test'); | ||||
|                     done(); | ||||
|                 } catch (err) { | ||||
|                     done(err); | ||||
|                 } | ||||
|             }); | ||||
|         }); | ||||
|         it('should log an Error Message', function (done) { | ||||
|             var flow = [{id: "n1", type: "function", wires: [["n2"]], func: "node.error('test');"}]; | ||||
|             helper.load(functionNode, flow, function () { | ||||
|                 var n1 = helper.getNode("n1"); | ||||
|                 n1.receive({payload: "foo", topic: "bar"}); | ||||
|                 try { | ||||
|                     helper.log().called.should.be.true; | ||||
|                     var logEvents = helper.log().args.filter(function (evt) { | ||||
|                         return evt[0].type == "function"; | ||||
|                     }); | ||||
|                     logEvents.should.have.length(1); | ||||
|                     var msg = logEvents[0][0]; | ||||
|                     msg.should.have.property('level', helper.log().ERROR); | ||||
|                     msg.should.have.property('id', 'n1'); | ||||
|                     msg.should.have.property('type', 'function'); | ||||
|                     msg.should.have.property('msg', 'test'); | ||||
|                     done(); | ||||
|                 } catch (err) { | ||||
|                     done(err); | ||||
|                 } | ||||
|             }); | ||||
|         }); | ||||
|     }); | ||||
|  | ||||
| }); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user