myloglog 打印和分析工具

联合创作 · 2023-09-30 00:40

mylog 是一个多线程安全、高效、易用性很强的 C/C++ 库 .只需要一个初始化日志目录以及文件名,你就可以像使用 printf 一样的去打印日志。通过日志可以定位(多线程)请求 ip, logid,执行时间等。


1 说明
1) 本文件夹包含源码src以及示例文件sample
2) 编译src生成output(包括iånclude和lib)
3) 使用output的lib和include,具体可以查看sample的Makefile和code
4) 日志级别为FATAL, WARNING, NOTICE, TRACE, DEBUG,以此级别变低
5) 使用MY_LOG_FATAL等打印日志,和printf使用方式类似,非常简单。


2 使用API(查看mylog.h)


1) 初始化日志目录


my_log_init(const char* log_path, const char* normal_path, const char* warn_fatal_path, const int log_level) 

log_path : log路径 normal_path : 正常日志目录 warn_fatal_path : 异常日志目录 log_level : 日志级别


2) 初始化线程日志数据


  my_log_thread_init() 

多线程使用


3) 设置一个线程的logid


my_log_set_logid(logid) 

必须在my_log_thread_init() 之后使用。


4) 设置一个线程的reqip 


my_log_set_reqip(reqip) 

必须在my_log_thread_init() 之后使用.


5) 设置一个线程的reqip


my_log_set_mod(mod) 

设置一个线程的reqip, 必须在单线程中使用或者my_log_thread_init() 之后使用.


6)设置计算执行时间的类型(打印时间是ms还是us)


 my_log_set_time_type(time_type) 

必须在单线程中使用或者my_log_thread_init() 之后使用.


7) 打印FATAL日志


MY_LOG_FATAL(logfmt, arg...) 

日记级别 >=1会打印 FATAL日志。


8)打印WARNNING日志


MY_LOG_WARNING(logfmt, arg...) 

日记级别 >=2会打印 WARNING日志。


9) 打印NOTICE日志


MY_LOG_NOTICE(logfmt, arg...) 

日记级别 >=4会打印 NOTICE日志。


10)打印TRACE日志


MY_LOG_TRACE(logfmt, arg...) 

日记级别 >=8会打印TRACE日志。


11) 打印DEBUG日志



MY_LOG_DEBUG(logfmt, arg...) 


日记级别 >=16会打印DEBUG日志。


3 范例


1) code


 


#include "mylog.h"


void* test_thread1(void*)
{
my_log_thread_init();
my_log_set_reqip("10.10.10.31");
my_log_set_time_type(TIME_TYPE_MSEC);
for(int i=0; i<100; i++)
{
my_log_set_logid(i);
MY_LOG_FATAL("thread 1 fatal is at %d, it's %s", i , "OK");
MY_LOG_WARNNING("thread 1 warning is at %d, it's %s", i , "OK");
MY_LOG_NOTICE("thread 1 notice is at %d, it's %s", i , "OK");
MY_LOG_TRACE("thread 1 trace is at %d, it's %s", i , "OK");
MY_LOG_DEBUG("thread 1 debug is at %d, it's %s", i , "OK");
sleep(1);

}
}


void* test_thread2(void*)
{
my_log_thread_init();
for(int i=0; i<3; i++)
{
my_log_set_mod("test2");
MY_LOG_FATAL("thread 2 fatal is at %d, it's %s", i , "OK");
MY_LOG_WARNNING("thread 2 warning is at %d, it's %s", i , "OK");
MY_LOG_NOTICE("thread 2 notice is at %d, it's %s", i , "OK");
MY_LOG_TRACE("thread 2 trace is at %d, it's %s", i , "OK");
MY_LOG_DEBUG("thread 2 debug is at %d, it's %s", i , "OK");
sleep(1);
}
}


int main()
{
my_log_init(".", "test.log", "test.log.wf", 16);
MY_LOG_DEBUG("main begin");


pthread_t t1, t2;
pthread_create(&t1, NULL, test_thread1, NULL);
pthread_create(&t2, NULL, test_thread2, NULL);


pthread_join(t1, NULL);
pthread_join(t2, NULL);
MY_LOG_DEBUG("main end");
}

 


 


 


2)运行结果


浏览 20
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报