Setting log level of message at runtime in slf4j

JavaLoggingLog4jSlf4j

Java Problem Overview


When using log4j, the Logger.log(Priority p, Object message) method is available and can be used to log a message at a log level determined at runtime. We're using this fact and this tip to redirect stderr to a logger at a specific log level.

slf4j doesn't have a generic log() method that I can find. Does that mean there's no way to implement the above?

Java Solutions


Solution 1 - Java

There is no way to do this with slf4j 1.x.

I imagine that the reason that this functionality is missing is that it is next to impossible to construct a Level type for slf4j that can be efficiently mapped to the Level (or equivalent) type used in all of the possible logging implementations behind the facade. Alternatively, the designers decided that your use-case is too unusual to justify the overheads of supporting it.

Concerning @ripper234's use-case (unit testing), I think the pragmatic solution is modify the unit test(s) to hard-wire knowledge of what logging system is behind the slf4j facade ... when running the unit tests.


UPDATE

They intend to implement piecemeal construction of logging events (with dynamic logging levels) in slf4j 2.0; see https://jira.qos.ch/browse/SLF4J-124. According to @Ceki (see comments), it is implemented in the 2.0.0-alpha2 release.

Solution 2 - Java

Richard Fearn has the right idea, so I wrote up the full class based on his skeleton code. It's hopefully short enough to post here. Copy & paste for enjoyment. I should probably add some magic incantation, too: "This code is released to the public domain"

import org.slf4j.Logger;

public class LogLevel {

    /**
     * Allowed levels, as an enum. Import using "import [package].LogLevel.Level"
     * Every logging implementation has something like this except SLF4J.
     */

    public static enum Level {
        TRACE, DEBUG, INFO, WARN, ERROR
    }

    /**
     * This class cannot be instantiated, why would you want to?
     */

    private LogLevel() {
        // Unreachable
    }

    /**
     * Log at the specified level. If the "logger" is null, nothing is logged.
     * If the "level" is null, nothing is logged. If the "txt" is null,
     * behaviour depends on the SLF4J implementation.
     */

    public static void log(Logger logger, Level level, String txt) {
        if (logger != null && level != null) {
            switch (level) {
            case TRACE:
                logger.trace(txt);
                break;
            case DEBUG:
                logger.debug(txt);
                break;
            case INFO:
                logger.info(txt);
                break;
            case WARN:
                logger.warn(txt);
                break;
            case ERROR:
                logger.error(txt);
                break;
            }
        }
    }
    
    /**
     * Log at the specified level. If the "logger" is null, nothing is logged.
     * If the "level" is null, nothing is logged. If the "format" or the "argArray"
     * are null, behaviour depends on the SLF4J-backing implementation.
     */

    public static void log(Logger logger, Level level, String format, Object[] argArray) {
        if (logger != null && level != null) {
            switch (level) {
            case TRACE:
                logger.trace(format, argArray);
                break;
            case DEBUG:
                logger.debug(format, argArray);
                break;
            case INFO:
                logger.info(format, argArray);
                break;
            case WARN:
                logger.warn(format, argArray);
                break;
            case ERROR:
                logger.error(format, argArray);
                break;
            }
        }
    }

    /**
     * Log at the specified level, with a Throwable on top. If the "logger" is null,
     * nothing is logged. If the "level" is null, nothing is logged. If the "format" or
     * the "argArray" or the "throwable" are null, behaviour depends on the SLF4J-backing
     * implementation.
     */
    
    public static void log(Logger logger, Level level, String txt, Throwable throwable) {
        if (logger != null && level != null) {
            switch (level) {
            case TRACE:
                logger.trace(txt, throwable);
                break;
            case DEBUG:
                logger.debug(txt, throwable);
                break;
            case INFO:
                logger.info(txt, throwable);
                break;
            case WARN:
                logger.warn(txt, throwable);
                break;
            case ERROR:
                logger.error(txt, throwable);
                break;
            }
        }
    }
    
    /**
     * Check whether a SLF4J logger is enabled for a certain loglevel. 
     * If the "logger" or the "level" is null, false is returned.
     */

    public static boolean isEnabledFor(Logger logger, Level level) {
        boolean res = false;
        if (logger != null && level != null) {
            switch (level) {
            case TRACE:
                res = logger.isTraceEnabled();
                break;
            case DEBUG:
                res = logger.isDebugEnabled();
                break;
            case INFO:
                res = logger.isInfoEnabled();
                break;
            case WARN:
                res = logger.isWarnEnabled();
                break;
            case ERROR:
                res = logger.isErrorEnabled();
                break;
            }
        }
        return res;
    }
}

Solution 3 - Java

Try switching to Logback and use

