开发者

log4cxx config file syntax

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?


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).


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