I have a JAVA EE application that uses logback (intended as a successor to the popular log4j project)
Here the logback.xml file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- trace, debug, info, warn, error, fatal -->
<timestamp key="myTimestamp" datePattern="yyyy-MM-dd'_'HH-mm-ss.SSS"/>
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
<!-- To enable JMX Management -->
<jmxConfigurator/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{"yyyy-MM-dd HH:mm"} [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>telefonica.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>telefonica.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>3</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>5MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{"yyyy-MM-dd HH:mm"} [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.telefonicaā€¯ level="debug" />
<logger name="org.springframework" level="debug" />
<logger name="org.springframework.web" level="debug" />
<logger name="org.springframework.security" level="debug" />
<root level="debug">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>
In 1 of the class inside the package com.telefonica I have this code
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public static final Logger log = LoggerFactory.getLogger(TS.class);
log.debug ("logging to WS " + WS_VERSION);
and I don't see anything in the console, but I see it when I put log.info ("logging to WS " + WS_VERSION);
Try adding appender to your logger like below
<logger name="org.springframework" additivity="false">
<level value="DEBUG" />
<appender-ref ref="STDOUT" />
</logger>
PLease refer this question and solution
Spring : Logging not working with log4j or logback
Change your logger level to level="DEBUG" from INFO
<logger name="org.springframework" level="DEBUG"/>
Related
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>
I am using Logback in my spring boot application.
The problem is logback do not print my logger messages in 'eclipse' console for my both 2 packages dao and web.
The log file is written without any problem , and print my logger messages.
I am the root so probably I should see my logger messages in my console.
logger.info("Page X INFO");
logger.debug(" Page X Debug ");
Here is my logback.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 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} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<!-- Send debug messages to a file at "C:/logs/Log.log" -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>C:/logs/Log.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>C:/logs/Log.%i.log.zip</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>10</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<logger name="package.web" level="INFO" >
<appender-ref ref="FILE" />
</logger>
<logger name="package.dao" level="DEBUG" >
<appender-ref ref="FILE" />
</logger>
<!-- By default, the level of the root level is set to DEBUG -->
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
<logger name="package.web" level="INFO" >
<appender-ref ref="FILE" />
</logger>
You need to add the console appender.
<logger name="package.web" level="INFO" >
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</logger>
UPDATE: I just reread the logback config doku. Actually, those two should inherit both appenders from root. So, what you can try is to not specify any appender-ref on those and see what happens. If no output is written to the file then, neither - then there is something pretty strange. I'd expect that behavior if the additivity flag was set to false. But the default is appender accumulation.
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 ??
I want the console logging to turn off and file logging to turn on. Is this possible in logback?
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 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 -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>OFF</level>
</filter>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
</pattern>
</encoder>
</appender>
<!-- Send debug messages to a file at "c:/jcg.log" -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/home/dev-01/Documents/jcg.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>/home/dev-01/Documents/jcg-%i.log</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>10</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>100MB</MaxFileSize>
</triggeringPolicy>
</appender>
<!-- By default, the level of the root level is set to DEBUG -->
<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
I am open to use another logging libraries if this is not possible in logback.
Delete the appender-ref STDOUT reference
<root level="INFO">
<appender-ref ref="FILE" />
</root>
I don't know logback but you might set logging level to OFF for the console logger.
Like this (removing STDOUT appender from root logger) :
<logger name="consoleLogger" level="OFF">
<appender-ref ref="STDOUT" />
</logger>
<root level="INFO">
<appender-ref ref="FILE" />
</root>
See : Can I disable an appender in logback?
EDIT : added XML sample
I configured Log4J to send me emails when an exception occurs, but when it does, instead of sending me one email, I get A LOT of emails (like 10) for the same exception.
I don't know what's wrong.
Here's my configuration :
<configuration>
<conversionRule conversionWord="coloredLevel" converterClass="play.api.Logger$ColoredLevel" />
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${application.home}/logs/application.log</file>
<encoder>
<pattern>%date - [%level] - from %logger in %thread %n%message%n%xException%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%coloredLevel %logger{15} - %message%n%xException{5}</pattern>
</encoder>
</appender>
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>smtp.mandrillapp.com</smtpHost>
<smtpPort>587</smtpPort>
<SSL>true</SSL>
<username>my#email.com</username>
<password>******</password>
<to>me#account.com</to>
<from>support#app.com</from>
<subject>[App error] - %ex{short}</subject>
<cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
<bufferSize>5</bufferSize>
</cyclicBufferTracker>
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%ex{full}</pattern>
</layout>
<!--<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d%n%throwable{short}%n%n%xEx{full}</pattern>
</layout>-->
</appender>
<logger name="play" level="INFO" />
<logger name="application" level="INFO"/>
<root level="ERROR">
<appender-ref ref="STDOUT" />
<appender-ref ref="EMAIL" />
</root>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>