How to store log message in file using log4qt library ?
-
I am using log4qt a open source library for C++ logging. i want to store log message in file using this library and on off feature of this library . So i need sample example to achieve this.
I am able to get print on console using below code
Log4Qt::LogManager::rootLogger(); Log4Qt::TTCCLayout *p_layout = new Log4Qt::TTCCLayout(); p_layout->setName(QLatin1String("My Layout")); p_layout->activateOptions(); // Create an appender Log4Qt::ConsoleAppender *p_appender = new Log4Qt::ConsoleAppender(p_layout, Log4Qt::ConsoleAppender::STDOUT_TARGET); p_appender->setName(QLatin1String("My Appender")); p_appender->activateOptions(); // Set appender on root logger Log4Qt::Logger::rootLogger()->addAppender(p_appender); Log4Qt::Logger::logger(QLatin1String("My Logger"))->info("Hello World!");
-
@Qt-embedded-developer said in How to store log message in file using log4qt library ?:
log4j.rootLogger=INFO, file
Shouldn't it be FILE, not file?
Compare your properties to https://www.tutorialspoint.com/log4j/log4j_logging_files.htm -
@Qt-embedded-developer What about http://log4qt.sourceforge.net/html/class_log4_qt_1_1_rolling_file_appender.html or http://log4qt.sourceforge.net/html/class_log4_qt_1_1_daily_rolling_file_appender.html ? All in the documentation which you should read...
-
@jsulm Thanks for help. your help encouraged me to achieve something which i need.
I have implemented below code but i am getting error :
ERROR Log4Qt::PropertyConfigurator - Missing appender definition for appender named 'file'
How to resolve this error and How to add "Hello wold" in logging file as warning ?
My log4qt.properties contents are
log4j.rootLogger=INFO, file lo4j.appender.file = org.apache.log4j.FileAppender lo4j.appender.file.File = /home/mangal/WORK_S/Project/24july21_log/50/trunk/logging.log lo4j.appender.file.MaxFileSize=3MB lo4j.appender.file.MaxBackupIndex=2 lo4j.appender.file.layout=org.apache.log4j.PatternLayout lo4j.appender.file.layout.ConversionPattern=%d{yyyy-mm-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
My main file is
QApplication a(argc, argv); // Configure with the specified configuration file QString configFile = "/home/mangal/WORK_S/Project/24july21_log/50/trunk/log4qt.properties"; //QString configFile = "D:/ExecuteProgram/TagDataMonitor/trunk/output/config/log4qt.properties"; if (QFile::exists(configFile)) { Log4Qt::PropertyConfigurator::configureAndWatch(configFile); } Log4Qt::BasicConfigurator::configure(); //log pattern Log4Qt::Logger *log = Log4Qt::Logger::rootLogger(); Log4Qt::PatternLayout *lay = new Log4Qt::PatternLayout(Log4Qt::PatternLayout::TTCC_CONVERSION_PATTERN); lay->setConversionPattern("%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n"); Log4Qt::FileAppender *fileappender = new Log4Qt::FileAppender(lay,"/home/mangal/WORK_S/Project/24july21_log/50/trunk/log4qt.properties"); fileappender->setAppendFile(true); fileappender->activateOptions(); fileappender->setEncoding(QTextCodec::codecForName("UTF-8")); log->addAppender(fileappender); //delete dirtmp; log->deleteLater(); return a.exec(); // Qt application is running, events are generated and sent to the widgets of the application.
-
@Qt-embedded-developer said in How to store log message in file using log4qt library ?:
log4j.rootLogger=INFO, file
Shouldn't it be FILE, not file?
Compare your properties to https://www.tutorialspoint.com/log4j/log4j_logging_files.htm -
Thanks for help. i get the output which i want.
WHEN i am replacing with file print come into file with below print come 2 time in application output window with out put
25 [0x0000000000ba7740] ERROR Log4Qt::PropertyConfigurator - Missing appender definition for appender named 'FILE' (Log4Qt::PropertyConfigurator::CONFIGURATOR_MISSING_APPENDER_ERROR, 24)
24 [0x0000000002571d80] INFO My Logger - Hello World!
My code is :
QApplication a(argc, argv); Log4Qt::BasicConfigurator::configure(); Log4Qt::PropertyConfigurator::configure("/home/mangal/WORK_S/Project/24july21_log/50/trunk/log4qt.properties"); // Configure with the specified configuration file QString configFile = "/home/mangal/WORK_S/Project/24july21_log/50/trunk/log4qt.properties"; if (QFile::exists(configFile)) { Log4Qt::PropertyConfigurator::configureAndWatch(configFile); } Log4Qt::BasicConfigurator::configure(); //log pattern Log4Qt::Logger *log = Log4Qt::Logger::rootLogger(); Log4Qt::PatternLayout *lay = new Log4Qt::PatternLayout(Log4Qt::PatternLayout::TTCC_CONVERSION_PATTERN); lay->setConversionPattern("%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n"); Log4Qt::FileAppender *fileappender = new Log4Qt::FileAppender(lay,"/home/mangal/WORK_S/Project/24july21_log/50/trunk/logging.log"); fileappender->setAppendFile(true); fileappender->activateOptions(); fileappender->setEncoding(QTextCodec::codecForName("UTF-8")); log->addAppender(fileappender); Log4Qt::Logger::logger(QLatin1String("My Logger"))->info("Hello World!"); //delete dirtmp; log->deleteLater();