Spring Boot程序化日志记录配置 [英] Spring Boot programmatic logging configuration

查看:123
本文介绍了Spring Boot程序化日志记录配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Spring启动应用程序中配置以编程方式进行日志记录?

How can I configure logging programmatically in a spring boot application?

使用xml或属性文件不够灵活,无法满足我的需求。

Using an xml or properties file is not flexible enough for my needs.

更新:我希望达到以下目的:

Update: I want to achieve something like this:

@Value("${logging.level.root}")
private String loggingLevelRoot;

@Value("${logging.level.myApp}")
private String loggingLevelMyApp;

@Value("${logging.file}")
private boolean fileAppenderEnabled;

....

setLevel(Logger.ROOT_LOGGER_NAME, Level.toLevel(loggingLevelRoot)));
setLevel("com.myapp", Level.toLevel(loggingLevelMyApp)));
setLevel("org.springframework", Level.WARN);
setLevel("org.apache.coyote", Level.INFO);
setLevel("org.apache.catalina", Level.INFO);
setLevel("org.apache.catalina.startup.DigesterFactory", Level.ERROR);
setLevel("org.apache.catalina.util.LifecycleMBeanBase", Level.ERROR);

Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
logger.addAppender(createConsoleAppender());
if (fileAppenderEnabled) {
    logger.addAppender(createFileAppender());
}

我所拥有的每个环境都是:

All I have per environment is:


  • logging.level.root = [INFO,DEBUG,..]

  • logging.level.myApp = [INFO,DEBUG ,. 。]

  • logging.file = [true | false]

没有重复的XML,Groovy和其他格式我真的不想处理。

No duplication of XML, Groovy and other formats I really don't want to deal with.

在一天结束时,这实际上是为了实现与Spring JavaConfig对bean一样的日志记录灵活性。 XML或其他文件格式过于静态,需要过多的重复,并且与应用程序的其余配置集成得不够好。

At the end of the day, this is really about achieving the same flexibility for logging as Spring JavaConfig did for beans. XML or other file formats are simply too static, require too much duplication and not integrated well enough with the rest of the configuration of the application.

为什么要记录是否配置不同于任何其他bean或服务?没有意义。

推荐答案

我不确定你想要或者需要禁用日志记录系统的默认XML配置,但是您确实希望在之后执行自定义调用。幸运的是,这很简单,因为它在初始化链中尽早完成了 SpringApplication 。放置代码的最简单的地方可能是 SpringApplicationInitializer (它必须实现 ApplicationContextInitializer 以便可以添加它到 SpringApplication )。例如,

I'm not sure you want or need to disable the default XML configuration of the logging system, but you do want to execute your customization calls after that was done. Fortunately that's pretty easy as it's done as early as possible in the initializer chain for a SpringApplication. The easiest place to put your code is probably a SpringApplicationInitializer (it has to implement ApplicationContextInitializer as well so it can be added to the SpringApplication). E.g.

SpringApplication application = new SpringApplication(MySources.class);
application.addInitializers(new LoggingInitializer());
application.run(args);

如果你这样做,你将无法向初始化程序注入依赖项,但是它将确保在生命周期中尽早调用它。如果您的初始化程序实现 EnvironmentAware ,那么在调用 Environment 的实例> SpringApplicationInitializer.initialize() - 使用它可以解析样本中依赖于环境的部分,例如

You won't be able to do dependency injection into the initializer if you do it that way, but it will ensure that it gets called as early as possible in the lifecycle. If your initializer implements EnvironmentAware then you will also be passed an instance of Environment before the call to SpringApplicationInitializer.initialize() - using that you can resolve the environment dependent pieces in your sample, e.g.

String loggingLevelRoot = environment.getProperty("logging.level.root");

一旦你有工作,为了避免对所有应用程序做同样的事情你可以做到通过添加包含初始化程序类的 META-INF / spring.factories 进行声明:

Once you have it working, to avoid having to do the same thing for all apps you can make it declarative by adding a META-INF/spring.factories containing your initializer class:

org.springframework.context.ApplicationContextInitializer=\
my.pkg.for.LoggingInitializer

如果您确实需要依赖注入和 @Value 解析,我认为您将不得不接受 ApplicationContext 在你有机会配置任何东西之前,它会完全刷新。如果这是一个可接受的折衷方案,我建议您只在您的上下文中添加一个 LoggingInitializer ,并让它实现 CommandLineRunner

If you really need dependency injection and @Value resolution I think you are going to have to accept that the ApplicationContext will have fully refreshed before you get a chance to configure anything. If that's an acceptable compromise I recommend just adding a LoggingInitializer to your context and have it implement CommandLineRunner.

这篇关于Spring Boot程序化日志记录配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