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.
Related
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>
I have my current logback file, which to my understanding is a classic logback implementation:
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>Hello %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
I have a specific log:
log.info(String.format("ProxyICA %s has an integrity violation. %s.",
processorId.getProxyIca(),
fkViolationMsg)
);
that I am trying to get in a text file. The only configuration I need in my logback is to put every instance of this log into a text file. Every other log can default log to the console (which it was doing without the logback.xml).
Thank you in advanced!
Here is an example of setting it up log to a specific file "Custom-Errors.txt", and to have the logs be deleted if they reach a certain size 20MB or 182 days elapse
<appender name="Custom_Logger
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>Custom-Errors.txt</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>Custom-Errors-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
<!-- each file should be at most 20MB, keep 182 days worth of history,but
at most 500MB -->
<maxFileSize>20MB</maxFileSize>
<maxHistory>182</maxHistory>
<totalSizeCap>500MB</totalSizeCap>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO, DEBUG, TRACE</level>
</filter>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%date %level [%thread] [%file:%line] %msg%n%throwable
</Pattern>
</layout>
</appender>
<logger name="CUSTOM_LOGGER" additivity="false">
<appender-ref ref="Custom_Logger" />
</logger>
Then in your java class make a private variable to use the logger like this
private static final Logger CUSTOM_LOGGER =
LoggerFactory.getLogger("CUSTOM_LOGGER");
logging a dependency, or use to log anything you want based on package
here is an example of logging a library
<appender name="ConsoleOutForSpring" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<logger name="org.springframework" additivity="false">
<appender-ref ref="ConsoleOutForSpring" />
</logger>
I do admit there are a lot of tags and settings to this logger that are kind of complicated, but reading docs and looking at others examples has helped me.
Here were some docs I noted in my logback.xml
<!-- http://logback.qos.ch/manual/configuration.html this helped me with this weird looking stuff i.e. ===> %date %level [%thread] [%file:%line] %msg%n%throwable -->
<!-- http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
helped me set this up:
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>Errors-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>20MB</maxFileSize>
<maxHistory>182</maxHistory>
<totalSizeCap>500MB</totalSizeCap>
</rollingPolicy>
I am using below logback.xml in my Micronaut project, it is not generating new log file as per rollingPolicy provided in xml configuration. I tried with SizeAndTimeBasedRollingPolicy and TimeBasedRollingPolicy but it did't worked.
Micronaut version: 1.2.2
logback-classic : 1.2.3
File: logback.xml
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>logs/my-app.log</file>
<encoder>
<pattern>%cyan(%d{yyyy-MM-dd HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}):%line- %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.RollingFileAppender">
<fileNamePattern>
logs/my-app.log-%d{yyyy-MM-dd}-%i.log.gz
</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>1MB</totalSizeCap>
</rollingPolicy>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%cyan(%d{yyyy-MM-dd HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}):%line- %msg%n</pattern>
</encoder>
</appender>
<root level="ALL">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
If im not wrong, your maxHistory policy is preventing you from building more than one file. Instead it deletes the old one and just creates a new one for the new day or when the 1mb in size is reached.
maxHistory in this case has to be combined with the TimeBasedRollingPolicy as you stated and maxHistory is the parameter for days then. Without the TimeBasedRollingPolicy it might just be the number of files.
Based on my own experience with logback, you should do something like:
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
...
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>logs/my-app.log-%d{yyyy-MM-dd}-%i.log.gz</fileNamePattern>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>1MB</MaxFileSize>
</triggeringPolicy>
...
</appender>
Currently, you are using "RollingFileAppender" for policy, so it can't work.
I need a little help, I'm developing a bot in my java telegram and I thought to insert slf4j as a loggin system, so you can manage the log through a server-side xml file, I wanted to print the log on the file, only since that moment it seems that the loggin system is not managed anymore by the xml file also because I only print the ERROR level logs even if I inject the debug level.
Another note that I think is relevant and that on the server I get a SEVERE type error and I do not type you ERROR so from here I have deduced that the logger is managed differently, how can I solve it?
I'll post my logback.xml file
<configuration>
<timestamp key="byDay" datePattern="yyyyMMdd'T'HHmmss"/>
<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="FILE" class="ch.qos.logback.core.FileAppender">
<file> log-${byDay}.txt </file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
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.