how to cofigure slf4j in spring-mvc for logging? - java

I want to use SLF4j in Spring mvc-hibernate project. i have made
logback.xml for logging
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="DEV_HOME" value="c:/logs" />
<appender name="FILE-AUDIT"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/debug.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="com.mkyong.web" level="debug"
additivity="false">
<appender-ref ref="FILE-AUDIT" />
</logger>
<root level="error">
<appender-ref ref="FILE-AUDIT" />
</root>
</configuration>
but i do not know to cofigure logback.xml in spring-mvc.
In which folder in project should i keep my logback.xml?
how do i configure logback.xml in DefaultServlet-servlet.xml

No need to do anything in Spring configuration. Just add the slf4j and logback dependencies (e.g. in pom.xml if you're using maven), keep logback.xml in the classpath (e.g. src/main/resources) and use the SLF4J API in your spring-mvc Controllers.
You can also see more details here:
https://wiki.base22.com/display/btg/How+to+setup+SLF4J+and+LOGBack+in+a+web+app+-+fast

Related

Split log file in Spring Boot Gradle App Batchwise

I am working on Spring Boot Gradle App and I am facing issues while generating the log files.
Currently my app is generating 1 big log file and my spring boot uses below logback-spring.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<springProperty scope="context" name="app_name" source="spring.application.name"/>
<springProperty scope="context" name="environment" source="param_env" defaultValue="default_env"/>
<springProperty scope="context" name="tenant" source="param_tenant" defaultValue="default_tenant"/>
<property name="GENERIC_PATTERN"
value="[%date{ISO8601}] [%p] [%t] [%c{2}] %4L | %m%n"/>
<property name="CONSOLE_LOG_PATTERN" value="$GENERIC_PATTERN"/>
<property name="CHARSET" value="UTF-8"/>
<property name="LOG_DIR"
value="logs/${tenantname:-${tenant}}/${envname:-${environment}}/${param_gslogname:-${app_name}}"/>
<logger name="org.springframework" level="INFO"/>
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.gs" level="INFO"/>
<logger name="com.gs" level="INFO"/>
<logger name="liquibase" level="FINE"/>
<logger name="org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor"
level="INFO"/>
<logger name="org.springframework.security" level="INFO"/>
<logger name="org.hibernate" level="INFO"/>
<!-- Auto configuration report-->
<logger name="org.springframework.boot.autoconfigure" level="DEBUG"/>
<logger name="org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener"
level="TRACE"/>
<springProfile name="local,component-test,component-test-security,scheduler-test">
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>${CHARSET}</charset>
<pattern>${GENERIC_PATTERN}</pattern>
</encoder>
</appender>
<logger name="zuul.web.request.logger" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</springProfile>
<!-- logging configuration for NOT LOCAL profile -->
<springProfile name="dev, test, uat, preprod, prod, perf">
<appender name="FILE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/${param_gslogname:-${app_name}}-${HOSTNAME}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>
${LOG_DIR}/${param_gslogname:-${app_name}}-${HOSTNAME}.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>1GB</maxFileSize>
<totalSizeCap>10GB</totalSizeCap>
<!-- keep 30 days worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<mdc/>
<pattern>
<omitEmptyFields>true</omitEmptyFields>
<pattern>
{
"app_name": "${app_name}",
"HOSTNAME":"${HOSTNAME}",
"environment":"${environment}"
}
</pattern>
</pattern>
<logLevel/>
<timestamp/>
<threadName/>
<logstashMarkers/>
<tags/>
<arguments/>
<version/>
<stackTrace/>
<loggerName/>
<threadName/>
<message/>
</providers>
</encoder>
</appender>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>${CHARSET}</charset>
<pattern>${GENERIC_PATTERN}</pattern>
</encoder>
</appender>
<appender name="CONSOLE_JSON" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<fieldNames>
<timestamp>timestamp</timestamp>
<message>logdata</message>
<version>[ignore]</version>
<levelValue>[ignore]</levelValue>
</fieldNames>
<timeZone>UTC</timeZone>
</encoder>
</appender>
<appender name="API_FILE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/api-${param_gslogname:-${app_name}}-${HOSTNAME}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>
${LOG_DIR}/api-${param_gslogname:-${app_name}}-${HOSTNAME}.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>1GB</maxFileSize>
<totalSizeCap>10GB</totalSizeCap>
<!-- keep 30 days worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<fieldNames>
<timestamp>[ignore]</timestamp>
<message>logdata</message>
<version>[ignore]</version>
<levelValue>[ignore]</levelValue>
</fieldNames>
<timeZone>UTC</timeZone>
</encoder>
</appender>
<appender name="APPLICATION_FILE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/startup-${param_gslogname:-${app_name}}-${HOSTNAME}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>
${LOG_DIR}/startup-${param_gslogname:-${app_name}}-${HOSTNAME}.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>1GB</maxFileSize>
<totalSizeCap>10GB</totalSizeCap>
<!-- keep 30 days worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<fieldNames>
<timestamp>timestamp</timestamp>
<message>logdata</message>
<version>[ignore]</version>
<levelValue>[ignore]</levelValue>
</fieldNames>
<timeZone>UTC</timeZone>
</encoder>
</appender>
<logger name="api.logger" level="INFO" additivity="false">
<appender-ref ref="API_FILE_APPENDER"/>
</logger>
<logger name="application.startup.logger" level="INFO" additivity="false">
<appender-ref ref="APPLICATION_FILE_APPENDER"/>
</logger>
<!-- Added because of version 1.7.30 and we can't upgrade because our server is not supporting latest version-->
<appender name="SENTRY" class="io.sentry.logback.SentryAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<pattern>${GENERIC_PATTERN}</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE_APPENDER"/>
<appender-ref ref="SENTRY"/>
</root>
</springProfile>
</configuration>
My app works in way that whenever new request comes it generates 1 batch id for 1 request and all the processing stuffs or info logs are being written to one log file called gs_log_file.
and likewise when there is another request ie the 2nd request then 2nd batch id is being generated and all the processing stuffs or info logs are being written to the same log file as mentioned in point no. 1 (gs_log_file). and so on for subsequent requests the logs are being written to the same log file (gs_log_file).
So my issue is how can I generate the batchwise log file for each request or the batch id.
e.g
gs_log_file_batchId1 for batch id 1(request1).
gs_log_file_batchId2 for batch id 2(request2).
gs_log_file_batchId3 for batch id 3(request3).
gs_log_file_batchIdn for batch id n(requestn).
my app api is like this with pathVariable batchJobId -> /process-batch-jobs/{batchId}
How can I improve my implementation so that my app will put the log info in particular log file separately according to per request or per batch.
Should I go for multi-threading, again not sure on this part.
Or Should I manually process the batch id and according to that batch id I should generate the files and then log data into that log file
Once I generate the log files like gs_log_file_batchId1 for batch id 1(request1) and gs_log_file_batchId2 for batch id 2(request2), I should be also able to read this log file via UI...so I ll search these log file from UI based on some batch id or request or some customer id, here batchid can be pathvariable or request param or some sort of filter.
Also please let me know how can I externalize the configuration so that I can supply some argument to app so that app will generate the log file smartly based on supplied argument like log file size or the no of lines in log file?
Can anyone please help me on this?

How to enable Tomcat Logs in Spring Boot

When we use external tomcat to deploy our Spring MVC application, there are certain log files which get printed in the logs folder, i.e., host-manager.log, localhost.log, manager.log, etc.
I needed to enable the generation of these logs in my Spring Boot Application too. How can we do that?
Please check spring docs for logging in file-output: https://docs.spring.io/spring-boot/docs/2.7.0/reference/htmlsingle/#features.logging.file-output
Or alternatively you can use logback-spring.xml to achieve this.
Sample logback-spring.xml which prints logs in the console and file and it daily rollovers the file or if it reaches 10MB:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOGS" value="./logs"/> <!-- the folder where you want your log files to be -->
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/spring-boot-logger.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 10MBs -->
<fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- LOG everything at INFO level -->
<root level="INFO">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
</root>
More about how to config logback: https://logback.qos.ch/documentation.html

Logback logging maven multi-module

I'm developing an application made of multiple modules that will be deployed on wildfly 13. One of these modules is using another of my project as a jar maven dependency.(included to pom)
Expectation
I want my dependency using its own logback.xml to log in its own file. And I want the application using its own logback.xml file to log in the console and a separate file than the dependency.
What it does now
For the moment both the application module (com.test.app.console.ca.operation)
which include the jar dependency and the dependency use the dependency's logback.xml and everything is logged in the same file. This is very strange to me because of other modules from the main application (which dont have dependecy from the library jar), are correctly logging in the right file.
Could please help me to understand and solve this problem?
Details about the projects
Both use logback as a logger. The dependency is a security implementation that logs communication information in a file that must be in a separate file than application logs. Both the application and the dependency have a classic maven structure with the logback.xml file inside the resource folder.
The main modules logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<property resource="application.properties" />
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder
by default -->
<encoder>
<pattern>%d{ISO8601} [%thread] %-5level %logger - %msg %n</pattern>
</encoder>
</appender>
<!--Application Log (Daily rolling file appender) -->
<appender name="appCaLog"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${jboss.server.log.dir}/main_modules_app.log</File>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${jboss.server.log.dir}/main_modules_app.log.%d{yyyy-MM-dd}.log
</FileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{ISO8601} [%thread] %-5level %logger - %msg %n</pattern>
</encoder>
</appender>
<logger name="com.test.app.console.ca.usermanagement"
level="${cops.usermanagement.log.level}" additivity="false">
<appender-ref ref="appCaLog" />
</logger>
<logger name="com.test.app.console.ca.operation"
level="${cops.operation.log.level}" additivity="false">
<appender-ref ref="appCaLog" />
</logger>
<logger name="com.test.app.console.ca"
level="${cops.main.log.level}" additivity="false">
<appender-ref ref="appCaLog" />
</logger>
<root level="INFO">
<appender-ref ref="appCaLog" />
</root>
</configuration>
The dependency logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder
by default -->
<encoder>
<pattern>%d{ISO8601} [%thread] %-5level %logger - %msg %n</pattern>
</encoder>
</appender>
<!--Application Log (Daily rolling file appender) -->
<appender name="dependencyCaCryptoLog"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${jboss.server.log.dir}/dependency_module.log</File>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${jboss.server.log.dir}/dependency_module.log.%d{yyyy-MM-dd}.log
</FileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{ISO8601} [%thread] %-5level %logger - %msg %n</pattern>
</encoder>
</appender>
<logger name="com.test.app.console.ca.crypto"
level="INFO" additivity="false">
<appender-ref ref="dependencyCaCryptoLog" />
</logger>
<root level="INFO">
<appender-ref ref="dependencyCaCryptoLog" />
</root>
</configuration>

Changing logback.xml file in Jenkins without rebuilding code

I have configured logback.xml to change log level during build time using "scan" property. Using this I can change log level without rebuilding the code. I can change log level by updating logback.xml file
<?xml version="1.0" encoding="UTF-8"?><!--
For more configuration information and examples see
http://logback.qos.ch/manual/configuration.html-->
<configuration scan="true" scanPeriod="10 seconds">
<!--<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />-->
<!-- Debugging appender (duplicates the normal log, PLUS any debug messages) -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{ISO8601} %level{5} %c{3} --- %message%n</pattern>
</encoder>
</appender>
<!---->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>./log/elk-file.log</file>
<append>true</append>
<!--cleanHistoryOnStart>true</cleanHistoryOnStart-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>./log/elk-file_%d{yyyyMMdd}-%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>200MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>1</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date{ISO8601} [%thread] %level{5} %c{3} - %message%n</pattern>
</encoder>
</appender>
<!-- Our logger writes to file, console and sends the data to Logstash -->
<logger name="ro.fortsoft.elk.testdata" level="DEBUG" additivity="false">
<appender-ref ref="STASH"/>
</logger>
<logger level="INFO" name="rollingFileLogger">
<appender-ref ref="FILE" />
</logger>
<!-- ROOT logger setup -->
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
</root>
How can I do this in Jenkins, i-e change logback.xml and change log level without rebuilding the code.

Log file not being created in linux/EC2 instance

Hi I have created logback.xml the log files are not created when I am deploying the app in the ec2 server.
I tested in windows/eclipse and it creates the log file in windows path. But it is not created in linux/EC2 machine.
Below is my logback.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="DEV_HOME" value="/home/ec2-user/logs" />
<appender name="FILE-AUDIT" class="ch.qos.logback.core.FileAppender">
<!-- class="ch.qos.logback.core.rolling.RollingFileAppender"> -->
<file>${DEV_HOME}/debug.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="com.technomedha.subscribe" level="DEBUG"
additivity="false">
<appender-ref ref="FILE-AUDIT" />
</logger>
<root level="DEBUG">
<appender-ref ref="FILE-AUDIT" />
</root>
</configuration>
Please help me in this.
I changed the path of value="/home/ec2-user/logs" to value = "/tmp/logs" since the /tmp folder has rwx permissions in the linux box.
After changing the path it worked for me perfectly and the logs are stored in the folder.

Categories

Resources