How to use map lookups using Log4j2?

∥☆過路亽.° 提交于 2019-12-24 13:40:22

问题


I have been looking over these three sites on how to create a map lookup (or any other lookup for that matter) using log4j2:

  • http://logging.apache.org/log4j/2.x/manual/extending.html#Lookups
  • http://logging.apache.org/log4j/2.x/manual/lookups.html
  • http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution

I understand the general idea behind what I am reading, I am just having a difficult time putting all the bits and pieces together.

Goal

To be able to place a variable, of some sort, in a log4j2.xml file that at configuration time, or runtime, replaces that variable with the values in a resource bundle. This is what i have so far:

Configuration File

Lookup Class

The getValue(key) method looks in a resource bundle for the email values associated with that key.The @Plugin name is "map" which, if i have read correctly, is related to prefixes in the .xml file that are also "map". So, wouldn't "error.notification.emails" get passed into the lookup(String key) method in the MapPropertiesLookup class? What do i need to do in order to get this to work?


回答1:


Yes, map is the name of the lookup you are creating. Since Log4j already has a Lookup named map creating another is going to cause problems.

Yes, error.notification.emails should be passed as the key value to the Lookup. Assuming that getValue(key) works your lookup would probably work if it was named something else.

However, it might make more sense for you to file a Jira and ask for a ResourceBundleLookup where the syntax could be something like: ${bundle:bundleName:key}. Of course, "bundleName:key" would be passed to the lookup as the key and it would split the key into the component parts.

If you have further problems you might consider asking on the Log4j developers mailing list.




回答2:


I added a new lookup for the next release (whether it is a beta or release candidate):

Committed revision 1531064.

You can use it now if you build Log4J from source. The format is documented if you build the site, it is used as follows:

${bundle:BundleName:BundleKey}

For example:

${bundle:com.domain.Messages:MyKey}

The java.util.ResourceBundle class will look for com/domain/Messages.properties on the classpath.

Our JIRA issue: https://issues.apache.org/jira/browse/LOG4J2-420



来源:https://stackoverflow.com/questions/19277445/how-to-use-map-lookups-using-log4j2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!