Logback add Error appender made the others appenders not work - java

Here is my logback.xml;
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOGS" value="./logs"/>
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{ISO8601} %highlight(%-5level) [%blue(%t)] %yellow(%C{1.} [%M#%L]) %X{serialID}: %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/info-file.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %highlight(%-5level) [%blue(%t)] %yellow(%C{1.} [%M#%L]) : %msg%n%throwable</Pattern>
</encoder>
</appender>
<appender name="DexLogFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/dex.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %highlight(%-5level) [%blue(%t)] %yellow(%C{1.} [%M#%L]) : %msg%n%throwable</Pattern>
</encoder>
</appender>
<appender name="ErrorLogFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/error.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %highlight(%-5level) [%blue(%t)] %yellow(%C{1.} [%M#%L]) : %msg%n%throwable</Pattern>
</encoder>
</appender>
<!-- LOG everything at INFO level -->
<root level="info">
<appender-ref ref="Console"/>
<appender-ref ref="RollingFile"/>
</root>
<logger name="com.ssca" level="info" additivity="false">
<appender-ref ref="DexLogFile"/>
</logger>
<logger name="ur.company" level="trace" additivity="false">
<appender-ref ref="Console"/>
</logger>
<logger name="ur.company" level="info" additivity="false">
<appender-ref ref="RollingFile"/>
</logger>
<logger name="ur.company" level="error" additivity="false">
<appender-ref ref="ErrorLogFile"/>
</logger>
</configuration>
I just wanna a clearly error log file, so I tried to redirect error to the error.log, so I added those lines:
<logger name="ur.company" level="error" additivity="false">
<appender-ref ref="ErrorLogFile"/>
</logger>
Without this code, everything was fine. However once I added those lines, the RollingFile appender and Console appender did not work any more...
It's like log level problem, but I tried to remove additivity, not work either.
I've read the logback website, but didn't find any helpfuls.
I used lombok #slf4j annotation, Does that cause this conflicts?

well, I just add filter into the error appender, and put the appender-ref from error level logger to info level logger. Code is like below:
<appender name="ErrorLogFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<file>${LOGS}/error.log</file>
........
</appender>
AND
<logger name="ur.company" level="INFO" additivity="false">
<appender-ref ref="RollingFile"/>
<appender-ref ref="ErrorLogFile"/>
</logger>

Related

Custom log based on Marker

Anyone knows if it is possible to create log files based on custom Markers using slf4j?
My Log with my custom Marker:
Marker myCustomMarker = MarkerFactory.getMarker("MY_CUSTOM_MARKER");
logger.error(myCustomMarker , "My application log.");
My logback.xml with logs/%marker.log
<configuration>
<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>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>logs/%marker.log</file>
<append>false</append>
<immediateFlush>true</immediateFlush>
<encoder>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</encoder>
</appender>
<logger name="com.mkyong" level="debug" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>
<root level="error">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE" />
</root>
<root level="info">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE" />
</root>
</configuration>
but the result was /logs.log%marker
Thanks for any help

Logback unable to log in debug

I have a bit problem with a logback configuration, all of my --> log.debug("one log")
of my application aren't written is my debug.log file ... do you know why ?
bellow my logback configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<property name="LOG_PATH" value="/home/log/company"/>
<appender name="trafic" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/trafic.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}]|%-5level|%thread|%logger.%method%cyan(\(%line\)): --- %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<maxHistory>30</maxHistory>
<totalSizeCap>50MB</totalSizeCap>
<cleanHistoryOnStart>false</cleanHistoryOnStart>
<fileNamePattern>${LOG_PATH}/trafic/year=%d{yyyy, aux}/month=%d{MM, aux}/day=%d{dd,
aux}/trafic.%d{yyyy-MM-dd}.log
</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/debug.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}]|%-5level|%thread|%logger.%method%cyan(\(%line\)): --- %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<maxHistory>30</maxHistory>
<totalSizeCap>50MB</totalSizeCap>
<cleanHistoryOnStart>false</cleanHistoryOnStart>
<fileNamePattern>${LOG_PATH}/debug/year=%d{yyyy, aux}/month=%d{MM, aux}/day=%d{dd,
aux}/debug.%d{yyyy-MM-dd}.log
</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/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.SSS}]|%-5level|%thread|%logger.%method%cyan(\(%line\)): --- %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<maxHistory>30</maxHistory>
<totalSizeCap>50MB</totalSizeCap>
<cleanHistoryOnStart>false</cleanHistoryOnStart>
<fileNamePattern>${LOG_PATH}/error/year=%d{yyyy, aux}/month=%d{MM, aux}/day=%d{dd,
aux}/error.%d{yyyy-MM-dd}.log
</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="access" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/access.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}]|%-5level|%thread|%logger.%method%cyan(\(%line\)): --- %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<maxHistory>30</maxHistory>
<totalSizeCap>50MB</totalSizeCap>
<cleanHistoryOnStart>false</cleanHistoryOnStart>
<fileNamePattern>${LOG_PATH}/access/year=%d{yyyy, aux}/month=%d{MM, aux}/day=%d{dd,
aux}/access.%d{yyyy-MM-dd}.log
</fileNamePattern>
</rollingPolicy>
</appender>
<logger name="fr.company" level="ERROR" additivity="false">
<appender-ref ref="error"/>
</logger>
<logger name="fr.company" level="WARN" additivity="false">
<appender-ref ref="error"/>
</logger>
<logger name="fr.company" level="DEBUG" additivity="false">
<appender-ref ref="debug"/>
</logger>
<logger name="fr.company" level="INFO" additivity="false">
<appender-ref ref="trafic"/>
</logger>
<logger name="org.apache.activemq" level="WARN" additivity="false">
<appender-ref ref="error"/>
</logger>
<logger name="org.springframework" level="INFO" additivity="false">
<appender-ref ref="access"/>
<appender-ref ref="trafic"/>
</logger>
<logger name="springfox.documentation" level="WARN" additivity="false">
<appender-ref ref="error"/>
</logger>
<logger name="org.hibernate" level="WARN" additivity="false">
<appender-ref ref="error"/>
</logger>
<logger name="com.zaxxer" level="WARN" additivity="false">
<appender-ref ref="error"/>
</logger>
<root>
<appender-ref ref="trafic"/>
<appender-ref ref="access"/>
<appender-ref ref="debug"/>
<appender-ref ref="error"/>
</root>
And my dependencies:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
And the logger declaration/utilization:
#Slf4j
#Service
public class EventProcessor {
public EventProcessor() {
log.info("Event initialized with '{}' uuid received", getEventUuid());
log.debug("Event initialized with '{}' uuid received", getEventUuid());
}
Do you know why, the INFO lines are correctly writted in debug.log and not DEBUG lines ? ...
thank you very much
<logger name="fr.company" level="ERROR" additivity="false">
<appender-ref ref="error"/>
</logger>
<logger name="fr.company" level="WARN" additivity="false">
<appender-ref ref="error"/>
</logger>
<logger name="fr.company" level="DEBUG" additivity="false">
<appender-ref ref="debug"/>
</logger>
<logger name="fr.company" level="INFO" additivity="false">
<appender-ref ref="trafic"/>
</logger>
You are configuring the same logger four times.
Only the last configuration will be effective (which sets the log level to INFO).
What you seem to want is have the logger at DEBUG, add all appenders to it, and then filter again in the appenders to control the output (which you are already doing).
<logger name="fr.company" level="DEBUG" additivity="false">
<appender-ref ref="error"/>
<appender-ref ref="debug"/>
<appender-ref ref="trafic"/>
</logger>

How to specify the path for storing logs in application.properties one level (or same level) above the project directory

I have specified in logback.xml the dependency on the Spring logger:
logging.file.path=logs
logging.file.name=${logging.file.path}/app.log
And now, when writing logs, a logs folder is created in the root folder of the project and for some reason the logging.path_IS_UNDEFINED folder is created too.
I have two questions:
How to make logs written not inside the project folder , but on the same level with it, while writing not an absolute path, but a relative one? That is, if the project is in D:\Projects\MyProject, then the logs should be in D:\Projects\logs
And why are the logs duplicated in logging.path_IS_UNDEFINED?
Here is my logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>%d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %logger{36} - %msg%n</Pattern>
</encoder>
</appender>
<appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logging.path}/${logging.file}.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>2MB</maxFileSize>
</triggeringPolicy>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${logging.path}/${logging.file}-%d{yyyy-MM-dd}-%i.log</fileNamePattern>
<maxFileSize>2MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>10MB</totalSizeCap>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="Console"/>
</root>
<root level="DEBUG">
<appender-ref ref="RollingFile"/>
</root>
<logger name="com.my.project" level="TRACE" additivity="false">
<appender-ref ref="Console"/>
</logger>
<logger name="com.my.project" level="ERROR" additivity="false">
<appender-ref ref="RollingFile"/>
</logger>
<logger name="com.my.project" level="WARN" additivity="false">
<appender-ref ref="RollingFile"/>
</logger>
</configuration>

