Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting log level of message at runtime in slf4j

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?

like image 737
Edward Dale Avatar asked Apr 12 '10 11:04

Edward Dale


People also ask

How do you change the log level in slf4j?

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.

Can we change log level at runtime?

In terms of scaling, the challenge is to change log levels in each instance. So to avoid all pitfalls following is needed: Dynamically change the log level at runtime without application restart. Propagation of log level changes across the application instances.

What is the default logging level in slf4j?

slf4j. simpleLogger. defaultLogLevel - Default log level for all instances of SimpleLogger. Must be one of ("trace", "debug", "info", "warn", "error" or "off").


1 Answers

There is no way to do this with slf4j.

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.

like image 118
Stephen C Avatar answered Sep 19 '22 00:09

Stephen C