1

I am using log4cxx with normal way, which provides a property file for log4cxx. Here we can find a reference:

log4cxx: configuring appender with arguments

Reading Property Info in Log4cxx

But if I want to set log4cxx in my C++ code without property file, what should I do? I check the log4cxx code and try to set properties by log4cxx::helpers::Properties, here is my sample code:

    log4cxx::helpers::Properties properties;
    properties.setProperty(L"log4j.rootLogger",L"DEBUG");
    properties.setProperty(L"log4j.appender",L"ca");
    properties.setProperty(L"log4j.appender",L"fa");

    properties.setProperty(L"log4j.appender.fa",L"org.apache.log4j.FileAppender");
    properties.setProperty(L"log4j.appender.fa.Append",L"false");
    properties.setProperty(L"log4j.appender.fa.File",L"./test.log");
    properties.setProperty(L"log4j.appender.fa.layout",L"org.apache.log4j.PatternLayout");
    properties.setProperty(L"log4j.appender.fa.layout.ConversionPattern",L"%d [%t] %-5p %.16c - %m%n");

    properties.setProperty(L"log4j.appender.ca",L"org.apache.log4j.ConsoleAppender");
    properties.setProperty(L"log4j.appender.ca.layout",L"log4j.appender.ca.layout");
    properties.setProperty(L"log4j.appender.ca.layout.ConversionPattern",L"%d [%t] %-5p %.16c - %m%n");

    log4cxx::PropertyConfigurator::configure(properties);

But my sample code doesn't work. Why? Anyone knows how to initialize log4cxx logger without property file?

Thanks

Water Lin

4

3 に答える 3

2

私はこれについて投稿します:

http://bigbang.waterlin.org/bang/the-way-to-set-log4cxx-properties-without-using-property-file/

さらに参照を追加します。

ログをファイルとコンソールに同時にリダイレクトする場合は、次のコードを使用してください。

#include <log4cxx/logger.h>
#include <log4cxx/helpers/pool.h>
#include <log4cxx/basicconfigurator.h>
#include <log4cxx/fileappender.h>
#include <log4cxx/simplelayout.h>
#include "log4cxx/consoleappender.h"

LoggerPtr logger;

int main() {
    log4cxx::FileAppender * fileAppender = new log4cxx::FileAppender(log4cxx::LayoutPtr(new log4cxx::SimpleLayout()), L"logfile", false);

    log4cxx::ConsoleAppender * consoleAppender = new log4cxx::ConsoleAppender(log4cxx::LayoutPtr(new log4cxx::SimpleLayout()));

    log4cxx::helpers::Pool p;
    fileAppender->activateOptions(p);

    log4cxx::BasicConfigurator::configure(log4cxx::AppenderPtr(fileAppender));
    log4cxx::BasicConfigurator::configure(log4cxx::AppenderPtr(consoleAppender));
    log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getDebug());
    log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger("logger");

    LOG4CXX_INFO(logger,"Created FileAppender appender");

    return 0;
}
于 2013-01-23T10:43:14.217 に答える
1

同じ課題に直面したときに、 log4cxxwikiのコード例が役立つことがわかりました。

于 2013-01-16T04:10:18.977 に答える
0

ByteArrayInputStreamを使用してlog4cxxを設定できます

std::vector<byte> vc;
// copy property file content into vc
log4cxx::helpers::ByteArrayInputStreamPtr bais(
   new log4cxx::helpers::ByteArrayInputStream(vc));
log4cxx::helpers::Properties pp;
pp.load(bais);
log4cxx::PropertyConfigurator::configure(pp);
于 2013-04-07T04:00:51.767 に答える