Running Node-Red on a remote computer with no display, it can be difficult to see exactly what is going on. This example allows you to see the Node-Red log output in a formatted web page.
Many people are now running Node-Red on a Pi or other Linux based computer using systemd and this enables the log information to be accessed via an application called journalctl
. Whilst this is normally run at a command line (using SSH/PuTTY for example), it can also be called from within Node-Red which is what we do here.
Unfortunately, journalctl also outputs some additional runtime information, especially if you are running exec
nodes. In this example, we strip out those extra lines so that we can see exactly what Node-Red is doing. You can easily amend the example to include them if you like, you could even add some formatting to them (see below).
We run journalctl with continual output, capture that output, tidy it up and present it in reverse order so that the latest information is at the top of the output page which is a lot easier to follow. We also restrict the output to 200 lines to prevent memory issues. Change that if you like.
We have to accumulate the output into a context variable since I haven't been able to find a way to append output to the dashboard.
Finally we pass the accumulated array of log entries to a Dashboard template node and use some Angular cleverness to display it.
As mentioned above, it would be possible to amend this example to add formatting in the function node rather than the template.
You might want to do that if you wanted different types of lines, or even different parts of a line formatted in different ways - with colour for example.
To do this, amend the function node and add HTML formatting to each line as appropriate. Each line should be wrapped in a <div>
and you might want to move the current manual formatting (see the ng-style
directive in the template node) to a separate <style>
section to reduce the output.
Then amend the template, remove the inner <div>
wrapper but don't forget to adjust the {{line}}
to be {{{line}}}
to make Angular realise that the content is HTML not text. If triple-brackets don't work, you will need instead to use an empty HTML element with the Angular directive ng-bind-html="line"
.
Although this example uses journalctl
to show the log, it wouldn't be difficult to amend for other log types. The easiest way would be to write the log to a file and use tail
in the exec node instead of journalctl
.
While this works, it isn't perhaps the most efficient way since it resends all the lines of output every time there is an update. It would probably be more efficient to use a template that created a websocket that you push the data to and let the browser accumulate the data. However, there doesn't appear to be an easy way to do that with Dashboard 2.0 at present.