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 ??
Related
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
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>
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>
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 create app using JHipster. Is there any simple way to configure application to create log files for each separate day ? I tried everything in one file but its getting to big and its not good practice. I did that by adding this part on the end of my server start command :
>> console.log &
Logback-spring.xml :
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/log-file.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder>
<charset>utf-8</charset>
<Pattern>%d %-5level [%thread] %logger{0}: %msg%n</Pattern>
</encoder>
</appender>
<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<queueSize>512</queueSize>
<appender-ref ref="FILE"/>
</appender>
<logger name="javax.activation" level="WARN"/>
<logger name="javax.mail" level="WARN"/>
<logger name="javax.xml.bind" level="WARN"/>
<logger name="ch.qos.logback" level="WARN"/>
<logger name="com.codahale.metrics" level="WARN"/>
<logger name="com.ryantenney" level="WARN"/>
<logger name="com.sun" level="WARN"/>
<logger name="com.zaxxer" level="WARN"/>
<logger name="io.undertow" level="WARN"/>
<logger name="io.undertow.websockets.jsr" level="ERROR"/>
<logger name="org.ehcache" level="WARN"/>
<logger name="org.apache" level="WARN"/>
<logger name="org.apache.catalina.startup.DigesterFactory" level="OFF"/>
<logger name="org.bson" level="WARN"/>
<logger name="org.elasticsearch" level="WARN"/>
<logger name="org.hibernate.validator" level="WARN"/>
<logger name="org.hibernate" level="WARN"/>
<logger name="org.hibernate.ejb.HibernatePersistence" level="OFF"/>
<logger name="org.springframework" level="WARN"/>
<logger name="org.springframework.web" level="WARN"/>
<logger name="org.springframework.security" level="WARN"/>
<logger name="org.springframework.cache" level="WARN"/>
<logger name="org.thymeleaf" level="WARN"/>
<logger name="org.xnio" level="WARN"/>
<logger name="springfox" level="WARN"/>
<logger name="sun.rmi" level="WARN"/>
<logger name="liquibase" level="WARN"/>
<logger name="LiquibaseSchemaResolver" level="INFO"/>
<logger name="sun.rmi.transport" level="WARN"/>
<!-- https://logback.qos.ch/manual/configuration.html#shutdownHook and https://jira.qos.ch/browse/LOGBACK-1090 -->
<shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook"/>
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
Configure a RollingFileAppender in logback-spring.xml rather than using a ConsoleAppender. See https://logback.qos.ch/manual/appenders.html#RollingFileAppender
The logback-spring.xml generated by JHipster contains all the prod configuration, you just have to uncomment it.
<!-- The FILE and ASYNC appenders are here as examples for a production configuration -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder>
<charset>utf-8</charset>
<Pattern>%d %-5level [%thread] %logger{0}: %msg%n</Pattern>
</encoder>
</appender>
<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<queueSize>512</queueSize>
<appender-ref ref="FILE"/>
</appender>
<root level="${logging.level.root}">
<appender-ref ref="ASYNC"/>
</root>
you can use this code in your own logback.xml
<appender name="dailyRollingFileAppenderError" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${DEV_HOME}/error.log</File>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d [%-5level] %logger{36} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<FileNamePattern>${DEV_HOME}/error.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="error">
<appender-ref ref="dailyRollingFileAppenderError"/>
</root>