In my spring boot application I wanna add logging, for that I tried to use log4j2
This my log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="LOG_PATTERN">
%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex
</Property>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT"
follow="true">
<PatternLayout pattern="${LOG_PATTERN}" />
</Console>
<!-- Rolling File Appender -->
<RollingFile name="FileAppender"
fileName="logs/log4j2-demo.log"
filePattern="logs/log4j2-demo-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="10" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="tn.manual.controller" level="debug"
additivity="false">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="FileAppender"/>
</Logger>
<Root level="info">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="FileAppender"/>
</Root>
</Loggers>
In the console, I can see the log messages.
But NO file is created, the logs folder should I create it before?
Really, I'm disturbed, I should finish this task today.
I need your help please
you can use like this:
<?xml version="1.0" encoding="UTF-8"?>
<!-- This the default configuration path of logback if you want to modify
the location please provide its entry in application.properties file as value
of logging.config key -->
<configuration scan="false">
<property name="logPattern"
value="%-4relative [%thread] %-5level %logger{35} - %msg%n" />
<property name="logEncoding" value="UTF-8" />
<property name="logDirectory" value="logs" />
<property name="file_base_name" value="Your service name" />
<property name="archive" value="archive" />
<timestamp key="current_date" datePattern="yyyy-MM-dd" />
<!-- Shut down hook registered to close logging factory Elegantly . -->
<shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook" />
<appender name="fileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logDirectory}/${file_base_name}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${logDirectory}/${archive}/${file_base_name}_%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>30MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<charset>${logEncoding}</charset>
<pattern>${logPattern}</pattern>
</encoder>
</appender>
<appender name="ANALYTICS-FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logDirectory}/${file_base_name}_error</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${logDirectory}/${archive}/${file_base_name}_error_%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>30MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<charset>${logEncoding}</charset>
<pattern>${logPattern}</pattern>
</encoder>
</appender>
<!--<logger name="analytics" level="INFO" additivity="false">-->
<!--<appender-ref ref="ANALYTICS-FILE"/>-->
<!--</logger>-->
<!-- <appender name="ASYNC500" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="fileAppender"/> <queueSize>5000</queueSize> <discardingThreshold>0</discardingThreshold>
<appender-ref ref="FILE" /> </appender> -->
<!-- Only use this appender when you are developing your application but
during production it is advisable to disable logging to console to reduce
latency in logging as it costs more I/O time. -->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} %highlight(%-5level)
%logger{36}.%M - %msg%n</pattern>
</encoder>
</appender>
<!-- Configuration for loggers ,Loggers who do not have logging level defined
will inherit levels from root
-->
<root level="error">
<appender-ref ref="ANALYTICS-FILE"/>
</root>
<root level="info" additivity="false">
<appender-ref ref="ANALYTICS-FILE"/>
</root>
<!-- -->
<root level="WARN" additivity="false">
<appender-ref ref="fileAppender" />
<appender-ref ref="stdout" />
</root>
<logger name="Your package name" level="DEBUG" additivity="false">
<appender-ref ref="fileAppender"/>
<appender-ref ref="stdout" />
</logger>
<logger name="org.springframework.web" level="WARN" additivity="false">
<appender-ref ref="fileAppender"/>
<appender-ref ref="stdout" />
</logger>
<logger name="org.apache.commons.beanutils.converters" level="WARN" additivity="false">
<appender-ref ref="fileAppender"/>
<appender-ref ref="stdout" />
</logger>
</configuration>
I hope this will help. If it will help, you can accept it as your accepted answer.
Thanks,