Log rotation in Node-RED

#1

How to rotate log files in Node-RED? I will be writing Node-RED logs to a log file. But I want a new file to be created/replace the existing file when it reaches a particular time or size limit. How can I achieve this like I do in Java and other programming languages?

If my approach is wrong in this scenario, please advice the right approach.

0 Likes

#2

Assuming you are using a Linux OS such as Raspbian then a good way to do this is by using lograte, which is how system logs and so on are rotated. Node red need not be involved at all.

1 Like

#3

As Colin says this isn't really a Node-RED question. By default Node-RED logs to the console so nothing to rotate. If you run it as a systemd service (eg on Pi or Linux) then it uses the system log which is already part of logrotate so doesn't need to be configured. If it's your own logging setup then how you rotate the log is down to how your platform/os does it already. If linux then /etc/logrotate.d would be where I would add an entry to rotate my own log file.

0 Likes

#4

Okay. Based on the comments, it need not be related to Node-RED.
By default, Node-RED logs to the console. If I want these to go to a file instead, I need to use some approach. For function nodes, since I use Javascript, I need to use some logging option in Javascript. But for other nodes in Node-RED, it has to be set somewhere in Node-RED settings or config, right?

0 Likes

#5

Where node red logs to depends on your OS and how you installed and how you run node red. So tell us that information please.

0 Likes

#6

Have you tried reading the docs?

There’s a page that explains Node-RED logging
https://nodered.org/docs/user-guide/logging

There’s also a section in writing nodes about how additional nodes write to a log

0 Likes

#7

It is Windows 7. Installed npm and then NodeRED via command prompt as given in the NodeRED tutorial.

0 Likes

#8

Already went through it. I could not find details about log rotation.

0 Likes

#9

No, it doesn't specifically mention log rotation. But those pages do explain how you can write your own custom logging tool which could do log rotation and how nodes write log entries.

0 Likes

#10

Does node-RED on Windows not write to a log file by default as it does on Linux systems?

0 Likes

#11

By default - on all platforms - it just writes to the console. On Pi we add the ability to run it as a service using systemd - which if you start it that way - then redirects the console to the system log. On windows we don't have systemd - so it depends how people wish to run it as a service (if at all) as to where the logs then end up.

Of course one simple way is to just redirect the console to a file - but then (as the OP says) you may then need to rotate that file.

The other way is to write a custom logger (as per the docs) - which means you can then write them wherever - and add as much rotation complexity as you wish.

0 Likes

#12

OK, understood, thanks.

0 Likes