Log4cxx 自定义附加程序
是否可以为 log4cxx 编写自定义附加程序并通过属性文件对其进行配置(如内置附加程序)? 如果可能的话,我更愿意在不必重建 log4cxx 的情况下执行此操作(例如通过派生/扩展现有的附加程序)。
你能给我举个例子吗?
Is it possible to write a custom appender for log4cxx and have it configurable via a properties file (like the built-in appenders)? I'd prefer doing this without having to rebuild log4cxx (e.g. by deriving/extending an existing appender), if possible.
Can you point me toward an example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以继承 AppenderSkeleton 或 WriterAppender 并获得相同的底层行为,而无需重新构建 log4cxx。
http://svn. apache.org/viewvc/incubator/log4cxx/trunk/src/test/cpp/vectorappender.h?view=markup
http://svn. apache.org/viewvc/incubator/log4cxx/trunk/src/test/cpp/vectorappender.cpp?view=markup
You can inherit from AppenderSkeleton or WriterAppender and get the same underlying behaviors without having to rebuilt log4cxx.
http://svn.apache.org/viewvc/incubator/log4cxx/trunk/src/test/cpp/vectorappender.h?view=markup
http://svn.apache.org/viewvc/incubator/log4cxx/trunk/src/test/cpp/vectorappender.cpp?view=markup
brlcad 的建议是正确的 - 在我的公司,我们已将这种方法用于我们自己的自定义附加程序。
事实上,您已经可以从配置文件中配置这些 - 类定义中的 DECLARE_LOG4CXX_OBJECT(CustomAppenderClassName) 等宏设置了 log4cxx 环境。 您可以通过“CustomAppenderClassName”来引用您的附加程序类型。
显示标准和自定义附加程序的配置文件(旧式),使用自定义附加程序的标准布局:
brlcad's suggestion is correct - at my company we've used this method for our own custom appenders.
You can in fact already configure these from a configuration file - the macros like DECLARE_LOG4CXX_OBJECT(CustomAppenderClassName) in the class definition set up the log4cxx environment. You can refer to your appender type by your "CustomAppenderClassName".
A configfile (old-style) showing a standard and custom appender, using the standard layouts for the custom appender:
我添加这个答案是因为评分最高的答案中的链接似乎来自旧版本。 我使用 版本 1.2.0 遵循 SysLogAppender 中所做的操作。 在较高级别上,您将需要执行以下步骤:
void close()
和void append(const spi::InternalLoggingEvent& event)
这是一个示例类,其中包含名称空间为 yournamespace::ExampleCustomAppender 的类的代码:
然后是方法的实现:
最后是如何在配置文件中使用它的示例:
我通过剪切其他一些代码来完成此代码,以便它本身并未以这种确切的格式进行编译。 如果有任何问题请告诉我,我会纠正它。
I am adding this answer as the the links in the most rated answer appear to be from an older version. I have created a custom appender using version 1.2.0 following what was done in their SysLogAppender. At a high level you will need to do these steps:
void close()
andvoid append(const spi::InternalLoggingEvent& event)
Here is an example class with code for class with the namespace yournamespace::ExampleCustomAppender:
Then the implementation of the methods:
and lastly an example of how it may be used in the configuration file:
I did this code by cutting up some other code so it has not been compiled per se in this exact format. Let me know of any issues and I will correct it.