问题描述
我想使用MDC在我的程序中设置一个上下文,然后可以在配置文件中设置一个过滤器,只显示来自特定上下文中的记录器的消息。
MDC mdc;
mdc.put(fieldName,foo);
LOG4CPLUS_DEBUG(log,ABC);
mdc.put(fieldsName,bar);
LOG4CPLUS_DEBUG(log,XYZ);
我只想从 log
fieldName
是bar
,因此只有XYZ
在log4j中,使用XML可以格式配置过滤器的配置文件。 log4cplus可以使用XML配置文件吗?是否可以在XML日志中设置此类过滤器?
此外,有没有办法使log4cplus verbose,所以我可以看看它是否找到我的配置文件,如果它能够读取它等。 / p>
我无法找到一个log4cplus XML配置文件的示例,或者如何读取一个。
Log4cplus不支持任何类型的XML配置文件。这是因为它需要一个XML解析器,这是一个相当重的依赖。
对于使用MDC过滤,目前没有这样的过滤器。你必须实现自己的。查看类。
您可以使用 log4cplus.configDebug = 1
从属性文件设置log4cplus调试。或者您可以将 LOG4CPLUS_LOGLOG_DEBUGENABLED
环境变量设置为 1
。前者优先于后者。
I want to use MDC to set a context in my program, and then be able to set a filter in the config file to only show messages from a logger in a certain context.
MDC mdc;
mdc.put("fieldName", "foo");
LOG4CPLUS_DEBUG(log, "ABC");
mdc.put("fieldName", "bar");
LOG4CPLUS_DEBUG(log, "XYZ");
I only want to print from log
when "fieldName"
is "bar"
, so only "XYZ" should be printed to my log.
In log4j this can be done using the XML format config file with filters. Can log4cplus use an XML config file? Can filters like this be set up in the XML log?
Also, is there a way to make log4cplus verbose, so I can see if it is finding my config file, and if it is able to read it, etc.
I am having trouble finding even one example of a log4cplus XML config file or how to read one.
Log4cplus does not support any kind of XML configuration file. This is because it would require an XML parser and that is a pretty heavy dependency.
As for the filtering using MDC, there is currently no such filter. You would have to implement your own. See the Filter
class.
You can set up log4cplus debugging from property file using log4cplus.configDebug=1
. Or you can set the LOG4CPLUS_LOGLOG_DEBUGENABLED
environment variable to 1
. The former takes precedence over the latter.
这篇关于log4cplus是否支持xml配置文件和条件日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!