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>
Related
I am using Logback for logging. Now all logs are writing into one file, and it becomes very large. How to organize logging in such a way that a new file is created at the beginning of a new day?
I use such logback.xml
<configuration>
<property name="LOG_PATTERN" value="%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n" />
<property name="APP_LOG_ROOT" value="admin-logs"/>
<timestamp key="year" datePattern="yyyy" />
<timestamp key="month" datePattern="MM" />
<timestamp key="date" datePattern="dd" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-5relative %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<logger level="INFO" name="my.name">
<appender-ref ref="STDOUT" />
</logger>
<root level="ERROR">
<appender-ref ref="STDOUT" />
</root>
</configuration>
all logs are writing into one file
The shown config is going to the STDOUT, not a file.
If you want to write to a daily file, use logrotate/cron process, or have logback do this itself.
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_DIR}/bim.%d{yyyy-MM-dd}.log</fileNamePattern>
...
</appender>
...
<logger level="INFO" name="my.name">
<appender-ref ref="file" />
</logger>
<root level="ERROR">
<appender-ref ref="file" />
</root>
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 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"/>
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 ??