Can I somehow redirect console.log() to the debug tab?

2,345 views
Skip to first unread message

Dan

unread,
Feb 28, 2015, 5:57:45 AM2/28/15
to node...@googlegroups.com
I'm not always have a ssh open to the server. 
Is there a way to log to the debug tab with console.log() or is there another command? 
It should be possible to log within a function node. I know that otherwise I can use the debug node ;-)
Thanks
Dan

Antoine Aflalo

unread,
Feb 28, 2015, 6:49:39 AM2/28/15
to node...@googlegroups.com
Hello,

That's quite a good idea, I just created a PR to add a Logger Object to the function node.

Logger.log() won't log to the debug tab because of the way the DebugLogHandler is working (only listening to Warning and Error).
To change that behaviour, we we need to discuss with Nicolas and Dave.

Hope this PR can help,
BR,
Antoine Aflalo

--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Antoine Aflalo

Dan

unread,
Feb 28, 2015, 7:38:20 AM2/28/15
to node...@googlegroups.com
Thanks Antoine,

It would also be good to have a resource (help page, article, etc) with some litte examples on how to do logging with node-red. Especially for people like me who have no clue about node.js underneath node-red and just want to have a mechanism to see what happens. :-) Most of that practices and tools are well know for the programmers like you...

I would love to see something which describes the different means of doing meaningful debugging:
- debugging between nodes (i.e. debug nodes, there are several ways using those debug nodes)
- from within nodes (especially function nodes), ie. console.log() or other functions. Would need to include a description on where the logs actually can be found and what I need to do to see them (ssh, tail -f logfile, etc..)
- a way to control debugging from the "outside", i.e. setting a flag somewhere in the red-node GUI, probably per tab. Perhaps just a node similar to inject that just sets a global variable to true or false? I think I've read something about util.debuglog(section) (based on your remark in your PR I started digging). It would have to be exposed to the node-red GUI, though.
- best practices with flow examples (different usage of debug nodes) and function code examples (use of all those nifty log functions I have not yet discovered...)
- probably, in another section, best practices for node developers to add debug capabilities to the node "blackboxes". I guess that would also extend to the creation of subflows?

Just my .02c

Dan

Dave C-J

unread,
Feb 28, 2015, 10:34:09 AM2/28/15
to node...@googlegroups.com
Hi - yes the whole area of debug and error handling is just about top of the current list.
A quick hack you can do is to start node-red re-directing it's output to a file.. eg
    node-red  > /tmp/node-red.log
and then use a tail file node to tail that file and feed into a debug node :-) 
not exactly ideal but ....

As Antoine points out you can't actually log to the debug window from within a function node just yet... best you can do is console.log - which as you know only goes to the main log. Though of course there is nothing to stop you passing any info out of your function on the msg object.... - just add a property msg.log = "My error"; sort of thing and then use the debug to look at that... (but of course you can only return that when you complete your function...)

Antoine Aflalo

unread,
Feb 28, 2015, 10:41:58 AM2/28/15
to node...@googlegroups.com
Hi Dave,

Is there a reason why the LogHandler of the Debug node is not listening to Log.INFO events ?

Using the settings api, do you think it would be possible to make the a menu item in the UI to select which type of "log event" the debug tab will be connected to ?

BR,
Antoine

--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Antoine Aflalo

Nicholas O'Leary

unread,
Feb 28, 2015, 12:20:47 PM2/28/15
to node...@googlegroups.com

As Dave mentioned, improving debugging and error handling is a focus area right now. For example, the new error handler node describe on the issue list: https://github.com/node-red/node-red/issues/560

Rather than do things piecemeal, we want to have a coherent overall plan in place.

We dont send .info to debug as it could overwhelm the useful things that have been explicitly wired to debug. We have an issue open around making the Debug sidebar more capable - https://github.com/node-red/node-red/issues/545 This could well include options to access the log output. The exact mechanism by which that would get enabled will depend on the wider design of the debug tab.

Enabling log/warn/debug from the function node has always been on the todo list and is in the plan for this release. Not had a chance to look at your PR but will do later and see how it fits with what we had in mind, along with the new error handler node and the requirements it brings.

Dan - all of this work will feed into better documentation around how you debug things in Node-RED.

Nick

Dan

unread,
Feb 28, 2015, 12:43:51 PM2/28/15
to node...@googlegroups.com
Ah... yes.. the tile node. 
That is a fine workaround. I'm going to use console.log() and tail for now. (until you come up with a great debugging facility ;-)
Thanks many times.
Dan

Dan

unread,
Feb 28, 2015, 12:45:40 PM2/28/15
to node...@googlegroups.com
Thanks Nick,
Looking forward to that.
Dan

Dan

