Jmix Timers and Web Application Usage Logging

Introduction

Logging and auditing are important aspects of web application security and monitoring. The former, when it is done well, makes the latter easy. That is, if our web applications feature good logging, it will be easier for us to audit the activity of users and system components. When we can efficiently (without great programming effort and with computational efficiency) audit when a user opened a screen, when they pressed a button, when they created a record, when they updated a record, when they deleted a record, and when they exited a screen, it is straightforward to answer questions about application usage. The dashboard seen in Figure 1 was created based on usage data captured in the background as users interact with a web application.

 

This post is a first look at using the Timer facet of Jmix to facilitate the automated capture of application module usage. It is a short read with documented code demonstrating an uncomplicated way to add auditing to your application today. By the end you will know how to do the following:

  • Add a logging service to your Spring Boot application
  • Capture log events and push them to the database
  • Ensure that the capture time is in the time of the user and not the database or application server.

The Logging Database Tables

We will put our logging events into a relational database. (In this post will will use PostgreSQL. However, you can use in RBDMS you like. Just make sure that you can find JDBC drivers that will work with your Spring Boot web application server.) We have four tables. They are shown in an ERD in the Figure 2.

Figure 2 — Jmix Logging ERD

The SQL script for creating these tables is shown in Listing 1.

 

Listing 1 – SQL script for creating logging tables.

The SQL script in Listing 1 will help you create the tables required for this post. You can rename them to whatever you want but need to remember that the names provided are part of a working solution. Also, the tables were modeled in Jmix’s data model visual designer. (It makes it easier to maintain the design if we leverage the design features of the Jmix plugin.)

The Logging Service in Spring Boot

Logging of application events is handled by a service. In Listing 2 we provide the code for an application event logging service.

Listing 2 – Java Code for an application vent logging service.

The Timer and Capturing Usage Events

Timer is component of Jmix defined in the facets element an application screen. Specifically, it will be in the screen XML descriptor. The configuration of a Timer through the XML is done through three attributes: delay, autostart, and repeating.

Parameter Name Required? Desciption
delay Yes This is the timer interval in milliseconds.
autostart No indicates whether or not the timer will start automatically. The default setting is false; when autostart is set to false the timer will start only when its start() method is invoked. When it is set to true, the timer starts immediately after the screen opening. This parameter is optional.
repeating No Turns on repeated executions of the timer. If the attribute is set to true, the timer runs in cycles at equal intervals defined in the delay attribute. Otherwise, the timer runs only once as many milliseconds as specified in the delay attribute after the timer start. This parameter is optional.

 

 

 

 

 

 

 

Ensure that Captured Time is in the Time of the User

 

Conclusion

 

References