2012-12-13 39 views
1

我使用的是Log4qt,我按照official steps配置了一个日志文件。我想知道是否可以在运行时动态更改配置。假设默认文件是“myapp.log”我可以在运行时更改Log4Qt配置的文件名吗?

// 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"); 

回答

2

看起来你改变你的QSettings后等,它们都体现在那里,你需要使用:

Log4Qt::Properties::load()

Log4Qt::PropertyConfigurator::configure()

与一个参考你的QSettings变量。如果这不适用文件名更改,则可能需要寻找一种在Log4Qt单例上运行启动的方法。

希望有所帮助。

+0

我使用Log4Qt :: PropertyConfigurator :: configure(),因为我没有Properties类。它工作正常。非常感谢。 – arthur86

相关问题