QT实现一个简单的日志打印系统

简介: QT实现一个简单的日志打印系统

效果演示

使用方法:直接将logsystem.cpp文件和logsystem.h文件添加到工程目录中,然后包含即可使用。

直接调用打印接口

然后在程序exe文件夹下会生成一个文件名叫log_debug.txt文件。

源文件.C

#include "logsystem.h"
LogSystem::LogSystem()
{
}
void LogSystem::error(QString msg)
{
    if(!Write_debug_information("error",msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}
void LogSystem::debug(QString msg)
{
    if(!Write_debug_information("debug",msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}
void LogSystem::warning(QString msg)
{
    if(!Write_debug_information("warning",msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}
void LogSystem::info(QString msg)
{
    if(!Write_debug_information("info",msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}
bool LogSystem::Write_debug_information(QString level, QString message)
{
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_date =current_date_time.toString("yyyy.MM.dd hh:mm");
    QString fileName = QCoreApplication::applicationDirPath() + "/debug_log.txt";// 获取程序当前运行目录
    QTextStream txtOutput(&file);
    QString write_str;
    file.setFileName(fileName);
    if (!file.open(QIODevice::ReadWrite | QIODevice::Append))   /* 以append的方式打开 */
    {
        return false;
    }
    if (level == "DEBUG" || level == "debug")
    {
        write_str = QString("[%1][%2]:%3").arg("DEBUG").arg(current_date).arg(message);
        txtOutput << write_str <<endl;
//        file.write(write_str.toLocal8Bit());
    }
    else if(level == "WARNING" || level == "warning")
    {
        write_str = QString("[%1][%2]:%3").arg("WARNING").arg(current_date).arg(message);
        txtOutput << write_str <<endl;
//        file.write(write_str.toLocal8Bit());
    }
    else if(level == "ERROR" || level == "error")
    {
        write_str = QString("[%1][%2]:%3").arg("ERROR").arg(current_date).arg(message);
        txtOutput << write_str <<endl;
//        file.write(write_str.toLocal8Bit());
    }
    else if(level == "INFO" || level == "info")
    {
        write_str = QString("[%1][%2]:%3").arg("INFO").arg(current_date).arg(message);
        txtOutput << write_str <<endl;
//        file.write(write_str.toLocal8Bit());
    }else
    {
        file.close();/* 关闭文件 */
        return false;
    }
    file.close();/* 关闭文件 */
    return true;
}

源文件.h文件

#ifndef LOGSYSTEM_H
#define LOGSYSTEM_H
#include <QFile>
#include <QString>
#include <QTime>
#include <QDebug>
#include <QTextStream>
#include <QCoreApplication>
#include <QMessageBox>
class LogSystem
{
public:
    LogSystem();
    QFile file;
    QString fileName;
    
private:
    bool Write_debug_information(QString level, QString info);
    
public slots:   
    void info(QString msg);
    void error(QString msg);
    void warning(QString msg);
    void debug(QString msg);
};
#endif // LOGSYSTEM_H

2021-11-21更新

.cpp文件

#include "logsystem.h"
/***
 * 构造函数:
 * 初始化日志系统目录
 ***/
LogSystem::LogSystem()
{
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_date =current_date_time.toString("yyyyMMdd");
    QDir dir;
    if (!dir.exists(fileAddress + current_date))
    {
        dir.mkdir(fileAddress + current_date);
        qDebug() << "[INFO][:" +current_date_time.toString("yyyy.MM.dd hh:ss") +  "]:新建LOG文件夹" << endl;
    }
    fileName = fileAddress + current_date + fileName;
}
/***
 * 打印错误信息并附带系统时间
 ***/
void LogSystem::error(QString msg)
{
    if(openFlag == false) return;
    if(!Write_debug_information("error",true,msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}
/***
 * 打印调试信息并附带系统时间
 ***/
void LogSystem::debug(QString msg)
{
    if(openFlag == false) return;
    if(!Write_debug_information("debug",true,msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}
/***
 * 打印警告信息并附带系统时间
 ***/
void LogSystem::warning(QString msg)
{
    if(openFlag == false) return;
    if(!Write_debug_information("warning",true,msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}
/***
 * 打印提示信息并附带系统时间
 ***/
void LogSystem::info(QString msg)
{
    if(openFlag == false) return;
    if(!Write_debug_information("info",true,msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}
/***
 * 打印错误信息
 ***/
void LogSystem::error_notime(QString msg)
{
    if(openFlag == false) return;
    if(!Write_debug_information("error",false,msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}
/***
 * 打印调试信息
 ***/
void LogSystem::debug_notime(QString msg)
{
    if(openFlag == false) return;
    if(!Write_debug_information("debug",false,msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}
/***
 * 打印警告信息
 ***/
void LogSystem::warning_notime(QString msg)
{
    if(openFlag == false) return;
    if(!Write_debug_information("warning",false,msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}
/***
 * 打印提示信息
 ***/
void LogSystem::info_notime(QString msg)
{
    if(openFlag == false) return;
    if(!Write_debug_information("info",false,msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}
/***
 * 设置日志信息文件名字
 ***/
void LogSystem::setFileName(QString name)
{
    this->fileName = name;
}
/***
 * 设置是否开启日子系统的qdebug信息输出
 ***/
void LogSystem::setQdebugFlag(bool flag)
{
    this->qDebugFlag = flag;
}
/***
 * 打开日志系统
 ***/
void LogSystem::start()
{
    openFlag = true;
}
/***
 * 关闭日志系统
 ***/
void LogSystem::stop()
{
    openFlag = false;
}
/***
 * 写入信息到日志信息文本文件中
 ***/
bool LogSystem::Write_debug_information(QString level, bool time, QString message)
{
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_date =current_date_time.toString("yyyy-MM-dd hh:mm");
    QTextStream txtOutput(&file);
    QString write_str;
    file.setFileName(fileName);
    if (!file.open(QIODevice::ReadWrite | QIODevice::Append))   /* 以append的方式打开 */
    {
        return false;
    }
    if (time == true){
        if (level == "DEBUG" || level == "debug")
        {
            write_str = QString("[%1][%2]:%3").arg("DEBUG").arg(current_date).arg(message);
            txtOutput << write_str <<endl;
            if(qDebugFlag) qDebug() << write_str <<endl;
        }
        else if(level == "WARNING" || level == "warning")
        {
            write_str = QString("[%1][%2]:%3").arg("WARNING").arg(current_date).arg(message);
            txtOutput << write_str <<endl;
            if(qDebugFlag) qDebug() << write_str <<endl;
        }
        else if(level == "ERROR" || level == "error")
        {
            write_str = QString("[%1][%2]:%3").arg("ERROR").arg(current_date).arg(message);
            txtOutput << write_str <<endl;
            if(qDebugFlag) qDebug() << write_str <<endl;
        }
        else if(level == "INFO" || level == "info")
        {
            write_str = QString("[%1][%2]:%3").arg("INFO").arg(current_date).arg(message);
            txtOutput << write_str <<endl;
            if(qDebugFlag) qDebug() << write_str <<endl;
        }else
        {
            file.close();/* 关闭文件 */
            return false;
        }
    }else{
        if (level == "DEBUG" || level == "debug")
        {
            write_str = QString("[%1]:%2").arg("DEBUG_NOTIME").arg(message);
            txtOutput << write_str <<endl;
            if(qDebugFlag) qDebug() << write_str <<endl;
        }
        else if(level == "WARNING" || level == "warning")
        {
            write_str = QString("[%1]:%2").arg("WARNING_NOTIME").arg(message);
            txtOutput << write_str <<endl;
            if(qDebugFlag) qDebug() << write_str <<endl;
        }
        else if(level == "ERROR" || level == "error")
        {
            write_str = QString("[%1]:%2").arg("ERROR_NOTIME").arg(message);
            txtOutput << write_str <<endl;
            if(qDebugFlag) qDebug() << write_str <<endl;
        }
        else if(level == "INFO" || level == "info")
        {
            write_str = QString("[%1]:%2").arg("INFO_NOTIME").arg(message);
            txtOutput << write_str <<endl;
            if(qDebugFlag) qDebug() << write_str <<endl;
        }else
        {
            file.close();/* 关闭文件 */
            return false;
        }
    }
    file.close();/* 关闭文件 */
    return true;
}

.h文件

调用

LogSystem *log = new LogSystem();
log.setQdebugFlag(true);
log.start();


相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
1月前
|
开发框架 Java 编译器
【Qt 元对象系统 01 】深入探索Qt的元对象系统:核心地位、功能与构成
【Qt 元对象系统 01 】深入探索Qt的元对象系统:核心地位、功能与构成
52 0
|
1月前
|
Linux 数据处理 C++
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用(一)
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用
80 0
|
1月前
|
XML 安全 C++
DBus类型系统以及在Qt和C++ 中的使用(二)
DBus类型系统以及在Qt和C++ 中的使用
46 0
|
1月前
|
XML 存储 Unix
DBus类型系统以及在Qt和C++ 中的使用(一)
DBus类型系统以及在Qt和C++ 中的使用
51 0
|
1月前
|
Shell Linux C语言
【Shell 命令集合 网络通讯 】Linux 查看系统中的UUCP日志文件 uulog命令 使用指南
【Shell 命令集合 网络通讯 】Linux 查看系统中的UUCP日志文件 uulog命令 使用指南
31 0
|
1月前
|
存储 Linux API
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用(三)
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用
32 1
|
1月前
|
消息中间件 Linux 数据处理
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用(二)
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用
34 1
|
1月前
|
安全 数据处理 C++
【Qt 底层之事件驱动系统】深入理解 Qt 事件机制:主事件循环与工作线程的交互探究,包括 QML 的视角
【Qt 底层之事件驱动系统】深入理解 Qt 事件机制:主事件循环与工作线程的交互探究,包括 QML 的视角
126 3
|
11天前
|
JavaScript Java 测试技术
基于Java的公司员工工作日志办公系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的公司员工工作日志办公系统的设计与实现(源码+lw+部署文档+讲解等)
32 3
|
1月前
|
Linux API iOS开发
【Qt 渲染引擎】一文带你了解qt的三种 渲染引擎,包括栅格引擎(Raster)、OpenGL 和本地绘图系统
【Qt 渲染引擎】一文带你了解qt的三种 渲染引擎,包括栅格引擎(Raster)、OpenGL 和本地绘图系统
37 0

热门文章

最新文章

推荐镜像

更多