From 72409da6275dcc04cf4de5a6e2464076cbf5b4f4 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Wed, 17 Aug 2022 09:35:59 +0530 Subject: [PATCH] BAEL-5677 - Logging to both file and console with log4j (#12550) * BAEL-5677 - Logging to both file and console with log4j * Moving from log4j to log4j2 * Correction in class name * Moving to log4j2 package --- .../consoleandfile/Log4j2ConsoleAndFile.java | 14 ++++++++++++++ .../log4j2/src/main/resources/log4j2.properties | 14 ++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/consoleandfile/Log4j2ConsoleAndFile.java create mode 100644 logging-modules/log4j2/src/main/resources/log4j2.properties diff --git a/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/consoleandfile/Log4j2ConsoleAndFile.java b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/consoleandfile/Log4j2ConsoleAndFile.java new file mode 100644 index 0000000000..56119efd9e --- /dev/null +++ b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/consoleandfile/Log4j2ConsoleAndFile.java @@ -0,0 +1,14 @@ +package com.baeldung.logging.log4j2.consoleandfile; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class Log4j2ConsoleAndFile { + + private static final Logger logger = LogManager.getLogger(Log4j2ConsoleAndFile.class); + + public static void main(String[] args) { + logger.info("Hello World!"); + logger.debug("Hello World!"); + } +} diff --git a/logging-modules/log4j2/src/main/resources/log4j2.properties b/logging-modules/log4j2/src/main/resources/log4j2.properties new file mode 100644 index 0000000000..1d31dd55b5 --- /dev/null +++ b/logging-modules/log4j2/src/main/resources/log4j2.properties @@ -0,0 +1,14 @@ +appender.console.type = Console +appender.console.name = STDOUT +appender.console.layout.type = PatternLayout +appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n + +appender.file.type = File +appender.file.name = LOGFILE +appender.file.fileName=logs/log4j.log +appender.file.layout.type=PatternLayout +appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n +appender.file.filter.threshold.type = ThresholdFilter +appender.file.filter.threshold.level = info + +rootLogger=debug, STDOUT, LOGFILE \ No newline at end of file