ch.qos.logback.classic.Logger rootLogger = (ch.qos.logback.classic.Logger)LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
rootLogger.setLevel(Level.toLevel("info"));

I believe this will be the only call to Logback and the rest of your code will remain unchanged. Logback uses SLF4J and the migration will be painless, just the xml config files will have to be changed.

Remember to set the log level back after you're done.

Solution 4 - Java

You can implement this using Java 8 lambdas.

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;

public class LevelLogger {
	private static final Logger LOGGER = LoggerFactory.getLogger(LevelLogger.class);
	private static final Map<Level, LoggingFunction> map;

	static {
		map = new HashMap<>();
		map.put(Level.TRACE, (o) -> LOGGER.trace(o));
		map.put(Level.DEBUG, (o) -> LOGGER.debug(o));
		map.put(Level.INFO, (o) -> LOGGER.info(o));
		map.put(Level.WARN, (o) -> LOGGER.warn(o));
		map.put(Level.ERROR, (o) -> LOGGER.error(o));
	}
	
	public static void log(Level level, String s) {
		map.get(level).log(s);
	}
	
	@FunctionalInterface
	private interface LoggingFunction {
		public void log(String arg);
	}
}

Solution 5 - Java

This can be done with an enum and a helper method:

enum LogLevel {
    TRACE,
    DEBUG,
    INFO,
    WARN,
    ERROR,
}

public static void log(Logger logger, LogLevel level, String format, Object[] argArray) {
    switch (level) {
        case TRACE:
            logger.trace(format, argArray);
            break;
        case DEBUG:
            logger.debug(format, argArray);
            break;
        case INFO:
            logger.info(format, argArray);
            break;
        case WARN:
            logger.warn(format, argArray);
            break;
        case ERROR:
            logger.error(format, argArray);
            break;
    }
}

// example usage:
private static final Logger logger = ...
final LogLevel level = ...
log(logger, level, "Something bad happened", ...);

You could add other variants of log, say if you wanted generic equivalents of SLF4J's 1-parameter or 2-parameter warn/error/etc. methods.

Solution 6 - Java

It is not possible to specify a log level in sjf4j 1.x out of the box. But there is hope for slf4j 2.0 to fix the issue. In 2.0 it might look like this:

// POTENTIAL 2.0 SOLUTION
import org.slf4j.helpers.Util;
import static org.slf4j.spi.LocationAwareLogger.*;

// does not work with slf4j 1.x
Util.log(logger, DEBUG_INT, "hello world!");

In the meanwhile, for slf4j 1.x, you can use this workaround:

Copy this class into your classpath:

import org.slf4j.Logger;
import java.util.function.Function;

public enum LogLevel {

    TRACE(l -> l::trace, Logger::isTraceEnabled),
    DEBUG(l -> l::debug, Logger::isDebugEnabled),
    INFO(l -> l::info, Logger::isInfoEnabled),
    WARN(l -> l::warn, Logger::isWarnEnabled),
    ERROR(l -> l::error, Logger::isErrorEnabled);

    interface LogMethod {
        void log(String format, Object... arguments);
    }

    private final Function<Logger, LogMethod> logMethod;
    private final Function<Logger, Boolean> isEnabledMethod;

    LogLevel(Function<Logger, LogMethod> logMethod, Function<Logger, Boolean> isEnabledMethod) {
        this.logMethod = logMethod;
        this.isEnabledMethod = isEnabledMethod;
    }

    public LogMethod prepare(Logger logger) {
        return logMethod.apply(logger);
    }

    public boolean isEnabled(Logger logger) {
        return isEnabledMethod.apply(logger);
    }
}

Then you can use it like this:

Logger logger = LoggerFactory.getLogger(Application.class);

LogLevel level = LogLevel.ERROR;
level.prepare(logger).log("It works!"); // just message, without parameter
level.prepare(logger).log("Hello {}!", "world"); // with slf4j's parameter replacing

try {
    throw new RuntimeException("Oops");
} catch (Throwable t) {
    level.prepare(logger).log("Exception", t);
}

if (level.isEnabled(logger)) {
    level.prepare(logger).log("logging is enabled");
}

This will output a log like this:

[main] ERROR Application - It works!
[main] ERROR Application - Hello world!
[main] ERROR Application - Exception
java.lang.RuntimeException: Oops
	at Application.main(Application.java:14)
[main] ERROR Application - logging is enabled

Is it worth it?

  • Pro It keeps the source code location (class names, method names, line numbers will point to your code)
  • Pro You can easily define variables, parameters and return types as LogLevel
  • Pro Your business code stays short and easy to read, and no additional dependencies required.

The source code as minimal example is hosted on GitHub.

Solution 7 - Java

Anyone wanting a drop-in fully SLF4J compatible solution to this problem might want to check out Lidalia SLF4J Extensions - it's on Maven Central.

