I want to get thetimestamp as well date of a users login , if multiple occurences , i would want to list the same as well

I want to get thetimestamp as well date of a users login , if multiple occurences , i would want to list the same as well .
I already have security and audit trail plugins setup , and i am suing this groovy script to extract the same but theres no result related to login time thats coming up .
Any other way i can achieve this ?

import jenkins.security.SecurityListener
import org.acegisecurity.Authentication
import java.nio.file.*
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

// Specify the log file location
def logFile = “/jenkins/data/home/logs/user_login_times.txt”

// Listener to track successful logins
SecurityListener.all().add(new SecurityListener() {
@Override
void authenticated(Authentication authentication) {
String userId = authentication?.name
if (userId) {
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”))
String logMessage = “User '{userId}' logged in at {timestamp}\n”

        // Append login event to the log file
        Files.write(Paths.get(logFile), logMessage.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND)
        println logMessage
    }
}

})

println “Login listener registered. Tracking login times in ‘${logFile}’.”

Welcome back @rajatroy021, :wave:

To track user login times in Jenkins, you could use the SecurityListener class to listen for authentication events.

Here is a Groovy script that could work:

import jenkins.security.SecurityListener
import java.nio.file.*
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

// Specify the log file location
def logFile = "/var/jenkins_home/logs/user_login_times.txt"

// Ensure the directory exists
def logDir = Paths.get(logFile).parent
if (!Files.exists(logDir)) {
    try {
        Files.createDirectories(logDir)
        println "Created directory: ${logDir}"
    } catch (IOException e) {
        println "Failed to create directory: ${e.message}"
    }
}

// Listener to track successful logins
SecurityListener.all().add(new SecurityListener() {
    @Override
    void loggedIn(String userId) {
        println "loggedIn method called for user: ${userId}"
        if (userId) {
            String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
            String logMessage = "User '${userId}' logged in at ${timestamp}\n"

            try {
                // Append login event to the log file
                Files.write(Paths.get(logFile), logMessage.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND)
                println "Logged: ${logMessage}"
            } catch (IOException e) {
                println "Failed to write to log file: ${e.message}"
            }
        }
    }
})

println "Login listener registered. Tracking login times in '${logFile}'."

and the result:
Login listener registered. Tracking login times in '/var/jenkins_home/logs/user_login_times.txt'.

I wrote “could work”, as I can’t see the file on my controller as I have this error: groovy.lang.MissingPropertyException: No such property: LocalDateTime for class: Script1