Skip to content

Commit

Permalink
combine methods into Log4JUtil.init()
Browse files Browse the repository at this point in the history
  • Loading branch information
xzel23 committed Apr 25, 2024
1 parent 3ae2f00 commit 541e60a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ dependencyResolutionManagement {
library("log4j-api", "org.apache.logging.log4j", "log4j-api").versionRef("log4j")
library("log4j-core", "org.apache.logging.log4j", "log4j-core").versionRef("log4j")
library("log4j-jul", "org.apache.logging.log4j", "log4j-jul").versionRef("log4j")
library("log4j-jcl", "org.apache.logging.log4j", "log4j-jcl").versionRef("log4j")
library("log4j-slf4j2", "org.apache.logging.log4j", "log4j-slf4j2-impl").versionRef("log4j")
library("log4j-to-slf4j", "org.apache.logging.log4j", "log4j-to-slf4j").versionRef("log4j")

Expand Down
2 changes: 1 addition & 1 deletion utility-logging/utility-logging-log4j/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
description = "Java utilities (logging)"
description = "Java utilities (logging, Log4J)"

dependencies {
api(project(":utility-logging"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* </ul>
*/
public final class LogUtilLog4J {
// NOTE: do not use logging in this class as it interferes with LogManager creation!

private LogUtilLog4J() {
}
Expand Down Expand Up @@ -114,15 +115,6 @@ public static LogAppenderLog4j.LogEntryDispatcherLog4J getGlobalDispatcher() {
throw new IllegalStateException("wrong implementation: " + dispatcher.getClass());
}

/**
* Sets the root log level for Log4j.
*
* @param level the LogLevel to set as the root log level
*/
public static void setRootLevel(LogLevel level) {
Configurator.setRootLevel(translate(level));
}

/**
* Translates a Log4J Level object to a custom LogLevel object.
* <p>
Expand Down Expand Up @@ -166,4 +158,18 @@ public static Level translate(LogLevel level) {
case ERROR -> Level.ERROR;
};
}

/**
* Configures the Log4J bridge implementations that are available.
*/
public static void init(LogLevel rootLevel) {
// configure the JUL bridge
System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
// configure the commons-logging bridge
System.setProperty("org.apache.commons.logging.LogFactory", "org.apache.logging.log4j.jcl.LogFactoryImpl");
// no configuration necessary for SLF4J

// set the root logger level
Configurator.setRootLevel(translate(rootLevel));
}
}
2 changes: 1 addition & 1 deletion utility-logging/utility-logging-slf4j/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
description = "Java utilities (logging)"
description = "Java utilities (logging, SLF4J)"

dependencies {
implementation(project(":utility"))
Expand Down

0 comments on commit 541e60a

Please sign in to comment.