I have tried the solution from Logback: how to log only errors to file
but it doesn't work for me:
What can be wrong with my logback.xml file?
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>ALL.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>ALL.%d{yyyy-ww}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<appender name="FILE-ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>ERROR.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>ERROR.%d{yyyy-ww}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
<root level="ERROR">
<appender-ref ref="FILE-ERROR" />
</root>
</configuration>
Then to files and console goes only ERRORs.
Is it because I log the errors with log4j?
I believe this part is what causes the issue:
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
<root level="ERROR">
<appender-ref ref="FILE-ERROR" />
</root>
Here, we first set the level on root logger to INFO, attach two appenders to it, but then reassign the level to be ERROR and attach another appender.
Since you set the filter on FILE-ERROR appender, there’s no need to change the root’s level and you can attach all the appenders at once:
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
<appender-ref ref="FILE-ERROR" />
</root>
Related
Below my logback configuration file. System CentOS7.9 Every time I get error with permission issues however I gave all required
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<property name="LOG_FILE" value="/var/log/file_name/"/>
<springProfile name="local">
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</springProfile>
<springProfile name="dev">
<appender name="ROLLING-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>%d %contextName [%t] %level %logger{36} - %msg%n</pattern>
</encoder>
<file>${LOG_FILE}file_name</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}file_name.%d{yyyy-MM-dd-HH}.gz</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="ROLLING-FILE"/>
</root>
</springProfile>
<springProfile name="prod">
<appender name="ROLLING-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>%d %contextName [%t] %level %logger{36} - %msg%n</pattern>
</encoder>
<file>${LOG_FILE}file_name</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}file_name.%d{yyyy-MM-dd-HH}.gz</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="ROLLING-FILE"/>
</root>
</springProfile>
<logger name="com.amazon.sqs.javamessaging.SQSSession">
<level value="WARN"/>
</logger>
<logger name="com.amazon.sqs.javamessaging.SQSMessageProducer">
<level value="WARN"/>
</logger>
</configuration>
While run it below error appeared
Dec 26 12:11:54 fleet journal: Caused by: java.lang.IllegalStateException: Logback configuration error detected:
Dec 26 12:11:54 fleet journal: ERROR in ch.qos.logback.core.rolling.RollingFileAppender[ROLLING-FILE] - openFile(/var/log/folder/file_name,true) call failed. java.io.FileNotFoundException: /var/log/folder/file_name (Permission denied)
I provided all permission to that folder but still same error
I need to rotate my log file every day by zipping it and creating another one, the problem is that when the rotation of the log ocours, it doesn't create an new empty log file, it zips what happended in the previous day but doesn't create a new file, it keeps all the information. Here is my log4j.xml configuration:
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<jmxConfigurator/>
<appender class="ch.qos.logback.core.ConsoleAppender" name="Console_Appender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</pattern>
</layout>
</appender>
<appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="RollingFile_Appender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>/opt/s4bconrmkissflow/log/console-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<maxFileSize>200MB</maxFileSize>
<totalSizeCap>1GB</totalSizeCap>
<maxHistory>15</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger additivity="false" level="info" name="net.s4bdigital.intg.kissflow">
<appender-ref ref="Console_Appender"/>
<appender-ref ref="RollingFile_Appender"/>
</logger>
<logger additivity="false" level="error" name="org.apache.camel.processor.interceptor.Tracer">
<appender-ref ref="Console_Appender"/>
<appender-ref ref="RollingFile_Appender"/>
</logger>
<root level="error">
<appender-ref ref="Console_Appender"/>
<appender-ref ref="RollingFile_Appender"/>
</root>
</configuration>
By the way, I don't fully understand what the pattern that I'm using does, I got the %d,%t,%m and %n, but the others, no. So if you could explain me, I'd appreciate.
So I'm using Log4j2 to log information from my app to some files, but know i want separate the files by packages. I have one logger and i dont know how to make him filter the logs by package/class/
<Logger name="hextodec.MainClass" additivity="FALSE">
<AppenderRef ref="info" level="INFO"/>
<AppenderRef ref="error" level="ERROR"/>
<AppenderRef ref="stdout" level ="INFO" />
</Logger>
Hey this is just for your info, have an idea and implement for your test case:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="HIBERNATE_LOG_LOCATION" value="../logs/dev/hibernate" />
<property name="HIBERNATE_ARCHIVE_LOG_LOCATION" value="../logs/dev/hibernate/archive" />
<property name="SPRING_LOG_LOCATION" value="../logs/dev/spring" />
<property name="SPRING_ARCHIVE_LOG_LOCATION" value="../logs/dev/spring/archive" />
<appender name="HIBERNATE_INFO"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${BASE_LOCATION}${HIBERNATE_LOG_LOCATION}/RERUN_UTIL_HIB_INFO.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- hourly rollover -->
<fileNamePattern>${BASE_LOCATION}${HIBERNATE_ARCHIVE_LOG_LOCATION}/RERUN_UTIL_HIB_INFO.%d{yyyy-MM-dd_HH}.%i.log.gz
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<!-- <pattern>%date %level [%thread] %logger{10} [%file : %line] %msg%n</pattern> -->
<pattern>%date [%thread] %-5level %class [%M:%L] - %msg%n</pattern>
</encoder>
</appender>
<!-- similar appenders for all others -->
<appender name="SP_INFO"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${BASE_LOCATION}${SPRING_LOG_LOCATION}/SPRING_INFO.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- hourly rollover -->
<fileNamePattern>${BASE_LOCATION}${SPRING_ARCHIVE_LOG_LOCATION}/SPRING_INFO.%d{yyyy-MM-dd_HH}.%i.log.gz
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>%date [%thread] %-5level %class [%M:%L] - %msg%n</pattern>
</encoder>
</appender>
<!-- similar appenders for all others-->
<logger name="org.hibernate" additivity="false">
<appender-ref ref="HIBERNATE_DEBUG" />
<appender-ref ref="HIBERNATE_INFO" />
<appender-ref ref="HIBERNATE_ERROR" />
</logger>
<logger name="org.springframework" additivity="false">
<appender-ref ref="SP_ERROR" />
<appender-ref ref="SP_DEBUG" />
<appender-ref ref="SP_INFO" />
</logger>
<root level="ALL">
<appender-ref ref="INFO" />
<appender-ref ref="DEBUG" />
<appender-ref ref="ERROR" />
</root>
</configuration>
Hope this solves your quiz.
You can do this by creating multiple file appenders.
Examples: https://logging.apache.org/log4j/2.x/manual/configuration.html
Appender Example with
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="File" fileName="/var/log/file.log">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Logger name="com.foo.Bar" level="trace">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="com.foo.acme" level="trace">
<AppenderRef ref="File"/>
</Logger>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Currently my Spring-boot application logs to a file named: myLog.log, this is working as intended, however I would like the log file to have a timestamp at the end of it and create a new file each time it is ran.
I have tried to implement this in my logback-test.xml file shown below, but it is just giving me the filename: myLog.log without a timestamp.
How can I fix this?
Logback-test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.web" level="INFO"/>
<!-- Send debug messages to System.out -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>path/to/my/file/mylog.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>mylog.%i{yyyy-MM-dd_HH:mm:ss.SSS}}.log</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>10</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>2MB</MaxFileSize>
</triggeringPolicy>
</appender>
<logger name="com.my.package" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</logger>
<!-- By default, the level of the root level is set to DEBUG -->
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
You can define a variable like this:
<timestamp key="myTimestamp" datePattern="yyyy-MM-dd'_'HH-mm-ss.SSS"/>
(note: don't use colons in datePattern)
Then use it directly in your appender's file element:
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>path/to/my/file/mylog-${myTimestamp}.log</file>
...
</appender>
Or in a simple FileAppender:
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>path/to/my/file/mylog-${myTimestamp}.log</file>
<encoder>
<Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} - %msg%n</Pattern>
</encoder>
</appender>
I have the following logback XML configuration, however when I load my logger:
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger("com.nordea.icelink.adaptiv");
And do and info log by dooing
LOGGER.info(s);
Nothing gets outputed to the file only on the console? How can this be?
<configuration>
<logger name="org.springframework" level="WARN"/>
<logger name="org.eclipse.jetty" level="WARN"/>
<logger name="com.nordea.icelink.adaptiv" level="info">
<appender-ref ref="FILE"/>
</logger>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>../logs/NordeaIceLinkServer.log</file>
<encoder>
<pattern>%date{YYYY-MM-dd HH:mm} %logger{36} %level %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>../logs/NordeaIceLinkServer.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>3</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>10MB</maxFileSize>
</triggeringPolicy>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%logger{36} %level %msg%n</pattern>
</encoder>
<root level="debug">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
You should not use a relative path for the log file.
<file>../logs/NordeaIceLinkServer.log</file>
Use an absolute one instead.
<file>/path/logs/NordeaIceLinkServer.log</file>
Actually the appender must be created before the logger. then it worked.