Could logback has root level lower than other loggers? - java

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>

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

Ignore logging messages from dependencies [duplicate]

In my application I use Java, Hibernate.
Logging : I use logback.xml
Can anyone suggest if there is a way to disable the logs from the below specific class from Hibernate jar.
LOGGER to be removed from the specific class : ERROR o.h.e.jdbc.spi.SqlExceptionHelper
logback.xml:
<configuration>
<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>
<logger name="org.springframework" level="error"
additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<root level="error">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Add the following to your logback.xml configuration file:
<logger name="org.hibernate.engine.jdbc.spi.SqlExceptionHelper" level="OFF"/>
The instruction: level="OFF" tells Logback to disable all log output for a given logger.
In your logback.xml configuration adding the following element should work
<logger name="org.hibernate.engine.jdbc.spi.SqlExceptionHelper" level="OFF"/>

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>

Logback add Error appender made the others appenders not work

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>

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