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

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: 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日志并进行多维度分析。
相关文章
|
2月前
|
存储 数据采集 数据处理
【Flume拓扑揭秘】掌握Flume的四大常用结构,构建强大的日志收集系统!
【8月更文挑战第24天】Apache Flume是一个强大的工具,专为大规模日志数据的收集、聚合及传输设计。其核心架构包括源(Source)、通道(Channel)与接收器(Sink)。Flume支持多样化的拓扑结构以适应不同需求,包括单层、扇入(Fan-in)、扇出(Fan-out)及复杂多层拓扑。单层拓扑简单直观,适用于单一数据流场景;扇入结构集中处理多源头数据;扇出结构则实现数据多目的地分发;复杂多层拓扑提供高度灵活性,适合多层次数据处理。通过灵活配置,Flume能够高效构建各种规模的数据收集系统。
33 0
|
2月前
|
存储 消息中间件 人工智能
AI大模型独角兽 MiniMax 基于阿里云数据库 SelectDB 版内核 Apache Doris 升级日志系统,PB 数据秒级查询响应
早期 MiniMax 基于 Grafana Loki 构建了日志系统,在资源消耗、写入性能及系统稳定性上都面临巨大的挑战。为此 MiniMax 开始寻找全新的日志系统方案,并基于阿里云数据库 SelectDB 版内核 Apache Doris 升级了日志系统,新系统已接入 MiniMax 内部所有业务线日志数据,数据规模为 PB 级, 整体可用性达到 99.9% 以上,10 亿级日志数据的检索速度可实现秒级响应。
AI大模型独角兽 MiniMax 基于阿里云数据库 SelectDB 版内核 Apache Doris 升级日志系统,PB 数据秒级查询响应
|
2月前
|
缓存 NoSQL Linux
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
|
1月前
|
JSON 缓存 fastjson
一行日志引发的系统异常
本文记录了一行日志引发的系统异常以及作者解决问题的思路。
|
8天前
使用装饰器实现自动化日志系统
使用装饰器实现自动化日志系统
11 0
|
2月前
|
API
Qt绘图之Paint系统
Qt绘图之Paint系统
41 2
|
2月前
|
监控 安全 Linux
在Linux中,某个账号登陆linux后,系统会在哪些日志文件中记录相关信息?
在Linux中,某个账号登陆linux后,系统会在哪些日志文件中记录相关信息?
|
2月前
|
运维 监控 Ubuntu
在Linux中,如何查看系统日志文件?
在Linux中,如何查看系统日志文件?
|
2月前
|
存储 消息中间件 监控
Java日志详解:日志级别,优先级、配置文件、常见日志管理系统ELK、日志收集分析
Java日志详解:日志级别,优先级、配置文件、常见日志管理系统、日志收集分析。日志级别从小到大的关系(优先级从低到高): ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF 低级别的会输出高级别的信息,高级别的不会输出低级别的信息
|
2月前
从源码角度分析Qt元对象系统2
从源码角度分析Qt元对象系统
44 0
下一篇
无影云桌面