google / flogger

A Fluent Logging API for Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hard to find how to define logging level for system backend

igorgatis opened this issue · comments

I spent a fair amount of time trying to find how to set logging level for system backend. I failed and I gave up. Is there is a knob for that, mind making it easier to find or state it is not possible for system backend?

The system backend uses JDK logger, so just set the log level of the equivalently named JDK logger or parent logger. Using a logging.properties file is best.

http://tutorials.jenkov.com/java-logging/configuration.html

Currently Flogger is agnostic to how log levels are set and deliberately doesn't offer an API (since a different backend might treat log levels completely differently).At some point I might rethink what "controlling logging" means, but that's a very big effort and I don't have time, so for now you have to set logging levels according to the chosen backend in the system.

This makes it hard for random classes to change logging configuration programmatically, but personally I think that's something they should not be doing anyway (logging config should be set up by the outer logic in an application or via system properties, where the logger backend is known).

Thanks! Does it make sense to point that in the easy-to-find documentation?

Yeah, that's a good point, I'll add something to the top level docs. It's always hard to remember to document the things your code doesn't do :)

I'm also having a hard time figuring out how to configure flogger properly. I've migrated a huge codebase from slf4j to flogger, and for some reason when running tests none of the debug logs are being emitted.

Code that previously looked like this just worked:

private static final Logger logger = LoggerFactory.getLogger(MyClass.class);
...
logger.debug("some useful debug info");

but after replacing with:

private static final FluentLogger logger = FluentLogger.forEnclosingClass();
...
logger.atFine().log("some useful debug info");

the logs are not emitting when running tests. Only logs atInfo, atWarning and atSevere are coming out.

Same issue with tests in junit and testng.

Which logging backed are you using? The JDK logger based "system" backend uses INFO as the default log level, so it's expected that "fine" logging will be suppressed unless you use that standard JDK logging configuration mechanisms to change that.

It's using log4j as far as I know. This is also reproducible on the Gerrit project. See this issue.

Gerrit sets up the log like this, using Apache log4j's log manager:

  public static void configureLogging() {
    LogManager.resetConfiguration();

    PatternLayout layout = new PatternLayout();
    layout.setConversionPattern("%-5p %c %x: %m%n");

    ConsoleAppender dst = new ConsoleAppender();
    dst.setLayout(layout);
    dst.setTarget("System.err");
    dst.setThreshold(Level.DEBUG);
    dst.activateOptions();

    Logger root = LogManager.getRootLogger();
    root.removeAllAppenders();
    root.addAppender(dst);

    LOG_LEVELS.entrySet().stream().forEach(e -> getLogger(e.getKey()).setLevel(e.getValue()));
  }

Logs using flogger's atFine() don't emit, but logs using slf4j's debug() do.

I suspect it's something missing (or wrong) in the configuration, but I'm not really sure where to look.

If it's using Log4J, then I'm afraid I can't help you. The Log4J backend is community supported and not something I understand at all really. What I can tell you is that the "core" Flogger code never tries to decide whether to log or not, it always just calls the backend to get that.

My guess is still that it's actually using the JDK backend, since the observed changes in behaviour fit that idea exactly.

I've just posted an update on that issue with some more findings. It looks like the settings that are done by the configureLogging method are not effective for flogger.

As I said, if it's using log4J, then it's a log4J backend bug (which I can't really help with) and if it's using the JDK logger backend then it's expected behaviour (since you aren't doing anything special to enable "fine" logging).

Why not just run the code in a debugger and see at what point a log statement is "dropped", and which classes are being loaded/used in the backend? This won't be hard to track down.

The issue with Gerrit has been fixed by @davido
As I suspected, it was something missing in the configuration of the logger.