如何在 micronaut 中设置应用程序的根日志级别

Joh*_*ier 0 micronaut

我正在开发一个 cli 应用程序,它可以接受一些选项:--info, --debug, --trace。我想使用参数来设置整个应用程序的日志记录级别。是否有捷径可寻?这是我到目前为止所尝试过的:

    LogLevel level;
    if(info) {
      level = LogLevel.INFO;
    } else if (debug) {
      level = LogLevel.DEBUG;
    } else if (trace) {
      level = LogLevel.TRACE;
    } else {
      level = LogLevel.WARN;
    }
    loggingSystem.setLogLevel(Logger.ROOT_LOGGER_NAME, level);
Run Code Online (Sandbox Code Playgroud)

loggingSystem被注入到类中。

  @Inject
  private LoggingSystem loggingSystem;
Run Code Online (Sandbox Code Playgroud)

Maf*_*ick 5

I am not quite sure if I get your point. Here is what I found in the official documentation.

Controlling Log Levels with Properties

Controlling Log Levels with Properties

Log levels can be configured via properties defined in application.yml (and environment variables) with the log.level prefix:

logger:
    levels:
        foo.bar: ERROR
Run Code Online (Sandbox Code Playgroud)

Note that the ability to control log levels via config is controlled via the LoggingSystem interface. Currently Micronaut ships with a single implementation that allows setting log levels for the Logback library. If another library is chosen you should provide a bean that implements this interface.