This commit is contained in:
eugenp 2016-12-18 18:11:25 +02:00
commit 43f407e6e7
8 changed files with 279 additions and 15 deletions

View File

@ -8,13 +8,24 @@
<artifactId>log4j</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--log4j dependencies-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependencies>
<!--log4j dependencies -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>apache-log4j-extras</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>apache-log4j-extras</artifactId>
<version>${log4j.version}</version>
</dependency>
<!--log4j2 dependencies-->
<dependency>

View File

@ -0,0 +1,18 @@
package com.baeldung.log4j;
import java.util.stream.IntStream;
import org.apache.log4j.Logger;
public class Log4jRollingExample {
private final static Logger logger = Logger.getLogger(Log4jRollingExample.class);
public static void main(String[] args) throws InterruptedException {
for(int i = 0; i<2000; i++){
logger.info("This is the " + i + " time I say 'Hello World'.");
Thread.sleep(100);
}
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.log4j2;
import java.util.stream.IntStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Log4j2RollingExample {
private static final Logger logger = LogManager.getLogger(Log4j2RollingExample.class);
public static void main(String[] args) throws InterruptedException {
for(int i = 0; i<2000; i++){
logger.info("This is the {} time I say 'Hello World'.", i);
Thread.sleep(100);
}
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.logback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.stream.IntStream;
public class LogbackRollingExample {
private static final Logger logger = LoggerFactory.getLogger(LogbackRollingExample.class);
public static void main(String[] args) throws InterruptedException {
for(int i = 0; i<2000; i++){
logger.info("This is the {} time I say 'Hello World'.", i);
Thread.sleep(100);
}
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.stream.IntStream;
public class Slf4jRollingExample {
private static Logger logger = LoggerFactory.getLogger(Slf4jRollingExample.class);
public static void main(String[] args) throws InterruptedException {
for(int i = 0; i<2000; i++){
logger.info("This is the {} time I say 'Hello World'.", i);
Thread.sleep(100);
}
}
}

View File

@ -1,28 +1,95 @@
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration debug="false">
<!--Console appender-->
<!--Console appender -->
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %p %m%n"/>
</layout>
</appender>
<!-- File appender-->
<!-- File appender -->
<appender name="fout" class="org.apache.log4j.FileAppender">
<param name="file" value="log4j/target/baeldung-log4j.log"/>
<param name="append" value="false"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n"/>
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %p %m%n"/>
</layout>
</appender>
<!--Override log level for specified package-->
<!-- Rolling appenders -->
<appender name="roll-by-size" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="target/log4j/roll-by-size/app.log"/>
<param name="MaxFileSize" value="5KB"/>
<param name="MaxBackupIndex" value="2"/> <!-- It's one by default. -->
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n"/>
</layout>
</appender>
<appender name="roll-by-size-2" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="target/log4j/roll-by-size-2/app.log"/>
<param name="MaxFileSize" value="5KB"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n"/>
</layout>
</appender>
<appender name="roll-by-window"
class="org.apache.log4j.rolling.RollingFileAppender">
<rollingPolicy
class="org.apache.log4j.rolling.FixedWindowRollingPolicy">
<param name="ActiveFileName" value="target/log4j/roll-by-window/app.log"/>
<param name="FileNamePattern"
value="target/log4j/roll-by-window/app.%i.log.gz"/>
<param name="MinIndex" value="7"/>
<param name="MaxIndex" value="17"/>
</rollingPolicy>
<triggeringPolicy
class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
<param name="MaxFileSize" value="50000"/>
</triggeringPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n"/>
</layout>
</appender>
<appender name="roll-by-time"
class="org.apache.log4j.rolling.RollingFileAppender">
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern"
value="target/log4j/roll-by-time/app.%d{HH-mm}.log.gz"/>
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n"/>
</layout>
</appender>
<appender name="roll-by-time-and-size"
class="org.apache.log4j.rolling.RollingFileAppender">
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern"
value="target/log4j/roll-by-time-and-size/app.%d{HH-mm}.%i.log.gz"/>
</rollingPolicy>
<triggeringPolicy
class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
<param name="MaxFileSize" value="50000"/>
</triggeringPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n"/>
</layout>
</appender>
<!--Override log level for specified package -->
<category name="com.baeldung.log4j">
<priority value="TRACE"/>
</category>
<category name="com.baeldung.log4j.Log4jRollingExample">
<priority value="TRACE"/>
<appender-ref ref="roll-by-size"/>
<appender-ref ref="roll-by-size-2"/>
<appender-ref ref="roll-by-window"/>
<appender-ref ref="roll-by-time"/>
<appender-ref ref="roll-by-time-and-size"/>
</category>
<root>
<level value="DEBUG"/>
<appender-ref ref="stdout"/>

View File

@ -8,16 +8,70 @@
</Console>
# File appender
<File name="fout" fileName="log4j/target/baeldung-log4j2.log" immediateFlush="false" append="false">
<File name="fout" fileName="log4j/target/baeldung-log4j2.log"
immediateFlush="false" append="false">
# Pattern of log message for file appender
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %p %m%n"/>
</File>
# Rolling appender
<RollingFile name="roll-by-size"
fileName="target/log4j2/roll-by-size/app.log" filePattern="target/log4j2/roll-by-size/app.%i.log.gz"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %p %m%n</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy/>
<SizeBasedTriggeringPolicy
size="5 KB"/>
</Policies>
</RollingFile>
<RollingFile name="roll-by-time"
fileName="target/log4j2/roll-by-time/app.log"
filePattern="target/log4j2/roll-by-time/app.%d{MM-dd-yyyy-HH-mm}.log.gz"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %p %m%n</Pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy/>
</RollingFile>
<RollingFile name="roll-by-time-and-size"
fileName="target/log4j2/roll-by-time-and-size/app.log"
filePattern="target/log4j2/roll-by-time-and-size/app.%d{MM-dd-yyyy-HH-mm}.%i.log.gz"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %p %m%n</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy/>
<SizeBasedTriggeringPolicy
size="5 KB"/>
<TimeBasedTriggeringPolicy/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${baseDir}" maxDepth="2">
<IfFileName
glob="target/log4j2/roll-by-time-and-size/app.*.log.gz"/>
<IfLastModified age="20s"/>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
# Override log level for specified package
<Logger name="com.baeldung.log4j2" level="TRACE"/>
<Logger name="com.baeldung.log4j2.Log4j2RollingExample"
level="TRACE">
<AppenderRef ref="roll-by-size"/>
<AppenderRef ref="roll-by-time"/>
<AppenderRef ref="roll-by-time-and-size"/>
</Logger>
<AsyncRoot level="DEBUG">
<AppenderRef ref="stdout"/>
<AppenderRef ref="fout"/>

View File

@ -18,8 +18,65 @@
</encoder>
</appender>
# Rolling appenders
<appender name="roll-by-size"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>target/slf4j/roll-by-size/app.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>target/slf4j/roll-by-size/app.%i.log.zip
</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>3</maxIndex>
<totalSizeCap>1MB</totalSizeCap>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>5KB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n
</pattern>
</encoder>
</appender>
<appender name="roll-by-time"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>target/slf4j/roll-by-time/app.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>target/slf4j/roll-by-time/app.%d{yyyy-MM-dd-HH-mm}.log.zip
</fileNamePattern>
<maxHistory>20</maxHistory>
<totalSizeCap>1MB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %p %m%n</pattern>
</encoder>
</appender>
<appender name="roll-by-time-and-size"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>target/slf4j/roll-by-time-and-size/app.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>target/slf4j/roll-by-time-and-size/app.%d{yyyy-MM-dd-mm}.%i.log.zip
</fileNamePattern>
<maxFileSize>5KB</maxFileSize>
<maxHistory>20</maxHistory>
<totalSizeCap>1MB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %p %m%n</pattern>
</encoder>
</appender>
# Override log level for specified package
<logger name="com.baeldung.logback" level="TRACE"/>
<logger name="com.baeldung.slf4j.Slf4jRollingExample" level="TRACE">
<appender-ref ref="roll-by-size"/>
<appender-ref ref="roll-by-time"/>
<appender-ref ref="roll-by-time-and-size"/>
</logger>
<root level="DEBUG">
<appender-ref ref="stdout"/>