Logging an user's timestamp

Hi,

I'd like to log into my DB the user whenever he/she logs in. How do I go by? In other words, how can I capture the login of a user?

I tried the "is-logged-in" node but I'm not sure it's what I need.

The users are listed in the red-node settings and they're allowed only to see the UI.
TIA

Really, without knowing how your users log in, we can't really help very much.

Well, I guess I was not too clear but I did mention the users are listed in the red-node settings file. Therefore, the users must login through the standard UI login page.

If you mean logging into the Node-RED editor, you could use the audit log events for that.

The first thing would be to set audit to true on the logging section of the settings file and restart Node-RED

You should then see the audit events in the regular log.

If that gives you the info you need, you could create a custom logger that just collected the audit events and write them wherever you want. Don't have a good link for how to write a custom logger, but can share something tomorrow.

Thanks, the user has read permissions, will what you mentioned capture it too?

If they login, there wil be an audit event.

Thanks, I'm getting close to what I'm looking for. In /var/log/syslog I see the following info:

10 Oct 08:51:04 - [audit] {"event":"comms.open","level":98,"timestamp":1633848664512}
10 Oct 08:51:04 - [audit] {"event":"comms.auth","user":{"username":"admin","permissions":"*"},"level":98,"timestamp":1633848664540}

I suppose I can analyze the file with a Python process (at the end of the day) and save the info (username/timestamp) into my DB. Unless you suggest to do it using a flow.

I suggested you create a custom logger that captures just the audit events.

Docs for that are here: Logging : Node-RED

So, if I understood correctly the procedure, I can create my own logger with a function which sends to my DB the username and its timestamp when the user logs in?

Yes, that is what I've suggested.

Thank you! Now the challenge stands in writing a function in javascript within mylogger file to update my DB :upside_down_face:

Here is a very simple example of a log to file custom handler:

        /** Custom logging */
        tilog: {
            level: 'trace',
            metrics: false,
            audit: false,
            handler: function(settings) {
                // Called when the logger is initialised
                const fs = require('fs')
                const util = require('util')
                // Use flags 'a' to append or 'w' to create new on restart
                const log_file = fs.createWriteStream(__dirname + '/uibuilder.log', {flags : 'w'})
                
                const tilogLevels = {
                    10: 'fatal', 20: 'error', 30: 'warn', 40: 'info', 50: 'debug', 60: 'trace'
                }

                const tilog = function(d) { //
                    log_file.write(util.format.apply(null, arguments) + '\n')
                    //log_stdout.write(util.format(d) + '\n');
                }

                /** Return the logging function
                 * msg schema: { id, level, type, msg, timestamp}
                 * id = The node instance that produces the msg
                 * level = 10: fatal, 20: error, 30: warn, 40: Info, 50: Debug, 60: Trace
                 * timestamp = Javascript timestamp. Use `(new Date(msg.timestamp)).toIsoString()` or similar to convert
                 * type = 'flow' - not clear when/why this is produced
                 */
                return function(msg) {
                    if ( msg.msg.includes('[uibuilder') ) {
                        //console.log('TOLOG: ', msg)
                        tilog(
                            `${tilogLevels[msg.level]}:`, msg.msg
                        )
                    }
                    // else {
                    //     tilog(
                    //         `?? ${tilogLevels[msg.level]}:`, msg.msg
                    //     )                        
                    // }
                }
            }
        },

It just picks out log lines containing the string [uibuilder and sends them to a file includes all logs up to and including trace.

It would be really nice if we could pass or set a tag into the node-red logger which could be used in the custom logger functions!

Alternatively, if we could use custom log levels.

Thanks, will look into that. In the mid-time, I trying to undestand why the user 'demo' is not logged in syslog, it only shows 'admin' even though both users have credentials in 'settings.js' :thinking:

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.