# Logging an user's timestamp

**URL:** https://discourse.nodered.org/t/logging-an-users-timestamp/52042
**Category:** General
**Tags:** security
**Created:** [9 October 2021 15:16 UTC](https://discourse.nodered.org/t/logging-an-users-timestamp/52042 "2021-10-09T15:16:36Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)
#### Post date: [9 October 2021 15:16 UTC](https://discourse.nodered.org/t/logging-an-users-timestamp/52042/1 "2021-10-09T15:16:36Z")

</div>

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

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [9 October 2021 21:31 UTC](https://discourse.nodered.org/t/logging-an-users-timestamp/52042/2 "2021-10-09T21:31:59Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)
#### Post date: [9 October 2021 21:52 UTC](https://discourse.nodered.org/t/logging-an-users-timestamp/52042/3 "2021-10-09T21:52:02Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [9 October 2021 22:02 UTC](https://discourse.nodered.org/t/logging-an-users-timestamp/52042/4 "2021-10-09T22:02:01Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)
#### Post date: [9 October 2021 22:14 UTC](https://discourse.nodered.org/t/logging-an-users-timestamp/52042/5 "2021-10-09T22:14:06Z")

</div>

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

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [9 October 2021 22:26 UTC](https://discourse.nodered.org/t/logging-an-users-timestamp/52042/6 "2021-10-09T22:26:47Z")

</div>

If they login, there wil be an audit event.

---

<div class="post-metadata">

### Author: ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)
#### Post date: [10 October 2021 07:10 UTC](https://discourse.nodered.org/t/logging-an-users-timestamp/52042/7 "2021-10-10T07:10:54Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [10 October 2021 07:41 UTC](https://discourse.nodered.org/t/logging-an-users-timestamp/52042/8 "2021-10-10T07:41:28Z")

</div>

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

Docs for that are here: [Logging : Node-RED](https://nodered.org/docs/user-guide/runtime/logging#custom-logging-module)

---

<div class="post-metadata">

### Author: ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)
#### Post date: [10 October 2021 09:32 UTC](https://discourse.nodered.org/t/logging-an-users-timestamp/52042/9 "2021-10-10T09:32:16Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [10 October 2021 09:46 UTC](https://discourse.nodered.org/t/logging-an-users-timestamp/52042/10 "2021-10-10T09:46:53Z")

</div>

Yes, that is what I've suggested.

---

<div class="post-metadata">

### Author: ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)
#### Post date: [10 October 2021 10:27 UTC](https://discourse.nodered.org/t/logging-an-users-timestamp/52042/11 "2021-10-10T10:27:28Z")

</div>

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

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [10 October 2021 10:27 UTC](https://discourse.nodered.org/t/logging-an-users-timestamp/52042/12 "2021-10-10T10:27:32Z")

</div>

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

```auto
        /** 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.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [10 October 2021 10:32 UTC](https://discourse.nodered.org/t/logging-an-users-timestamp/52042/13 "2021-10-10T10:32:48Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)
#### Post date: [10 October 2021 12:49 UTC](https://discourse.nodered.org/t/logging-an-users-timestamp/52042/14 "2021-10-10T12:49:37Z")

</div>

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' 🤔

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [9 December 2021 12:49 UTC](https://discourse.nodered.org/t/logging-an-users-timestamp/52042/15 "2021-12-09T12:49:56Z")

</div>

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