3

I'm trying to convert Log4j Level to JUL Level, and would like to use some out-of-the-box utility class. I've found UtilLoggingLevel, but can't figure out how to use it for this particular case. Any ideas?

An ad hoc solution would be:

private static final ImmutableMap<Level, java.util.logging.Level> LEVELS =
    new ImmutableMap.Builder<Level, java.util.logging.Level>()
        .put(Level.ALL, java.util.logging.Level.ALL)
        .put(Level.DEBUG, java.util.logging.Level.FINE)
        .put(Level.ERROR, java.util.logging.Level.SEVERE)
        .put(Level.FATAL, java.util.logging.Level.SEVERE)
        .put(Level.INFO, java.util.logging.Level.INFO)
        .put(Level.OFF, java.util.logging.Level.OFF)
        .put(Level.TRACE, java.util.logging.Level.FINEST)
        .put(Level.WARN, java.util.logging.Level.WARNING)
        .build();

Would be nice to find a standardized function in log4j, if it exists...

yegor256
  • 102,010
  • 123
  • 446
  • 597
  • I would suggest piping it through [`slf4j`](http://www.slf4j.org/); you can pipe the log4j logging in with log4j-over-slf4j and then pipe the logging out over JUL. Something like the "SLF4J bound to JUL..." diagram [here](http://www.slf4j.org/images/legacy.png). – Boris the Spider Jun 12 '13 at 12:05
  • In my case I just need to convert `Level` to `Level`, without sending an actual log message – yegor256 Jun 12 '13 at 13:05
  • to map between two "things", a Map (immutableMap in your case) is a good solution. – DwB Jun 12 '13 at 15:58
  • check this: https://stackoverflow.com/questions/20795373/how-to-map-levels-of-java-util-logging-and-slf4j-logger – Macilias May 22 '17 at 17:06

0 Answers0