Log4Qt使用相关使用

 

简介       

    前一段时间用过log4Qt,只是零零散散的使用了。没有仔细的深究。抱着能用就行的心态终于遇到坑了。在网上也没有系统的介绍Log4Qt的文章。现在系统介绍下。希望对自己和别人有所帮助。log4Qt是根据Apache的log4j修改过来的,所有里面用的时候也会有jave的影子。需要详细的学习的同学可以搜说log4j的资料。因为log4Qt的资料太少太少了。

log4qt基本框架

           

logger

用来写日志。(根logger和普通属于从属关系)

Log4Qt::Logger::rootLogger() 【根logger】Log4Qt::Logger::Logger(“XXX”)【可以自己命名】

Appender

日志的输出位置,输出到文件,控制台,数据库等

consoleAppender(配置输出到控制台)

FileAppender(配置输出到文件)

。。。

Layout

配置日志的输出格式。

PartternLayout()根据格式字符串输出日志事件

SimpleLayout() 输出日志记录的级别和消息

TTCCLayout():输出日志事件的时间、线程名称、Logger名称和嵌套的诊断上下文信息。

 使用实例

1.通过配置文件使用logger

log4qt中通过QSetting配置config文件直接使用,代码如下:

配置日志输出到文件:

LoggerFile 类采用了单例模式,使用的时候调用get_instance()->debug(QString)直接输出;

logger_file.h

class LoggerFile : public QObject
{
    Q_OBJECT
public:
    explicit LoggerFile(QObject *parent = 0);
    ~LoggerFile();

    static LoggerFile *get_instance(const char *, int, const char *);
    static LoggerFile *get_instance();

    void init_file_setting();
    void init_file_logger();

    void debug(const QVariant&);
    void waring(const QVariant&);
    void info(const QVariant&);
    void error(const QVariant&);

private:
    Log4Qt::Logger *m_file_logger;
    QString m_file_path;
    QSettings *m_file_setting;
    static LoggerFile *m_instance;
    static QString m_filter;
};

 

logger_file.cc

LoggerFile* LoggerFile::m_instance = NULL;
QString LoggerFile::m_filter = "";

LoggerFile::LoggerFile(QObject *parent)
{
    setParent(parent);
    m_file_path = QDir::homePath() + "/.pangen_gui" + "/pgui_log/";
    init_file_setting();
    Log4Qt::PropertyConfigurator::configure(*m_file_setting);
    init_file_logger();
}

LoggerFile::~LoggerFile()
{
    delete m_instance;
    m_instance = NULL;
}

LoggerFile *LoggerFile::get_instance(const char *fileName, int lineNumber, const char *functionName)
{
    m_filter = QString("[FILE:%1, LINE(%2)]: ").arg(fileName).arg(lineNumber);
    QMutex m_mutex;
    m_mutex.lock();
    if(NULL == m_instance)
    {
        m_instance = new LoggerFile();
    }
    m_mutex.unlock();
    return m_instance;
}

LoggerFile *LoggerFile::get_instance()
{
    QMutex m_mutex;
    m_mutex.lock();
    if(NULL == m_instance)
    {
        m_instance = new LoggerFile();
    }
    m_mutex.unlock();
    return m_instance;
}

void LoggerFile::init_file_setting()
{
    QString configFile_path = QDir::homePath()+ "/.pangen_gui" + "/pgui_config";
    QDir dir(configFile_path);
    if (!dir.exists())
    {
        if(!dir.mkpath(configFile_path))
        {
            qDebug() << "make config_dir error !";
            return;
        }
    }
    m_file_setting = new QSettings(configFile_path + "/qtlogger_file.conf", QSettings::IniFormat);
    m_file_setting->clear();
    m_file_setting->setValue("log4j.rootLogger", "debug,dailyFile");
    m_file_setting->setValue("log4j.appender.dailyFile", "org.apache.log4j.DailyRollingFileAppender");
    m_file_setting->setValue("log4j.appender.dailyFile.File", m_file_path + QDateTime::currentDateTime().toString("yyyyMMdd")+ ".log");
    m_file_setting->setValue("log4j.appender.dailyFile.AppendFile", "true");
    m_file_setting->setValue("log4j.appender.dailyFile.layout", "org.apache.log4j.PatternLayout");
    m_file_setting->setValue("log4j.appender.dailyFile.layout.ConversionPattern", "%-d{yyyy-MM-dd HH:mm:ss.zzz} [%p] %m%n");
}

