myloglog 打印和分析工具
mylog 是一個(gè)多線程安全、高效、易用性很強(qiáng)的 C/C++ 庫(kù) .只需要一個(gè)初始化日志目錄以及文件名,你就可以像使用 printf 一樣的去打印日志。通過日志可以定位(多線程)請(qǐng)求 ip, logid,執(zhí)行時(shí)間等。
1 說明
1) 本文件夾包含源碼src以及示例文件sample
2) 編譯src生成output(包括i?nclude和lib)
3) 使用output的lib和include,具體可以查看sample的Makefile和code
4) 日志級(jí)別為FATAL, WARNING, NOTICE, TRACE, DEBUG,以此級(jí)別變低
5) 使用MY_LOG_FATAL等打印日志,和printf使用方式類似,非常簡(jiǎn)單。
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 : 日志級(jí)別
2) 初始化線程日志數(shù)據(jù)
my_log_thread_init()
多線程使用
3) 設(shè)置一個(gè)線程的logid
my_log_set_logid(logid)
必須在my_log_thread_init() 之后使用。
4) 設(shè)置一個(gè)線程的reqip
my_log_set_reqip(reqip)
必須在my_log_thread_init() 之后使用.
5) 設(shè)置一個(gè)線程的reqip
my_log_set_mod(mod)
設(shè)置一個(gè)線程的reqip, 必須在單線程中使用或者my_log_thread_init() 之后使用.
6)設(shè)置計(jì)算執(zhí)行時(shí)間的類型(打印時(shí)間是ms還是us)
my_log_set_time_type(time_type)
必須在單線程中使用或者my_log_thread_init() 之后使用.
7) 打印FATAL日志
MY_LOG_FATAL(logfmt, arg...)
日記級(jí)別 >=1會(huì)打印 FATAL日志。
8)打印WARNNING日志
MY_LOG_WARNING(logfmt, arg...)
日記級(jí)別 >=2會(huì)打印 WARNING日志。
9) 打印NOTICE日志
MY_LOG_NOTICE(logfmt, arg...)
日記級(jí)別 >=4會(huì)打印 NOTICE日志。
10)打印TRACE日志
MY_LOG_TRACE(logfmt, arg...)
日記級(jí)別 >=8會(huì)打印TRACE日志。
11) 打印DEBUG日志
MY_LOG_DEBUG(logfmt, arg...)
日記級(jí)別 >=16會(huì)打印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)運(yùn)行結(jié)果
