Spring Logback - SizeAndTimeBasedRollingPolicy is not creating log file - java

I am using SizeAndTimeBasedRollingPolicy of logback to create log files based on time and size. But log files are not creating in specified folder.
But when I used FixedWindowRollingPolicy or TimeBasedRollingPolicy, it is working as expected and log files are getting created in specified folder.
Spring boot version 1.2.0.RELEASE used in my application.
Below is my logback.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<property name="LOG_PATH" value="logs"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>utf-8</charset>
<Pattern>%d{yyyy-MMM-dd HH:mm:ss} [%thread] %-5p %c:%L - %m%n</Pattern>
</encoder>
</appender>
<appender name="SAVE-TO-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/log.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MMM-dd HH:mm:ss} [%thread] %-5p %c:%L - %m%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/archived/log_%d{dd-MM-yyyy}_%i.log</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>10</maxHistory>
<totalSizeCap>100MB</totalSizeCap>
</rollingPolicy>
</appender>
<root level="${logback.loglevel}">
<appender-ref ref="CONSOLE" />
<appender-ref ref="SAVE-TO-FILE" />;
</root>
</configuration>
Thanks in advance for any help.

Related

How to write logs at multiple location in logback-spring.xml

Is is possible to create logs at multiple locations using logback-spring.xml ?
I tried doing that but as soon as I give two locations it creates logs at default location .
Below is the mentioned logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<property name="springAppName" value="app-name"/>
<springProperty name="maxLogHistoryInDays" source="log.maxLogHistoryInDays"/>
<springProperty name="logPath" source="log.path"/>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>%d{dd MMM yyyy ;HH:mm:ss.SSS} %highlight(%level) [%thread] %property{HOSTNAME} ${springAppName:-} [%X{X-B3-TraceId}]
%logger{0}.%M\(%line\) - %msg%n
</pattern>
</encoder>
</appender>
<appender name="fileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logPath}/${springAppName}/application.log</file>
<file>application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>application_%d{yyyy-MM-dd}_%i.log</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>${maxLogHistoryInDays}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{dd MMM yyyy ;HH:mm:ss.SSS} %level [%thread] %property{HOSTNAME} ${springAppName:-} [%X{X-B3-TraceId}]
%logger{0}.%M\(%line\) - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="consoleAppender" />
<appender-ref ref="fileAppender" />
</root>
</configuration>
Here the logs are getting created at default location when two paths are given . And when only path is mentioned i.e., the first one it is working fine , logs are getting created at custom location.
Its not possible to provide multiple file paths in same appender block but You can do that by adding multiple appender blocks in your config file like -
<appender name="fileAppender" ............>
<appender name="fileAppender1" ............>
.
.
<root level="INFO">
<appender-ref ref="consoleAppender" />
<appender-ref ref="fileAppender" />
<appender-ref ref="fileAppender1" />
</root>
you can provide different file path in different appender blocks. While doing se make sure there is no collision in file. log file name and rolling file name must be different in both.
<appender name="fileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>./application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>application_%d{yyyy-MM-dd}_%i.log</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>1</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{dd MMM yyyy ;HH:mm:ss.SSS} %level [%thread] %property{HOSTNAME} ${springAppName:-} [%X{X-B3-TraceId}]
%logger{0}.%M\(%line\) - %msg%n</pattern>
</encoder>
</appender>
<appender name="fileAppender1"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>./application1.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>application1_%d{yyyy-MM-dd}_%i.log</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>1</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{dd MMM yyyy ;HH:mm:ss.SSS} %level [%thread] %property{HOSTNAME} ${springAppName:-} [%X{X-B3-TraceId}]
%logger{0}.%M\(%line\) - %msg%n</pattern>
</encoder>
</appender>

How to exclude error log level in a file logback spring boot