void LoggerFile::init_file_logger()
{
    QDir Log_Dir(m_file_path);
    if (!Log_Dir.exists())
    {
        if (Log_Dir.mkpath(m_file_path))
        {
            qDebug() << "make log file path error!";
        }
    }
    m_file_logger = Log4Qt::Logger::logger("File");
}

void LoggerFile::debug(const QVariant& Text)
{
    m_file_logger->debug(m_filter + Text.toString());
}

void LoggerFile::waring(const QVariant& Text)
{
    m_file_logger->warn(m_filter + Text.toString());
}

void LoggerFile::info(const QVariant& Text)
{
    m_file_logger->info(m_filter + Text.toString());
}

void LoggerFile::error(const QVariant& Text)
{
    m_file_logger->error(m_filter + Text.toString());
}

 

使用配置文件的方法,把日志输出到控制台中:

Logger_console.h

class LoggerConsole : public QObject
{
    Q_OBJECT
public:
    explicit LoggerConsole(QObject *parent = 0);
    ~LoggerConsole();

    const LoggerConsole& operator<<(QVariant);

    void init_console_setting();
    void init_console_logger();

    static LoggerConsole *get_instance(const char *, int, const char *);
    void debug(const QVariant&);
    void waring(const QVariant&);
    void info(const QVariant&);
    void error(const QVariant&);
//    QString variant_to_string(const QVariant&);

private:
    Log4Qt::Logger *m_console_logger;
    QSettings *m_console_setting;
    static LoggerConsole *m_instance;
    static QString m_filter;
};

Logger_console.cc

LoggerConsole::LoggerConsole(QObject *parent):
       QObject(parent)
{
    init_console_setting();
    Log4Qt::PropertyConfigurator::configure(*m_console_setting);
    Log4Qt::LogManager::setHandleQtMessages(true);
    init_console_logger();
}

LoggerConsole::~LoggerConsole()
{
    delete m_instance;
    m_instance = NULL;
}

void LoggerConsole::init_console_setting()
{
   QString configFile_path = QDir::homePath()+ "/.pangen_gui" + "/pgui_config";
   QDir dir(configFile_path);
   if (!dir.exists())
   {
       if(!dir.mkpath(configFile_path))
       {
           qDebug() << "make config_dir error !";
           return;
       }
   }
   m_console_setting = new QSettings(configFile_path + "/qtlogger_console.conf", QSettings::IniFormat);
   m_console_setting->clear();
   m_console_setting->setValue("log4j.rootLogger", "DEBUG, console");
   m_console_setting->setValue("log4j.appender.console", "org.apache.log4j.ConsoleAppender");
   m_console_setting->setValue("log4j.appender.console.layout", "org.apache.log4j.PatternLayout");
   m_console_setting->setValue("log4j.appender.console.layout.ConversionPattern", "%-d{yyyy-MM-dd HH:mm:ss.zzz} [%p] %m%n");
}

const LoggerConsole&  LoggerConsole::operator<<(QVariant data)
{
    debug(data);
    return *this;
}


void LoggerConsole::init_console_logger()
{
       m_console_logger = Log4Qt::Logger::logger("Debug");
}

LoggerConsole *LoggerConsole::get_instance(const char *fileName, int lineNumber, const char *functionName)
{
    m_filter = QString("[FILE:%1, LINE(%2)]: ").arg(fileName).arg(lineNumber);
    QMutex m_mutex;
    m_mutex.lock();
    if(NULL == m_instance)
    {
        m_instance = new LoggerConsole();
    }
    m_mutex.unlock();
    return m_instance;
}

void LoggerConsole::debug(const QVariant &Text)
{
    m_console_logger->debug(m_filter + Text.toString());
}

void LoggerConsole::waring(const QVariant &Text)
{
    m_console_logger->warn(m_filter + Text.toString());
}

void LoggerConsole::info(const QVariant &Text)
{
    m_console_logger->info(m_filter + Text.toString());
}

void LoggerConsole::error(const QVariant &Text)
{
    m_console_logger->error(m_filter + Text.toString());
}

上面要注意的函数 Log4Qt::PropertyConfigurator::configure(QSetting)

这个函数用来配置logger的全局的一个配置,不可以在程序中加载两次不同的配置文件,会导致日志配置混乱. 

上面两种配置最好单独使用

你可能感兴趣的:(QT,log4qt)