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>
Related
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.
I am new to coding and stackoverflow, We are using logback for logging our application, as of now we are using {fileName}.%d{yyyy-MM-dd}.log naming convention for the time based rolling policy (rolling take place each and every day) and have set 30 days as the maximum time for a log to be stored in the application. We need to change the naming convention to only date format ({fileName}.%d{dd}.log). But we are getting "The date format in fileNamePattern will result in collisions in the names of archived log files." error
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<appender name="dailyRollingFileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/deliveryService.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>logs/deliveryService.%d{yyyy-MM-dd}.log
</fileNamePattern>
<!-- Delete log after 30 days -->
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<!-- Send logs to both console and file audit -->
<logger name="com.bnsf" level="debug"
additivity="false">
<appender-ref ref="dailyRollingFileAppender" />
<appender-ref ref="STDOUT" />
</logger>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
Because in a month like February, with only 28/29 days, in a 30-day windows you will have deliveryService.01.log and deliveryService.02.log files twice.
I would like to see all info level output from console, and ONLY warn output from file.
Info level output on console is clearly shown, but all Info output is saved on file too, not only warn level output.
Here is my logback configuration. Any wrong in here?
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{20} [%mdc{threadid}] - [%mdc{useroid} : %mdc{sessionid}]%msg%n</pattern>
</encoder>
</appender>
<appender name="consoleFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log/console.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level | %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="console" />
</root>
<root level="WARN">
<appender-ref ref="consoleFileAppender" />
</root>
For your purpose I suggest to use filters in your appender. More specifically, if you want to see only INFO on console you can add a specific LevelFilter in your console appender, if you want to see all logs equals or above to INFO, you can use ThresholdFilter.
Here there is a possible solution for your task:
<appender name="console" 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 class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{20} [%mdc{threadid}] - [%mdc{useroid} : %mdc{sessionid}]%msg%n</pattern>
</encoder>
</appender>
<appender name="FileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<prudent>true</prudent>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log/console.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level | %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="console" />
<appender-ref ref="FileAppender" />
</root>
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.
I have this logback.xml in my app (Java 6 / Tomcat 7.0.27 / JSF: Mojarra 2.1.13) file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="LB_STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{dd/MM/yyyy HH:mm:ss.SSS} %-5level {%thread} [%logger{40}] : %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="LB_AOUB_FILE" class="ch.qos.logback.core.FileAppender">
<file>${catalina.base}/logs/lb_aoub.log</file>
<append>true</append>
<encoder>
<charset>UTF-8</charset>
<pattern>%d{dd/MM/yyyy HH:mm:ss.SSS} %-5level {%thread} [%logger] : %msg%n</pattern>
</encoder>
<rollingPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/lb_aoub-%d{yyyyMMdd}-%i.log.zip</fileNamePattern>
<maxHistory>60<!-- days -->
</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>2MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="br.com.aoub" level="DEBUG">
<appender-ref ref="LB_STDOUT" />
<appender-ref ref="LB_AOUB_FILE" />
</logger>
</configuration>
The errors, warns and infos are working. But the debug level logs in the application have not been written in the console neither been saved in the file.
If I add this:
<root level="debug">
<appender-ref ref="LB_STDOUT" />
</root>
The debug messages are written is the console, but I want to set only the br.com.aoub logger to debug, not the entire application.
Am I missing something?