unread,
Feb 28, 2015, 2:51:38 PM2/28/15
to node...@googlegroups.com
I know this can be done simpler and more elegant. And for sure more portable. You are welcome to improve, correct, make it muuuuch better.. But here is my take on this (remember, i'm not a programmer and I have no clue about node.js).. 

[{"id":"2f9ab2c7.d0654e","type":"inject","name":"Debug = TRUE","topic":"TRUE","payload":"TRUE","payloadType":"string","repeat":"","crontab":"","once":false,"x":134,"y":133,"z":"59e2a191.a61d6","wires":[["bc4f4995.43b0b8"]]},{"id":"bc4f4995.43b0b8","type":"function","name":"set context.global.mydebug TRUE or FALSE","func":"context.global.mydebug = msg.payload;\nreturn msg;","outputs":1,"x":405,"y":117,"z":"59e2a191.a61d6","wires":[["d6e3be11.291c4"]]},{"id":"780d77e5.87f288","type":"inject","name":"Check debug","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":128,"y":173,"z":"59e2a191.a61d6","wires":[["4f825917.b07da8"]]},{"id":"4f825917.b07da8","type":"function","name":"Check debug status","func":"msg.payload = context.global.mydebug;\nreturn msg;","outputs":1,"x":329,"y":173,"z":"59e2a191.a61d6","wires":[["d6e3be11.291c4"]]},{"id":"d6e3be11.291c4","type":"debug","name":"debugstatus","active":true,"console":"false","complete":"payload","x":684.0000190734863,"y":172.66667366027832,"z":"59e2a191.a61d6","wires":[]},{"id":"7d69b73a.829648","type":"inject","name":"Debug = FALSE","topic":"FALSE","payload":"FALSE","payloadType":"string","repeat":"","crontab":"","once":false,"x":137,"y":98,"z":"59e2a191.a61d6","wires":[["bc4f4995.43b0b8"]]},{"id":"d7767382.28899","type":"tail","name":"Get logfile","split":true,"filename":"/var/log/node-red.log","x":127.8823471069336,"y":259.2941198348999,"z":"59e2a191.a61d6","wires":[["124afd8e.edb502"]]},{"id":"124afd8e.edb502","type":"debug","name":"Redirected node-red.log","active":true,"console":"false","complete":"payload","x":350.9999885559082,"y":259.52941513061523,"z":"59e2a191.a61d6","wires":[]},{"id":"53132066.acece","type":"comment","name":"Set or check mydebug flag","info":"","x":147,"y":56,"z":"59e2a191.a61d6","wires":[]},{"id":"e93238de.16cdc8","type":"comment","name":"tail the console log","info":"Node-Red is strated like this:\n\nnode red.js -v > /var/log/node-red.log 2&>1\n","x":119.41177368164062,"y":224.23528957366943,"z":"59e2a191.a61d6","wires":[]},{"id":"f6564555.09a9b8","type":"function","name":"Just testing","func":"//Just for testing console.log() workaround\n\n// msg.payload = context.global.mydebug;\n// return msg;\n\nfunction mydebug(debugtext) {\n\tdebugflag = context.global.mydebug;\n\tif (debugflag == \"TRUE\") {\n\t   console.log(debugtext+\"\\n\"); \n\t} else {\n//\t   return [ msg, null ];\n\t}\n\n}\n\nmydebug(\"Debugging is TRUE\"); \n\nswitch (msg.topic) {\n    case \"One\":\n        msg.payload = \"Got input One\";\n        mydebug(msg.payload); \n        msg = null;\n        break;\n    case \"Two\":\n        msg.payload = \"Got input Two\";\n        mydebug(msg.payload); \n        msg = null;\n        break;\n        \n    default:\n    \tmydebug(\"Apparently I got an Invalid Topic\"); \n        msg = null;\n    \tbreak;\n\n} \nreturn msg;","outputs":1,"x":436.42016983032227,"y":476.0924549102783,"z":"59e2a191.a61d6","wires":[["5ebfcfd4.a1403"]]},{"id":"9d1b5ff1.62e4a","type":"inject","name":"","topic":"One","payload":"1","payloadType":"string","repeat":"","crontab":"","once":false,"x":120.42015838623047,"y":438.52100563049316,"z":"59e2a191.a61d6","wires":[["f6564555.09a9b8"]]},{"id":"275f10f5.d8a0f","type":"inject","name":"","topic":"Two","payload":"2","payloadType":"string","repeat":"","crontab":"","once":false,"x":120.13443756103516,"y":479.23530292510986,"z":"59e2a191.a61d6","wires":[["f6564555.09a9b8"]]},{"id":"100bfa9c.eff405","type":"comment","name":"Just a test case","info":"Testing mydebug flag.\nI am just interested in the console.log() stuff","x":117.14286041259766,"y":401.4285955429077,"z":"59e2a191.a61d6","wires":[]},{"id":"f18f938c.0e707","type":"inject","name":"","topic":"Invalid Topic","payload":"99","payloadType":"string","repeat":"","crontab":"","once":false,"x":143.5714340209961,"y":515.7142944335938,"z":"59e2a191.a61d6","wires":[["f6564555.09a9b8"]]},{"id":"5ebfcfd4.a1403","type":"debug","name":"","active":true,"console":"false","complete":"false","x":597.4999999999999,"y":476.1666666666666,"z":"59e2a191.a61d6","wires":[]}]

Debug is either TRUE or FALSE. Can be set with injects. I am using context.global.mydebug for the debugflag. 
I start node-red so that STDOUT goes to /var/log/node-red.log, and I tail that file.
I use a function mydebug(debugtext) which will use console.log() based on the debugflag. 

Perhaps the function can be a global function? I have not understood if this is possible and if yes, how.
Perhaps the whole debug stuff can be put into a subflow... or an own node with a nice GUI? No clue...

But I guess you get the idea?

Daniel

Julian Knight

unread,
Mar 1, 2015, 11:01:53 AM3/1/15
to node...@googlegroups.com
You might also want to think about running NR under Forever or PM2 or such like and redirect logs to file that way. I'm using PM2 as it works on all platforms I use. The Tail node as previously suggested works fine if you want to show the output in NR.

Using Forever/PM2/etc has the added benefit the the service restarts itself on a crash.
Reply all
Reply to author
Forward
0 new messages