日志的级别
一般分五级:
ERROR(错误):此信息输出后,主体系统核心模块不能正常工作,需要修复才能正常工作。
WARN(警告):此信息输出后,系统一般模块存在问题,不影响系统运行。
INFO(通知):此信息输出后,主要是记录系统运行状态等关联信息。
DEBUG(调试):最细粒度的输出,除却上面各种情况后,你希望输出的相关信息,都可以在这里输出。
TRACE(跟踪):最细粒度的输出,除却上面各种情况后,你希望输出的相关信息,都可以在这里输出。
在本文实现的简单日志系统中简单的把日志输出到屏幕和文件,屏幕上的日志有说明是什么类型的日志。文件中会创建想应类型的文件夹。
//test.cpp
#include <iostream>
#include "lo_logs.hpp"
using namespace std;
int main()
{
/*
LoLogs::WriteLog(LOGTYPE_WARNING,"%s,%s,%s,%d\n","author : qingyizhg","this is warning log test",__FILE__,__LINE__);
LoLogs::WriteLog(LOGTYPE_WARNING,"editor : first log sys\n");
LoLogs::WriteLog(LOGTYPE_WARNING,"email : qingyizhu12138@163.com\n");
LoLogs::WriteLog(LOGTYPE_INFO,"%s,%s,%s,%d\n","author : qingyizhg","this is info log test",__FILE__,__LINE__);
LoLogs::WriteLog(LOGTYPE_INFO,"editor : first log sys\n");
LoLogs::WriteLog(LOGTYPE_INFO,"email : qingyizhu12138@163.com\n");
LoLogs::WriteLog(LOGTYPE_DEBUG,"%s,%s,%s,%d\n","author : qingyizhg","this is debug log test",__FILE__,__LINE__);
LoLogs::WriteLog(LOGTYPE_DEBUG,"editor : first log sys\n");
LoLogs::WriteLog(LOGTYPE_DEBUG,"email : qingyizhu12138@163.com\n");
LoLogs::WriteLog(LOGTYPE_TRACE,"%s,%s,%s,%d\n","author : qingyizhg","this is trace log test",__FILE__,__LINE__);
LoLogs::WriteLog(LOGTYPE_TRACE,"editor : first log sys\n");
LoLogs::WriteLog(LOGTYPE_TRACE,"email : qingyizhu12138@163.com\n");
LoLogs::WriteLog(LOGTYPE_FATAL,"%s,%s,%s,%d\n","author : qingyizhg","this is fatal log test",__FILE__,__LINE__);
LoLogs::WriteLog(LOGTYPE_FATAL,"editor : first log sys\n");
LoLogs::WriteLog(LOGTYPE_FATAL,"email : qingyizhu12138@163.com\n");
*/
Q_WAR("%s,%s,%s,%d\n","author : qingyizhg","this is warning log test",__FILE__,__LINE__);
Q_WAR("editor : first log sys\n");
Q_WAR("email : qingyizhu12138@163.com\n");
Q_INF("%s,%s,%s,%d\n","author : qingyizhg","this is warning log test",__FILE__,__LINE__);
Q_INF("editor : first log sys\n");
Q_INF("email : qingyizhu12138@163.com\n");
Q_DEB("%s,%s,%s,%d\n","author : qingyizhg","this is warning log test",__FILE__,__LINE__);
Q_DEB("editor : first log sys\n");
Q_DEB("email : qingyizhu12138@163.com\n");
Q_ERR("%s,%s,%s,%d\n","author : qingyizhg","this is warning log test",__FILE__,__LINE__);
Q_ERR("editor : first log sys\n");
Q_ERR("email : qingyizhu12138@163.com\n");
Q_TRA("%s,%s,%s,%d\n","author : qingyizhg","this is warning log test",__FILE__,__LINE__);
Q_TRA("editor : first log sys\n");
Q_TRA("email : qingyizhu12138@163.com\n");
#ifdef WIN32
system("pause");
#endif
return 0;
}
运行结果:
并会生成一个logs的文件,里面保存着log日志,树形结构如下所示
./logs/
├── debug
│ └── debug2021-08-09.log
├── error
│ └── error2021-08-09.log
├── info
│ └── info2021-08-09.log
├── trace
│ └── trace2021-08-09.log
└── warning
└── warning2021-08-09.log
使用方法:
git clone https://github.com/qingyiz/q_log.git
cd q_log
make
./test
github地址 :https://github.com/qingyiz/q_log