New example: Output Node-Red log to Dashboard

1,241 views
Skip to first unread message

Julian Knight

unread,
Aug 10, 2016, 4:29:33 PM8/10/16
to Node-RED
Hi all, inspired by another conversation in this group, I ended up creating a flow to output the Node-Red runtime log to a web page using the new Dashboard 2.0 UI.

You will need to be running NR under systemd for the example to work as coded but it wouldn't be difficult to amend for other log types.

Maybe someone will find it useful but it was an interesting exercise anyway. Latest output is at the top and the crud that journalctl adds to the output is stripped away though you can easily include it if you like.

Walter Kraembring

unread,
Aug 11, 2016, 3:19:56 AM8/11/16
to Node-RED
Ange koden här...

Good Morning Julian,
Thanks, your flow works fine!
I just had a minor: when sending false I did receive this error message:

TypeError: Object false has no method 'substr'

So I changed one small thing in your code in the template node (I moved a bracket } to include more of the code )

See below

// Use a fn context variable to track
// the output lines
if ( msg.payload === false ) {
    // This lets us reset the log list
    // if we want to, just inject a false payload
    var lines = [];
} else {
    var lines = context.get('jrnlLines')|| [];
    // Add the new output TO THE START
    // so that the latest output is at the top
    // of the displayed page
    
    // BUT we only want to display ACTUAL
    // NR log output not all the other stuff
    // that the journal outputs. Actual NR log
    // output starts with a date (numeric)
    if ( isNaN(parseInt(msg.payload.substr(0,1))) ) {
        // Not numeric at start so not NR output
        // ignore
    } else {
        lines.unshift(msg.payload);
    }
    
    // # lines could get VERY large so cause memory
    // issues, limit to 200
    if ( lines.length > 200 ) {
        // too big so drop the last element
        lines.pop();
    }
    
    // save the total output in the context var
    context.set('jrnlLines',lines);
}

// output all the lines to the UI template
msg.payload = lines;

return msg;




Julian Knight

unread,
Aug 11, 2016, 3:30:43 AM8/11/16
to Node-RED
Oops! Silly me. I don't think I did another reset but then I added more code - you are absolutely right of course and I will fix the example flow. Thanks.

Julian Knight

unread,
Aug 11, 2016, 3:33:46 AM8/11/16
to Node-RED
Oh, one minor correction of your correction! The context.set needs to be outside the main if statement otherwise you cannot actually reset.

Incidentally, for others, the reset is entirely optional, you shouldn't really need it at all since the code self limits the output to 200 lines. I needed it when I changed from natural sort order to reverse so that the latest output is at the top.
Reply all
Reply to author
Forward
0 new messages