Logging Node-Red to File

I’ve got a rather complex flow that I am trying to debug. I know I could review the log file but because the flow has to run for several hours watching the log output on the console isn’t ideal. I would like to output the log information to a file but for the life of me can’t find any detailed help on how to do that. I’ve reviewed https://nodered.org/docs/user-guide/runtime/logging but the bit about custom logging is a tad beyond me.

Any suggestions? I’m running HassOS on a Raspberry Pi with Node-Red set up as an integration.

Thanks.

What a coincidence. I just did setup a log file to log when my router spontaneously resets.

It’s really not hard, Here’s the flow that uses simpletime and a function node to add date and time to msg.payload. Just enter simpletime with whatever you want to log in msg.payload.

[{"id":"93edaf35.bb12b","type":"file","z":"eb64de9a.f978e","name":"","filename":"/share/logs/router.log","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":800,"y":2420,"wires":[[]]},{"id":"238a0338.8d7edc","type":"function","z":"eb64de9a.f978e","name":"Add the date & time and topic to the payload","func":"//Add the date & time to the payload\n\nvar str = msg.mydate + ',' + msg.mytime + ',' + msg.payload;\nmsg.payload = str;\nreturn msg;\n","outputs":1,"noerr":0,"x":490,"y":2420,"wires":[["93edaf35.bb12b"]]},{"id":"81aabe64.18e9f","type":"simpletime","z":"eb64de9a.f978e","name":"","x":210,"y":2420,"wires":[["238a0338.8d7edc"]]}]

I am on an Intel NUC, so your file path will be different on the Raspberry. You can use an inject node to force the file out node to create a file, then use find to find where the OS put it. Or, just wait and someone with more experience than I will tell you the path.

If the log file is always the same name, then you could make the logging nodes into a subflow.

There’s a component to help with that - node-red-contrib-flogger. Beyond just writing to a file, it also handles log rotation with/without compression. One thing it couldn’t handle was log rotation triggered by a flow. I created a fork called node-red-contrib-flogger-now that adds that capability.

1 Like

Thanks for the suggestions but I’m not trying to log the output of a given node or two, I’m trying to log the activity of every node in the flow. Basically, I’d like to write to a file the information that is displayed in the log file of the Node-Red add-on. Sorry if I am not being clear.

1 Like

Yep, I wanted the same, to collect the logs.
Each debug node has an option to output to system log, but I’m not sure what that means

Hello @danielo515
Have you been able to find out how to read the logs when using node.log/warn and so on?
I’m struggling to find out about that while it’s looking so basic :open_mouth:
Thanks!

Yes.
You have to go to the addons section on home-assistant, open the node-red addon and there will be the logs of your functions mixed with all the application logs, like this:

At least they prepend the function name. It’s very lame, but it is better than nothing. I wish node-red had a built-in viewer, to be honest, but the tool itself is very lacky in terms of debugging and visibility.

2 Likes

Thanks for your quick answer, this did the trick about debug node and system log.
I must agree I kind of am disappointed.

I finally now (partly) understand how logging is handled.
I also found out that my node.warn / node.error weren’t showing up because they were after a return :frowning_face: I assume that’s common after trying many things over night :slight_smile:

That is the kind of thing any IDE will warn you about. I wish there was a way to build the functions on a proper ide, save them to the node-red folder and be able to use them.