Solution 8 - Java

I've just needed something like that and came up with:

@RequiredArgsConstructor //lombok annotation
public enum LogLevel{
	
	TRACE(l -> l::trace),
	INFO (l -> l::info),
	WARN (l -> l::warn),
	ERROR(l -> l::error);
	
	private final Function<Logger, Consumer<String>> function;
	
	public void log(Logger logger, String message) {
		function.apply(logger).accept(message);
	}
}

usage:

	LogLevel level = LogLevel.TRACE;
	level.log(logger, "message");

Logger is passed during invocation, so the class info should be ok, and it works nicely with @Slf4j lombok annotation.

Solution 9 - Java

Confirm answer Ondrej Skopek

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;

var rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
rootLogger.setLevel(Level.TRACE);

You will get result:

> 2020-05-14 14:01:16,644 TRACE [] [o.a.k.c.m.Metrics] Test worker > Registered metric named MetricName [name=bufferpool-wait-time-total, > group=producer-metrics, description=The total time an appender waits > for space allocation., tags={client-id=producer-2}]

Solution 10 - Java

The fluent API in SLF4J v2.0 introduces a new method, namely Logger.makeLoggingEventBuilder(Level) which an be used to accomplish the desired outcome.

Example code:

public void logAMessageAtGivenLevel(Level aLevel, String aMessage) {
  Logger logger = .. // some slf4j logger of choice
  logger.makeLoggingEventBuilder(aLevel).log(aMessage);
}

The default implementation will return the singleton instance of NOPLoggingEventBuilder if the logger is disabled for the given Level. This implementation of the LoggingEventBuilder interface, as the name NOP indicates, does nothing, preserving nanosecond execution time for disabled log messages.

Solution 11 - Java

The method I use is to import the ch.qos.logback modules and then type-cast the slf4j Logger instance to a ch.qos.logback.classic.Logger. This instance includes a setLevel() method.

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;

Logger levelSet = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);

// Now you can set the desired logging-level
levelSet.setLevel( Level.OFF );

To find out the possible Logging-levels, you can explode the ch.qos.logback class to see all the possible values for Level:

prompt$ javap -cp logback-classic-1.2.3.jar ch.qos.logback.classic.Level

The results are the following:

{
   // ...skipping
   public static final ch.qos.logback.classic.Level OFF;
   public static final ch.qos.logback.classic.Level ERROR;
   public static final ch.qos.logback.classic.Level WARN;
   public static final ch.qos.logback.classic.Level INFO;
   public static final ch.qos.logback.classic.Level DEBUG;
   public static final ch.qos.logback.classic.Level TRACE;
   public static final ch.qos.logback.classic.Level ALL;
}

Solution 12 - Java

It is not possible with slf4j API to dynamically change log level but you can configure logback (if you use this) by your own. In that case create factory class for your logger and implement root logger with configuration that you need.

LoggerContext loggerContext = new LoggerContext();
ch.qos.logback.classic.Logger root = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);

// Configure appender
final TTLLLayout layout = new TTLLLayout();
layout.start(); // default layout of logging messages (the form that message displays 
// e.g. 10:26:49.113 [main] INFO com.yourpackage.YourClazz - log message

final LayoutWrappingEncoder<ILoggingEvent> encoder = new LayoutWrappingEncoder<>();
encoder.setCharset(StandardCharsets.UTF_8);
encoder.setLayout(layout);

final ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>();
appender.setContext(loggerContext);
appender.setEncoder(encoder);
appender.setName("console");
appender.start();

root.addAppender(appender);

After you configure root logger (only once is enough) you can delegate getting new logger by

final ch.qos.logback.classic.Logger logger = loggerContext.getLogger(clazz);

Remember to use the same loggerContext.

Changing log level is easy to do with root logger given from loggerContext.

root.setLevel(Level.DEBUG);

Solution 13 - Java

I have just encountered a similar need. In my case, slf4j is configured with the java logging adapter (the jdk14 one). Using the following code snippet I have managed to change the debug level at runtime:

Logger logger = LoggerFactory.getLogger("testing");
java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger("testing");
julLogger.setLevel(java.util.logging.Level.FINE);
logger.debug("hello world");

Solution 14 - Java

Based on the answer of massimo virgilio, I've also managed to do it with slf4j-log4j using introspection. HTH.

Logger LOG = LoggerFactory.getLogger(MyOwnClass.class);

org.apache.logging.slf4j.Log4jLogger LOGGER = (org.apache.logging.slf4j.Log4jLogger) LOG;

