log4cxx 配置文件语法

发布于 2024-11-30 20:42:16 字数 128 浏览 0 评论 0原文

我刚刚发现 log4cxx 日志框架。 似乎有两种不同的语法用于编写配置文件:

  1. xml 方式

  2. 键值方式

这两种方法是否有区别或最佳实践?

I'm just discovering log4cxx logging framework.
It seems there are two different syntaxes for writing config file:

  1. xml manner

  2. key-value manner

Is there a difference or a best practice in this two approaches?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半岛未凉 2024-12-07 20:42:16

在 log4j 中,Ceki Gulcu(作者)建议 XML 配置优先于文本文件,并且它在默认初始化中也优先(log4j.xml 优先于 log4j.txt)。使用 XML 配置可以比使用文本文件实现更多功能(我认为您无法使用文本文件配置操作记录器可加性并设置 log4j 调试模式)。

也就是说,log4cxx 首先也会查找 log4cxx.xml,但网上几乎没有任何配置示例(也没有官方文档),因此您可能需要分析 < code>DOMConfigurator 源代码来找出可能的情况(参考 log4j 示例可能会产生误导,因为它并不总是完全相同)。

总而言之,log4cxx 在 C++ 世界中的受欢迎程度甚至无法与 log4j 在 Java 中的受欢迎程度相提并论。我想知道为什么(除了大量的临时解决方案之外,那里到底流行什么)。

In log4j, Ceki Gulcu (the author) suggests XML configuration over text file, and it takes precedence in default initialization, too (log4j.xml over log4j.txt). You can achieve slightly more with XML configuration than with the text file (I think you cannot manipulate logger additivity and set log4j debug mode with text file configuration).

That said, log4cxx first looks for log4cxx.xml, too, but there are hardly any examples of configuration on the net (and no official documentation, either), so you'll probably need to analyse the DOMConfigurator source code to find out what's possible (referring to log4j examples may prove misleading, as it's not always exactly the same thing).

To conclude, log4cxx popularity in C++ world does not even come close to log4j's in Java. I wonder why (and what the heck IS popular there, except for tons of ad-hoc solutions).

心的憧憬 2024-12-07 20:42:16

这实际上并不是问题的答案,但是当你用谷歌搜索时:

log4cxx xml 配置文件语法

这个问题是最热门的搜索结果。正如 @MaDa 提到的,很难找到 log4cxx 和语法描述的 XML 配置文件示例。所以就是这样。最简单的方法是登录控制台并登录日志文件。

<?xml version="1.0" encoding="UTF-8" ?>

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <!-- Output log messages to the system console. -->
    <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
        </layout>
    </appender>

    <!-- Also output log messages to the log file. -->
    <appender name="FileAppender" class="org.apache.log4j.FileAppender">
        <param name="file" value="LogFile.log" />
        <param name="append" value="true" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p %C{2} (%F:%L) - %m%n" />
        </layout>
    </appender>

    <root>
        <priority value="all" />
        <appender-ref ref="ConsoleAppender" />
        <appender-ref ref="FileAppender" />
    </root>

</log4j:configuration>

和简单的用法示例:

#include "log4cxx/logger.h"
#include "log4cxx/xml/domconfigurator.h"

using namespace log4cxx;
using namespace log4cxx::xml;

LoggerPtr logger (Logger::getLogger ("TEST"));

int main ()
{
    DOMConfigurator::configure ("Log4cxxConfig.xml");

    LOG4CXX_INFO (logger, "App started!");

    LOG4CXX_ERROR (logger, "Some error!");

    return 0;
}

This isn't actually an answer for the question but when you google for:

log4cxx xml config file syntax

this question is the top search result. As it was mentioned by @MaDa it's difficult to find an XML config file example for log4cxx and syntax description. So this is it. Simplest possible, just to log into console and into a log file.

<?xml version="1.0" encoding="UTF-8" ?>

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <!-- Output log messages to the system console. -->
    <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
        </layout>
    </appender>

    <!-- Also output log messages to the log file. -->
    <appender name="FileAppender" class="org.apache.log4j.FileAppender">
        <param name="file" value="LogFile.log" />
        <param name="append" value="true" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p %C{2} (%F:%L) - %m%n" />
        </layout>
    </appender>

    <root>
        <priority value="all" />
        <appender-ref ref="ConsoleAppender" />
        <appender-ref ref="FileAppender" />
    </root>

</log4j:configuration>

And simple usage example:

#include "log4cxx/logger.h"
#include "log4cxx/xml/domconfigurator.h"

using namespace log4cxx;
using namespace log4cxx::xml;

LoggerPtr logger (Logger::getLogger ("TEST"));

int main ()
{
    DOMConfigurator::configure ("Log4cxxConfig.xml");

    LOG4CXX_INFO (logger, "App started!");

    LOG4CXX_ERROR (logger, "Some error!");

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文