I have a spring boot app(spring-boot-starter-parent --> 2.3.0.RELEASE) that I am implementing log.
Basically what I am trying to achieve is that I have 2 log files one bridgeError.log which contains only error messages and bridge.log which will contain all log level except Error level since it is being logged in brideError.log file.
I am able to create 2 logs file brideError.log which contains only error messages but the issue is that
bridge.log also contains error messages.
Please find my logback-spring.xml configuration below:
<configuration>
<property name="HOME_LOG" value="logs/bridge.log"/>
<property name="HOME_LOG_ERROR" value="logs/bridgeError.log"/>
<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{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE-ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME_LOG}</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>logs/archived/bridge.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<!-- each archived file, size max 10MB -->
<maxFileSize>10MB</maxFileSize>
<!-- total size of all archive files, if total size > 20GB, it will delete old archived file -->
<totalSizeCap>20GB</totalSizeCap>
<!-- 60 days to keep -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</encoder>
</appender>
<appender name="FILE-ROLLING-ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME_LOG_ERROR}</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>logs/archived/bridgeError.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<!-- each archived file, size max 10MB -->
<maxFileSize>10MB</maxFileSize>
<!-- total size of all archive files, if total size > 20GB, it will delete old archived file -->
<totalSizeCap>20GB</totalSizeCap>
<!-- 60 days to keep -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</encoder>
</appender>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<logger name="com.dummy.package" level="debug" additivity="false">
<!-- <appender-ref ref="FILE-ROLLING"/>-->
<appender-ref ref="CONSOLE"/>
</logger>
<!-- <root level="error">
<appender-ref ref="FILE-ROLLING"/>
<!– <appender-ref ref="CONSOLE"/>–>
</root>-->
<logger name="com.dummy.package" level="DEBUG">
<appender-ref ref="FILE-ROLLING"/>
<appender-ref ref="FILE-ROLLING-ERROR"/>
</logger>
</configuration>
Is it possible to exclude error messages in bridge.log file?
Thanks in advance
You can use a level filter for that purpose
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>DENY</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
Docs: http://logback.qos.ch/manual/filters.html

Logback-Slf4j writes log messages to file from when runs from IDe but not as a jar

I am using SLF4j and logback to log messages. It works fine and writes to configured file. But when I build a executable jar and try to run from prompt as java -jar executableFile. It neither writes to configured file, also displays tons of debug messages on console.
this my logback.xml:
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{ISO8601} %-5level %logger{36} %X{sourceThread} - %msg%n</pattern>
</encoder>
</appender>-->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/var/log/LogWriter/log-test.log </file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>/var/log/LogWriter/log-test.log.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>10MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%date{ISO8601} %-5level %logger{36} %X{sourceThread} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
The reason is that your main class is not finding logback.xml, so logback uses the default appender - the console in this case. You need to make sure that when packing the jar, the logback.xml is in the correct place.

Logback RollingFileAppender Not Working

I have the following logback.xml file:
<configuration>
<!--Daily rolling file appender -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>/usr/share/tomcat6/logs/api.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>/usr/share/tomcat6/logs/api/api.%d{yyyy-MM-dd}.gz</FileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
My log file is working just fine. The folling file aspect however is not. Instead of gzipping the file and moving it into the api folder, it is putting it in the same directory and renaming it to
api.log(string of numbers).tmp
e.g.
api.log849916939395200.tmp
Does anyone know why this is happening?
Just remove the file tag from appender. Use something like this,
<appender name="contentDeliveryLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${ICEX_HOME}/logs/content-delivery.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 1 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d [%thread] %-5level %logger{36} H:${HOSTNAME} - SC:%X{optionalParam} %msg%n</pattern>
</encoder>
</appender>
This is working for me as recommended by documentation of logback here
I had the similar issue. To fix this issue change the pattern to
/usr/share/tomcat6/logs/api/api.%d{yyyy-MM-dd}.%i.gz.
You missed the %i in the end.

Logback RollingFileAppender not writing to proper file

I'm trying to set up Logback with my Web Application. My logback.xml file looks like below:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds">
<appender name="tests_file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>tests.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>tests.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>100MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="application_file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>application.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>application.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>100MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="tests" level="DEBUG">
<appender-ref ref="tests_file" />
</logger>
<logger name="application" level="INFO">
<appender-ref ref="application_file" />
</logger>
</configuration>
This file is placed in WEB-INF/classes.
I have a servlet with the following constructor:
public class FrontController extends HttpServlet {
private Logger logger;
public FrontController() {
// Initialise the logger
logger = (Logger)LoggerFactory.getLogger("application");
logger.info("Starting application");
}
}
When I start up the servlet within Tomcat, nothing gets written to the application.log file that I've declared in logback.xml. Is the LoggerFactory just not finding the xml file or is my xml file wrong?
Thanks
Well, as it turns on, relative paths are bad. So instead of using simple file name in the file entry, i used the line:
<file>${user.dir}/logs/application.log</file>
Now the servlet logging writes to my eclipse installation folder under ../logs/application.log. All is well.

Categories

Resources