实现简易的log日志系统

该博客介绍了一个简单的日志系统实现,包括ERROR、WARN、INFO、DEBUG和TRACE五个级别。日志信息会被输出到屏幕和按级别分类存储在文件中,形成清晰的目录结构。用户可以通过调用预定义的宏进行不同级别的日志记录。提供了一个GitHub仓库链接以获取和编译代码。
摘要由CSDN通过智能技术生成

日志的级别
一般分五级:
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值