Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to store log message in file using log4qt library ?
Forum Updated to NodeBB v4.3 + New Features

How to store log message in file using log4qt library ?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
5 Posts 2 Posters 914 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on 26 Jul 2021, 08:20 last edited by
    #1

    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!");
    
    J 1 Reply Last reply 26 Jul 2021, 08:25
    0
    • Q Qt embedded developer
      26 Jul 2021, 10:26

      @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.
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 26 Jul 2021, 14:30 last edited by
      #4

      @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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Q 1 Reply Last reply 28 Jul 2021, 10:58
      1
      • Q Qt embedded developer
        26 Jul 2021, 08:20

        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!");
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 26 Jul 2021, 08:25 last edited by
        #2

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        Q 1 Reply Last reply 26 Jul 2021, 10:26
        2
        • J jsulm
          26 Jul 2021, 08:25

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

          Q Offline
          Q Offline
          Qt embedded developer
          wrote on 26 Jul 2021, 10:26 last edited by Qt embedded developer
          #3

          @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.
          
          J 1 Reply Last reply 26 Jul 2021, 14:30
          0
          • Q Qt embedded developer
            26 Jul 2021, 10:26

            @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.
            
            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 26 Jul 2021, 14:30 last edited by
            #4

            @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

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            Q 1 Reply Last reply 28 Jul 2021, 10:58
            1
            • J jsulm
              26 Jul 2021, 14:30

              @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

              Q Offline
              Q Offline
              Qt embedded developer
              wrote on 28 Jul 2021, 10:58 last edited by Qt embedded developer
              #5

              @jsulm

              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();
              
              1 Reply Last reply
              0

              1/5

              26 Jul 2021, 08:20

              • Login

              • Login or register to search.
              1 out of 5
              • First post
                1/5
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved