mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02: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:
commit
ff093d98c6
@ -196,8 +196,14 @@
|
|||||||
'</span>';
|
'</span>';
|
||||||
// NOTE: relying on function error to have a "type" that all other msgs don't
|
// NOTE: relying on function error to have a "type" that all other msgs don't
|
||||||
if (o.hasOwnProperty("type") && (o.type === "function")) {
|
if (o.hasOwnProperty("type") && (o.type === "function")) {
|
||||||
msg.className = 'debug-message debug-message-level-20';
|
var errorLvlType = 'error';
|
||||||
msg.innerHTML += '<span class="debug-message-topic">[function] : (error)</span>';
|
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 {
|
} else {
|
||||||
msg.innerHTML += '<span class="debug-message-topic">'+(o.topic?topic+' : ':'')+
|
msg.innerHTML += '<span class="debug-message-topic">'+(o.topic?topic+' : ':'')+
|
||||||
(o.property?'[msg.'+property+']':'[msg]')+" : "+typ+'</span>';
|
(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>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
|
<p>By convention it will have a <code>msg.payload</code> property containing
|
||||||
the body of the message.</p>
|
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
|
<p>The function should return the messages it wants to pass on to the next nodes
|
||||||
in the flow. It can return:</p>
|
in the flow. It can return:</p>
|
||||||
<ul>
|
<ul>
|
||||||
|
@ -21,6 +21,7 @@ module.exports = function(RED) {
|
|||||||
|
|
||||||
function FunctionNode(n) {
|
function FunctionNode(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
|
var node = this;
|
||||||
this.name = n.name;
|
this.name = n.name;
|
||||||
this.func = n.func;
|
this.func = n.func;
|
||||||
var functionText = "var results = null; results = (function(msg){\n"+this.func+"\n})(msg);";
|
var functionText = "var results = null; results = (function(msg){\n"+this.func+"\n})(msg);";
|
||||||
@ -29,6 +30,17 @@ module.exports = function(RED) {
|
|||||||
console:console,
|
console:console,
|
||||||
util:util,
|
util:util,
|
||||||
Buffer:Buffer,
|
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: {
|
context: {
|
||||||
global:RED.settings.functionGlobalContext || {}
|
global:RED.settings.functionGlobalContext || {}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ describe('function node', function() {
|
|||||||
before(function(done) {
|
before(function(done) {
|
||||||
helper.startServer(done);
|
helper.startServer(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function() {
|
||||||
helper.unload();
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user