views:

593

answers:

2

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?

+1  A: 

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.

oedo
sorry, i see what you're asking now. no, there's no generic way to change log level at runtime, but you could easily implement a helper method with a switch statment.
oedo
+1  A: 

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.

Stephen C
There's no mapping necessary really. There are five levels already implicitly defined by the methods in `org.slf4j.Logger`: debug, error, info, trace, warn.
scompt.com
That is a mapping, and it costs CPU cycles to support it.
Stephen C