如何实现PropertyDefiner进行登录以访问多个属性 [英] How to implement PropertyDefiner for logback to access multiple properties

查看:505
本文介绍了如何实现PropertyDefiner进行登录以访问多个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的logback.xml配置文件中定义一些属性,并通过实现

I would like to define some properties in my logback.xml config file and saw that by implementing the PropertyDefiner was a great way to set properties in a customizable way.

开始实施后,我开始想知道如何访问标记内元素的 name 属性值.我什么也没看到,我正在挠头.这个PropertyDefiner真的会让您为每个属性创建一个新的实现吗?为什么不只是硬编码呢?我在网络上没有看到太多关于此的讨论.

After starting to implement it I began to wonder how to access the value of the name attribute of the element within the tag. I'm not seeing anyway to do this and I'm scratching my head. Would this PropertyDefiner really make you create a new implementation for every single property? Why not just hard code it? I didn't see much discussion about this out on the web.

我希望我只是看不到它,而stackoverflow的大脑可以帮助我.有谁知道如何做到这一点?谢谢!

I hope I'm just not seeing it and that the brains of stackoverflow can help me out. Does anyone know how to do this? Thanks!

我发现了这个讨论:基本上是相同的提出了问题,但未返回任何答案.

I found this discussion: essentially the same question is asked, but no answer was returned.

fyi:我想自定义获取属性的方式,因为我是从数据库中提取属性的.我有一个帮助程序类,它在服务器启动时提取属性.这些属性随环境(开发,测试,产品等)而异

fyi: I want to customize how I get my properties because I am pulling it from a database. I have a helper class which pulls the properties in on server startup. These properties vary based on environment (dev, test, prod, etc.)

推荐答案

从Logback版本1.0.6开始,不能直接访问 name 属性的值.但是,没有什么可以阻止您在您选择的属性中传递 name 属性的值.示例:

As of logback version 1.0.6, the value of the name attribute cannot be accessed directly. However, nothing prevents you from passing the value of the name attribute in a property of your choice. Example:

<define name="rootLevel" class="Your.PropertyDefiner">
  <myKey>rootLevel</myKey>
</define>

,其中myKeyYour.PropertyDefiner的属性.例如:

where myKey is a property of Your.PropertyDefiner. For example:

class Your.PropertyDefiner implements PropertyDefiner {   
  String myKey; 
  public void setMyKey(String k) {
    this.myKey= k;   
  }
  public String getPropertyValue() {
    return ...   
  }  
}

Logback的配置框架Joran负责接线. Joran会将 myKey 元素的值注入到Your.PropertyDefiner myKey 属性中.如果您对技术细节感到好奇,请参阅隐式操作在实践中隐含的动作.

Joran, logback's configuration framework, takes care of the wiring. Joran will inject the value of the myKey element into the myKey property of Your.PropertyDefiner. If you are curious about the technical details, see the documentation on implicit actions and implicit actions in practice.

这篇关于如何实现PropertyDefiner进行登录以访问多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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