配置logback以遵从Java配置(也称为logback的纯Java配置) [英] Configure logback to defer to Java configuration aka Plain Java Configuration of Logback

查看:623
本文介绍了配置logback以遵从Java配置(也称为logback的纯Java配置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是不喜欢Logback的XML或Groovy配置,而是更喜欢用Java进行配置(这也是因为我将在初始化后的不同时间在运行时更改配置).

I just do not like Logback's XML or Groovy configuration and would prefer to do the configuration in Java (this is also because I'm going to change the configuration at runtime at various times after initialization).

似乎进行Logback的Java配置的唯一方法是进行某种

It seems the only way to do Java configuration of Logback is to have some sort of initialization that hijacks the root appender or to have custom system properties that tell logback to not use the default configuration (which is rather annoying for unit tests and deployment).

我更喜欢的是类似logback.xml的东西:

What I would much rather prefer is something like a logback.xml :

<configuration javaConfig="com.myco.LogBackConfig" /> 

其中LogbackConfig具有一些已建立的静态方法来进行logback调用,或者只是重新实例化Java Bean.

Where LogbackConfig either has some established static method that logback calls or it just re-instantiates a Java bean.

那是我想要一个默认的类路径资源,该资源将告诉logback使用Java而不是groovy/xml.基本上,我想要类似于 Java ServiceLoader 的东西.用于登录配置.

That is I want a default classpath resource that will tell logback to use Java instead of groovy/xml. Basically I want something akin to the Java ServiceLoader for logback config.

也许我无法找到在Logback中执行此操作的方法?

Perhaps there is a way to do this in Logback that I am missing?

更新: 显然, Log4j2(新的log4j)完全符合我的要求,与Java ServiceLoader非常相似.

UPDATE: Apparently Log4j2 (the new log4j) does exactly what I want and is very similar to the Java ServiceLoader.

推荐答案

此功能刚刚合并到logback的源代码中.参见 https://github.com/qos-ch/logback/pull/181

This feature was just merged into logback's source code. See https://github.com/qos-ch/logback/pull/181

这篇关于配置logback以遵从Java配置(也称为logback的纯Java配置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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