Could logback has root level lower than other loggers?

Can I have the level of root logger at ERROR and other loggers like "com.test.abc" at DEBUG? Why does this not work? I can do this in log4j.
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{HH:mm:ss} %-5level [%thread] - [%logger{0}]- %msg%n</pattern>
</encoder>
</appender>
<logger name="com.test.abc" level="DEBUG" additivity="false"/>
<root level="ERROR">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
I found the problem. I need to remove this
additivity="false"
The reason why you don't see any debug logging is because your logger tag is empty, those log statements will go "nowhere". And since additivity="false" is won't be included in the root logger either.
You can try this:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{HH:mm:ss} %-5level [%thread] - [%logger{0}]- %msg%n</pattern>
</encoder>
</appender>
<logger name="com.test.abc" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
<root level="ERROR">
<appender-ref ref="STDOUT"/>
</root>
</configuration>

logback is ignoring springProfile tag

I want to logback to print log messages in file when profile is not 'local', if profile is local then I want it to print to console and as well file.
my logback.xml is as following.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- logger name="org.springframework.jdbc.core" level="debug" -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M %line - %msg%n</Pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i</fileNamePattern>
<!-- each file should be at most 10MB, keep 60 days worth of history, but at most 20GB -->
<maxFileSize>10MB</maxFileSize>
<maxHistory>10</maxHistory>
<totalSizeCap>2GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{35}.%M %line - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.mycom.name" level="info" additivity="false">
<appender-ref ref="FILE"/>
<springProfile name="local">
<appender-ref ref="STDOUT"/>
</springProfile>
</logger>
<logger name="com.mycom.name" level="debug" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
<!--
<root level="info">
<springProfile name="local">
<appender-ref ref="STDOUT"/>
</springProfile>
<appender-ref ref="FILE"/>
</root>
<springProfile name="local">
<root level="debug">
<appender-ref ref="STDOUT"/>
</root>
</springProfile>
-->
<root level="error">
<appender-ref ref="FILE" />
</root>
</configuration>
I see that its ignoring the springProfile tag and not printing to console at all.
OR am I missing something ??

Categories