try {
    Class loggerIntrospected = LOGGER.getClass();
    Field fields[] = loggerIntrospected.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        String fieldName = fields[i].getName();
        if (fieldName.equals("logger")) {
            fields[i].setAccessible(true);
            org.apache.logging.log4j.core.Logger loggerImpl = (org.apache.logging.log4j.core.Logger) fields[i].get(LOGGER);
            loggerImpl.setLevel(Level.DEBUG);
        }
    }
} catch (Exception e) {
    System.out.println("ERROR :" + e.getMessage());
}

Solution 15 - Java

Here's a lambda solution not as user-friendly as @Paul Croarkin's in one way (the level is effectively passed twice). But I think (a) the user should pass the Logger; and (b) AFAIU the original question was not asking for a convenient way for everywhere in the application, only a situation with few usages inside a library.

package test.lambda;
import java.util.function.*;
import org.slf4j.*;

public class LoggerLambda {
	private static final Logger LOG = LoggerFactory.getLogger(LoggerLambda.class);

	private LoggerLambda() {}

	public static void log(BiConsumer<? super String, ? super Object[]> logFunc, Supplier<Boolean> logEnabledPredicate, 
			String format, Object... args) {
		if (logEnabledPredicate.get()) {
			logFunc.accept(format, args);
		}
	}

	public static void main(String[] args) {
		int a = 1, b = 2, c = 3;
		Throwable e = new Exception("something went wrong", new IllegalArgumentException());
		log(LOG::info, LOG::isInfoEnabled, "a = {}, b = {}, c = {}", a, b, c);
		
		// warn(String, Object...) instead of warn(String, Throwable), but prints stacktrace nevertheless
		log(LOG::warn, LOG::isWarnEnabled, "error doing something: {}", e, e);
	}
}

Since slf4j allows a Throwable (whose stack trace should be logged) inside the varargs param, I think there is no need for overloading the log helper method for other consumers than (String, Object[]).

Solution 16 - Java

I was able to do this for the JDK14 binding by first requesting the SLF4J Logger instance and then setting the level on the binding -- you may try this for the Log4J binding.

private void setLevel(Class loggerClass, java.util.logging.Level level) {
  org.slf4j.LoggerFactory.getLogger(loggerClass);
  java.util.logging.Logger.getLogger(loggerClass.getName()).setLevel(level);
}

Solution 17 - Java

using java introspection you can do it, for example:

private void changeRootLoggerLevel(int level) {

	if (logger instanceof org.slf4j.impl.Log4jLoggerAdapter) {
		try {
			Class loggerIntrospected = logger.getClass();
			Field fields[] = loggerIntrospected.getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				String fieldName = fields[i].getName();
				if (fieldName.equals("logger")) {
					fields[i].setAccessible(true);
					org.apache.log4j.Logger loggerImpl = (org.apache.log4j.Logger) fields[i]
							.get(logger);
					
					if (level == DIAGNOSTIC_LEVEL) {
						loggerImpl.setLevel(Level.DEBUG);
					} else {
						loggerImpl.setLevel(org.apache.log4j.Logger.getRootLogger().getLevel());
					}

					// fields[i].setAccessible(false);
				}
			}
		} catch (Exception e) {
			org.apache.log4j.Logger.getLogger(LoggerSLF4JImpl.class).error("An error was thrown while changing the Logger level", e);
		}
	}

}

Solution 18 - Java

no, it has a number of methods, info(), debug(), warn(), etc (this replaces the priority field)

have a look at http://www.slf4j.org/api/org/slf4j/Logger.html for the full Logger api.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEdward DaleView Question on Stackoverflow
Solution 1 - JavaStephen CView Answer on Stackoverflow
Solution 2 - JavaDavid TonhoferView Answer on Stackoverflow
Solution 3 - JavaagelbessView Answer on Stackoverflow
Solution 4 - JavaPaul CroarkinView Answer on Stackoverflow
Solution 5 - JavaRichard FearnView Answer on Stackoverflow
Solution 6 - JavaslartidanView Answer on Stackoverflow
Solution 7 - JavaRobert ElliotView Answer on Stackoverflow
Solution 8 - JavaKamil NowakView Answer on Stackoverflow
Solution 9 - JavaTorinoView Answer on Stackoverflow
Solution 10 - JavaCekiView Answer on Stackoverflow
Solution 11 - JavaGlenn InnView Answer on Stackoverflow
Solution 12 - Javapablo127View Answer on Stackoverflow
Solution 13 - JavaYair ZaslavskyView Answer on Stackoverflow
Solution 14 - JavaGuidoView Answer on Stackoverflow
Solution 15 - JavaEndlosSchleifeView Answer on Stackoverflow
Solution 16 - JavayouurayyView Answer on Stackoverflow
Solution 17 - Javamassimo virgilioView Answer on Stackoverflow
Solution 18 - JavachrisView Answer on Stackoverflow