Logback Configuration - HunYahiko/smart-parking-system GitHub Wiki
For this project, spring-logback was used. The configuration can be found in ..sps-backend\src\main\resources\logback\logback-spring.xml.
First, a base configuration from Spring is included, so that we still get the base logging appended to console. Then we declare a variable which will hold the destination folder for all log files. In case none is found (later about this), there is a default value set as well.
An appender was created for request/response logging. The appender class is RollingFileAppender, which means our requests/responses will be written to a file, and archived when the end of the day is reached or the file reaches a certain size. The log pattern includes a variable username which indicates which user issued the request. This was done using MDC class, which is set when the request is intercepted. The username is taken from the authentication token.
Next configuration is a sift appender which is configured to create a new log file for each controller in the system. The purpose of this is to log all of the actions that happened from the beginning of the request to the response, for each individual user. All logs lines included in services, repositories etc will be included. Having such an appender separates booking process logging from park searching one. Sift appender requires the use of MDC class as well. To simplify the job of setting a key in MDC, some of the AOP functionality was used. A pointcut is created every time a thread calls a method from a class annotated with @RestController. Afterwards two new methods are created, annotated with @Before and respectively, @After. One executes a method before our RestController method, and the other one is called afterwards. In this method, we set the value of a key, the value being the name of the RestController class. This value is later used in sift appender to create a file for each RestController and append the logging correctly. The sift appender encapsulates another RollingFileAppender which does the actual job of writing into files.
Logback also includes a JMX configuration, which allows to change the root level of logging at runtime.