【问题标题】:Can I change the filename of a Log4Qt configuration at runtime?我可以在运行时更改 Log4Qt 配置的文件名吗?
【发布时间】:2012-12-13 21:31:33
【问题描述】:

我正在使用Log4qt 并按照official steps 配置日志文件。我想知道是否可以在运行时动态更改配置。假设默认文件是“myapp.log”

// Set logging level for Log4Qt to TRACE
s.beginGroup("Log4Qt");
s.setValue("Debug", "TRACE");

// Configure logging to log to the file C:/myapp.log using the level TRACE
s.beginGroup("Properties");
s.setValue("log4j.appender.A1", "org.apache.log4j.FileAppender");
s.setValue("log4j.appender.A1.file", "C:/myapp.log");
s.setValue("log4j.appender.A1.layout", "org.apache.log4j.TTCCLayout");
s.setValue("log4j.appender.A1.layout.DateFormat", "ISO8601");
s.setValue("log4j.rootLogger", "TRACE, A1");

// Log first message, which initialises Log4Qt
Log4Qt::Logger::logger("MyApplication")->info("Hello World");

我想在运行时更改文件名,像这样?

QSettings s;
s.beginGroup("Properties");
s.setValue("log4j.appender.A1.file", "C:/NewFile.log");

// Log first message, into new file
Log4Qt::Logger::logger("MyApplication")->info("Hello World");

【问题讨论】:

    标签: c++ qt logging log4cplus


    【解决方案1】:

    看起来在你改变了你的 QSettings 之后,它们反映在那里,你需要使用:

    Log4Qt::Properties::load()

    Log4Qt::PropertyConfigurator::configure()

    引用您的 QSettings 变量。如果这不应用文件名更改,您可能需要寻找一种在 Log4Qt 单例上运行启动的方法。

    希望对您有所帮助。

    【讨论】:

    • 我使用了 Log4Qt::PropertyConfigurator::configure() 因为我没有 Properties 类。它工作正常。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多