Log4cxx 在 log4cxx/logger.h 中产生错误

Log4cxx producing error in log4cxx/logger.h

提问人:tj26 提问时间:10/12/2023 更新时间:11/23/2023 访问量:45

问:

我有一个正在更新的 Qt 程序有一个不充分的日志记录系统(记录到 syslog),所以我希望使用 Log4cxx(我是 Log4cxx 的新手)。我已经尝试了网页上的一些示例(https://logging.apache.org/log4cxx/latest_stable/examples.html)

我尝试在我的程序中设置基本调用,但我有以下 2 个我不明白的错误。

/usr/include/log4cxx/logger.h:1357: error: 'LevelPtr' does not name a type
         void log(const LevelPtr& level, const std::string& message,
/usr/include/log4cxx/logger.h:1358: error: invalid use of '::'
             const log4cxx::spi::LocationInfo& location) const;
 

此错误来自我的 Input.cpp 文件,该文件调用 AppLog.h 文件,该文件指向下面标记行中的错误。

#ifndef LOG_H
#define LOG_H

#include <QString>
#include <stdlib.h>
#include <log4cxx/helpers/objectptr.h>
#include <log4cxx/level.h>

#include <log4cxx/logger.h> //ERROR HERE
#include <log4cxx/basicconfigurator.h>
#include <log4cxx/helpers/exception.h>
#include <log4cxx/logmanager.h>
#include <log4cxx/ndc.h>
#include <locale.h>

/*!
  * @brief Setup logging and log level to syslog. All log messages are written to syslog.
*/
class Q_Log
{
public:
  static Q_Log *getInstance();

private:
  Q_Log();
  static Q_Log *_instance;
  static log4cxx::LoggerPtr logger;

};

#endif

AppLog.cpp

    #include "AppLog.h"
    #include <QCoreApplication>
    
    using namespace log4cxx;
    using namespace log4cxx::helpers;
        
    Q_Log *Q_Log::_instance = 0;
    
    Q_Log::Q_Log()
    {
       QString name = "PROGRAM_CONVERTER_" +  QCoreApplication::arguments().at(1);
       char* p = new char[name.length() + 1];
       strcpy(p, name.toLatin1().constData());
         
    
       BasicConfigurator::configure();
       LoggerPtr rootLogger = Logger::getRootLogger();
       logger = log4cxx::LogManager::getLogger(p);
    
       LOG4CXX_INFO(logger, "Entering application.");
    }
Q_Log *Q_Log::getInstance()
{
  if(_instance == 0)
  {
    _instance = new Q_Log();
  }

  return _instance;
}

我的 libs 行在 .pro 文件中

LIBS += -lapr-1 -laprutil-1 -llog4cxx-1

我已经安装了 log4cxx 和 log4cxx-devel,并且 qmake 运行正常。

我尝试为示例中未列出的 log4cxx 包含许多不同的头文件,但相同的错误仍在继续。 我没有调用任何函数调用log(),代码中没有 #include < syslog.h>。

任何想法都非常感谢。

谢谢。

C++ Qt 日志记录 log4cxx

评论

0赞 rm5248 10/14/2023
这听起来像是您的编译器无法正常工作。它是否设置为至少 C++11?

答:

0赞 tj26 11/23/2023 #1

似乎它不喜欢在头文件中定义记录器。

static log4cxx::LoggerPtr logger;

所以将其定义移动到 .cpp 文件

#include "Log.h"
#include <QString>
#include <QDebug>
#include <QCoreApplication>

#include <log4cxx/logger.h>
#include <log4cxx/basicconfigurator.h>
#include <log4cxx/helpers/exception.h>
#include <log4cxx/xml/domconfigurator.h>
#include <log4cxx/fileappender.h>

using namespace log4cxx;
using namespace log4cxx::helpers;
using namespace log4cxx::xml;

LoggerPtr rootLogger = Logger::getRootLogger();
LoggerPtr logger(Logger::getLogger("NAME"));

Q_Log *Q_Log::_instance = 0;

Q_Log::Q_Log(QString filename, QString appname)
{   
   //Some code removed here
   // Load XML configuration file using DOMConfigurator
   DOMConfigurator::configure(filename.toStdString());
   BasicConfigurator::configure();
   logger->setLevel(log4cxx::Level::getInfo());
}