From b74df6163a4b45f4ec8ceebb9e0247b3f4925419 Mon Sep 17 00:00:00 2001 From: Daniele Demichelis Date: Sun, 18 Dec 2016 16:36:34 +0100 Subject: [PATCH] Pull Request for "A Guide to Rolling File Appenders" article. (#876) * First test with log4j rolling appenders * small fix * Log4j rolling appender * First set up with rolling file on log4j 2 * Added logback code. * log4j2 more detailed example * log4j2 more detailed example * Improved names and examples * Fixed configurations * improved configs * formatted * Final fix * fixed formatting * Formatting fix * Fix sample apps to avoid try / catch * Fix request to replace files * Fix end lines --- log4j/pom.xml | 29 ++++--- .../baeldung/log4j/Log4jRollingExample.java | 18 +++++ .../baeldung/log4j2/Log4j2RollingExample.java | 19 +++++ .../logback/LogbackRollingExample.java | 19 +++++ .../baeldung/slf4j/Slf4jRollingExample.java | 19 +++++ log4j/src/main/resources/log4j.xml | 75 ++++++++++++++++++- log4j/src/main/resources/log4j2.xml | 56 +++++++++++++- log4j/src/main/resources/logback.xml | 59 ++++++++++++++- 8 files changed, 279 insertions(+), 15 deletions(-) create mode 100644 log4j/src/main/java/com/baeldung/log4j/Log4jRollingExample.java create mode 100644 log4j/src/main/java/com/baeldung/log4j2/Log4j2RollingExample.java create mode 100644 log4j/src/main/java/com/baeldung/logback/LogbackRollingExample.java create mode 100644 log4j/src/main/java/com/baeldung/slf4j/Slf4jRollingExample.java diff --git a/log4j/pom.xml b/log4j/pom.xml index 9b87c131b9..856548327f 100644 --- a/log4j/pom.xml +++ b/log4j/pom.xml @@ -8,13 +8,24 @@ log4j 1.0-SNAPSHOT - - - - log4j - log4j - ${log4j.version} - + + + + log4j + log4j + ${log4j.version} + + + log4j + apache-log4j-extras + ${log4j.version} + + + + log4j + apache-log4j-extras + ${log4j.version} + @@ -58,14 +69,14 @@ - + 1.2.17 2.7 2.7 3.3.6 1.1.7 - + 3.6.0 diff --git a/log4j/src/main/java/com/baeldung/log4j/Log4jRollingExample.java b/log4j/src/main/java/com/baeldung/log4j/Log4jRollingExample.java new file mode 100644 index 0000000000..c0c05e1759 --- /dev/null +++ b/log4j/src/main/java/com/baeldung/log4j/Log4jRollingExample.java @@ -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); + } + } + +} diff --git a/log4j/src/main/java/com/baeldung/log4j2/Log4j2RollingExample.java b/log4j/src/main/java/com/baeldung/log4j2/Log4j2RollingExample.java new file mode 100644 index 0000000000..8e0b9fccab --- /dev/null +++ b/log4j/src/main/java/com/baeldung/log4j2/Log4j2RollingExample.java @@ -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); + } + } + +} diff --git a/log4j/src/main/java/com/baeldung/logback/LogbackRollingExample.java b/log4j/src/main/java/com/baeldung/logback/LogbackRollingExample.java new file mode 100644 index 0000000000..60d55752b3 --- /dev/null +++ b/log4j/src/main/java/com/baeldung/logback/LogbackRollingExample.java @@ -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); + } + } + +} diff --git a/log4j/src/main/java/com/baeldung/slf4j/Slf4jRollingExample.java b/log4j/src/main/java/com/baeldung/slf4j/Slf4jRollingExample.java new file mode 100644 index 0000000000..f5cc4a280a --- /dev/null +++ b/log4j/src/main/java/com/baeldung/slf4j/Slf4jRollingExample.java @@ -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); + } + } + +} diff --git a/log4j/src/main/resources/log4j.xml b/log4j/src/main/resources/log4j.xml index 58a924f970..562d6920f9 100644 --- a/log4j/src/main/resources/log4j.xml +++ b/log4j/src/main/resources/log4j.xml @@ -1,28 +1,95 @@ - + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/log4j/src/main/resources/log4j2.xml b/log4j/src/main/resources/log4j2.xml index 76a20bcd61..b5724c5bee 100644 --- a/log4j/src/main/resources/log4j2.xml +++ b/log4j/src/main/resources/log4j2.xml @@ -8,16 +8,70 @@ # File appender - + # Pattern of log message for file appender + + # Rolling appender + + + %d{yyyy-MM-dd HH:mm:ss} %p %m%n + + + + + + + + + + %d{yyyy-MM-dd HH:mm:ss} %p %m%n + + + + + + + %d{yyyy-MM-dd HH:mm:ss} %p %m%n + + + + + + + + + + + + + # Override log level for specified package + + + + + + diff --git a/log4j/src/main/resources/logback.xml b/log4j/src/main/resources/logback.xml index fc66d560aa..f567962cb6 100644 --- a/log4j/src/main/resources/logback.xml +++ b/log4j/src/main/resources/logback.xml @@ -18,8 +18,65 @@ + # Rolling appenders + + target/slf4j/roll-by-size/app.log + + target/slf4j/roll-by-size/app.%i.log.zip + + 1 + 3 + 1MB + + + 5KB + + + %-4relative [%thread] %-5level %logger{35} - %msg%n + + + + + + target/slf4j/roll-by-time/app.log + + target/slf4j/roll-by-time/app.%d{yyyy-MM-dd-HH-mm}.log.zip + + 20 + 1MB + + + %d{yyyy-MM-dd HH:mm:ss} %p %m%n + + + + + target/slf4j/roll-by-time-and-size/app.log + + target/slf4j/roll-by-time-and-size/app.%d{yyyy-MM-dd-mm}.%i.log.zip + + 5KB + 20 + 1MB + + + %d{yyyy-MM-dd HH:mm:ss} %p %m%n + + + # Override log level for specified package - + + + + +