Skip to content
Permalink
Browse files

Thread-safety for LoggingUtils as well as making configuration status…

… output configurable (previous behavior was to always print to console in the event that there were warnings or errors in log configuration)
  • Loading branch information...
revetkn committed Jun 26, 2019
1 parent c6c4286 commit a865db312811e6b6e37968db046d9afcbdbabd47
Showing with 57 additions and 38 deletions.
  1. +57 −38 src/main/java/com/soklet/util/LoggingUtils.java
@@ -16,68 +16,87 @@

package com.soklet.util;

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static java.util.logging.LogManager.getLogManager;
import static org.slf4j.LoggerFactory.getILoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;
import org.slf4j.bridge.SLF4JBridgeHandler;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.logging.Handler;

import org.slf4j.bridge.SLF4JBridgeHandler;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static java.util.logging.LogManager.getLogManager;
import static org.slf4j.LoggerFactory.getILoggerFactory;

/**
* Utility methods for common logging functions.
*
*
* @author <a href="http://revetkn.com">Mark Allen</a>
* @since 1.1.4
*/
public final class LoggingUtils {
private static final Object LOCK;

static {
LOCK = new Object();
}

public enum LogbackOption {
DEBUGGING_ENABLED
}

private LoggingUtils() {}

public static void initializeLogback(Path logbackConfigurationFile) {
requireNonNull(logbackConfigurationFile);
public static void initializeLogback(Path logbackConfigurationFile, LogbackOption... logbackOptions) {
synchronized (LOCK) {
requireNonNull(logbackConfigurationFile);

if (!Files.exists(logbackConfigurationFile))
throw new IllegalArgumentException(format(
"Unable to initialize Logback logging. Could not find a configuration file at %s",
logbackConfigurationFile.toAbsolutePath()));
List<LogbackOption> logbackOptionsAsList = logbackOptions == null ? Collections.emptyList() : Arrays.asList(logbackOptions);

if (!Files.isRegularFile(logbackConfigurationFile))
throw new IllegalArgumentException(format(
"Unable to initialize Logback logging. The configuration path %s does not appear to be a regular file",
logbackConfigurationFile.toAbsolutePath()));
if (!Files.exists(logbackConfigurationFile))
throw new IllegalArgumentException(format(
"Unable to initialize Logback logging. Could not find a configuration file at %s",
logbackConfigurationFile.toAbsolutePath()));

uninstallLogback();
if (!Files.isRegularFile(logbackConfigurationFile))
throw new IllegalArgumentException(format(
"Unable to initialize Logback logging. The configuration path %s does not appear to be a regular file",
logbackConfigurationFile.toAbsolutePath()));

LoggerContext loggerContext = (LoggerContext) getILoggerFactory();
uninstallLogback();

try {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
loggerContext.reset();
configurator.doConfigure(logbackConfigurationFile.toFile().getAbsolutePath());
} catch (JoranException e) {
throw new IllegalStateException("Unable to configure Logback logging", e);
}
LoggerContext loggerContext = (LoggerContext) getILoggerFactory();

StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
try {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
loggerContext.reset();
configurator.doConfigure(logbackConfigurationFile.toFile().getAbsolutePath());
} catch (JoranException e) {
throw new IllegalStateException("Unable to configure Logback logging", e);
}

// Bridge all java.util.logging to SLF4J
java.util.logging.Logger rootLogger = getLogManager().getLogger("");
for (Handler handler : rootLogger.getHandlers())
rootLogger.removeHandler(handler);
if (logbackOptionsAsList.contains(LogbackOption.DEBUGGING_ENABLED))
StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);

SLF4JBridgeHandler.install();
// Bridge all java.util.logging to SLF4J
java.util.logging.Logger rootLogger = getLogManager().getLogger("");
for (Handler handler : rootLogger.getHandlers())
rootLogger.removeHandler(handler);

SLF4JBridgeHandler.install();
}
}

public static void uninstallLogback() {
if (SLF4JBridgeHandler.isInstalled()) SLF4JBridgeHandler.uninstall();
synchronized (LOCK) {
if (SLF4JBridgeHandler.isInstalled()) SLF4JBridgeHandler.uninstall();
}
}
}

0 comments on commit a865db3

Please sign in to comment.
You can’t perform that action at this time.