From ff75b7ace2d4476ea7e4ba3d98e733286060617e Mon Sep 17 00:00:00 2001 From: dhruba619 Date: Mon, 30 Sep 2019 09:24:04 +0200 Subject: [PATCH] BAEL-2974 Logger vs System.out --- .../log4j2/Log4j2ComparisonSysout.java | 18 ++++++++++++++++++ .../logging/log4j2/Log4j2Example.java | 19 +++++++++++++++++++ .../log4j2/src/test/resources/log4j2.xml | 3 +-- 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/Log4j2ComparisonSysout.java create mode 100644 logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/Log4j2Example.java diff --git a/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/Log4j2ComparisonSysout.java b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/Log4j2ComparisonSysout.java new file mode 100644 index 0000000000..53592082b6 --- /dev/null +++ b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/Log4j2ComparisonSysout.java @@ -0,0 +1,18 @@ +package com.baeldung.logging.log4j2; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintStream; + +public class Log4j2ComparisonSysout { + + public static void main(String[] args) throws FileNotFoundException { + PrintStream outStream = new PrintStream(new File("outFile.txt")); + System.setOut(outStream); + System.out.println("This is a baeldung article"); + + PrintStream errStream = new PrintStream(new File("errFile.txt")); + System.setErr(errStream); + System.err.println("This is a baeldung article error"); + } +} diff --git a/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/Log4j2Example.java b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/Log4j2Example.java new file mode 100644 index 0000000000..b870924cf3 --- /dev/null +++ b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/Log4j2Example.java @@ -0,0 +1,19 @@ +package com.baeldung.logging.log4j2; + +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.LogManager; + +public class Log4j2Example { + + private static final Logger logger = LogManager.getLogger(Log4j2Example.class); + + public static void main(String[] args) { + logger.debug("Debug log message"); + logger.info("Info log message"); + logger.error("Error log message"); + logger.warn("Warn log message"); + logger.fatal("Fatal log message"); + logger.trace("Trace log message"); + } + +} diff --git a/logging-modules/log4j2/src/test/resources/log4j2.xml b/logging-modules/log4j2/src/test/resources/log4j2.xml index 246ffb0707..ee26bcecf2 100644 --- a/logging-modules/log4j2/src/test/resources/log4j2.xml +++ b/logging-modules/log4j2/src/test/resources/log4j2.xml @@ -5,8 +5,7 @@ - +