• 文章
  • 一个开源C++轻量级跨平台日志库
发布
2014年4月4日 (最后更新:2014年4月4日)

一个开源C++轻量级跨平台日志库:Log4z

评分:3.9/5 (75票)
*****

简介

Log4z是一个开源C++轻量级跨平台日志库。
它为C++应用程序提供日志和跟踪调试功能,适用于7*24小时服务的程序。

支持Debian、RedHat、CentOS、SUSE、Windows的64/32位系统。


特性

MIT开源许可证,非常宽松。

跨平台Linux和Windows,轻量级,仅需一个cpp源文件和一个头文件。

多日志输出,文件滚动,优先级过滤,线程安全。

屏幕显示日志,带有不同的颜色。

支持格式化和流式两种方式写入日志。

支持从文件配置并热更新,或者可以直接快速工作而无需任何配置。


文件名格式

LogName_年年月月日日时时分分_进程ID_块编号.log


E:\GITHUB\LOG4Z\PROJECT\BIN
│──advanced_test.exe
│──config.cfg
│──fast_test.exe
│──stress_test.exe
│
├─AdvacedLog
│──│───FileConfig_2013100921_003852_000.log
│──│───FileConfig_2013101313_005920_000.log
│
├─log
│──│──advanced_test_2013100921_003852_000.log
│──│──advanced_test_2013101313_005920_000.log
│──│──fast_test_2013100921_003855_000.log
│──│──fast_test_2013101313_006160_000.log
│──│──stress_test_2013101313_007196_000.log
│──│
│──└─2013_10
│─────│────Dynamic_2013100921_003852_000.log
│─────│────Dynamic_2013101313_005920_000.log
│
└─Stress
───│────NetWork_2013101313_007196_000.log
───│────NetWork_2013101313_007196_001.log
───│────NetWork_2013101313_007196_002.log
───│────NetWork_2013101313_007196_003.log
───│────NetWork_2013101313_007196_004.log

[日志内容]



如何使用Log4z 示例1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "../log4z.h"
#include <iostream>
#include <stdio.h>
#include <stdarg.h>
using namespace zsummer::log4z;

#ifdef WIN32
#include <windows.h>
#endif
int main(int argc, char *argv[])
{

	//start log4z
	ILog4zManager::GetInstance()->Start();

	LOGI("begin test stream log input....");
	LOGD("stream input *** " << "LOGD LOGD LOGD LOGD" << " *** ");

	LOGFMTI("begin test format log input....");
	LOGFMTD("format input *** %s *** %d ***", "LOGFMTD", 123456);
	LOGA("main quit ...");
	return 0;
}


如何使用Log4z 示例2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "../log4z.h"
#include <iostream>
#include <stdio.h>
#include <stdarg.h>
using namespace zsummer::log4z;

#ifdef WIN32
#include <windows.h>
#endif
int main(int argc, char *argv[])
{

	//start log4z
	ILog4zManager::GetInstance()->Start();

	//LOGD: LOG WITH level LOG_DEBUG
	//LOGI: LOG WITH level LOG_INFO
	//...
#ifdef WIN32
	LOGI("begin test stream log utf-16 string input....");
	WCHAR wChar[100] = L"check unicode log string";
	std::wstring wStr = L"check wstring log";
	LOGF(L"PATH=" << wChar << ":" << wStr);
#endif

	LOGI("begin test stream log input....");
	LOGD("stream input *** " << "LOGD LOGD LOGD LOGD" << " *** ");
	LOGI("stream input *** " << "LOGI LOGI LOGI LOGI" << " *** ");
	LOGW("stream input *** " << "LOGW LOGW LOGW LOGW" << " *** ");
	LOGE("stream input *** " << "LOGE LOGE LOGE LOGE" << " *** ");
	LOGA("stream input *** " << "LOGA LOGA LOGA LOGA" << " *** ");
	LOGF("stream input *** " << "LOGF LOGF LOGF LOGF" << " *** ");


	LOGI("begin test stream log all types input....");
	LOGD("char:" <<'c'
		<< ", unsigned char:" << (unsigned char) 'c'
		<< ", short:" << (short) -1
		<< ", unsigned short:" << (unsigned short) -1
		<< ", int:" << (int) -1
		<< ", unsigned int:" << (unsigned int) -1
		<< ", long:" << (long) -1
		<< ", unsigned long:" << (unsigned long) -1
		<< ", long long:" << (long long) -1
		<< ", unsigned long long:" << (unsigned long long) -1
		<< ", float:" << (float) -1.234567
		<< ", double:" << (double) -2.34566
		<< ", std::string:" << std::string("fffff")
		<< ", int *:" << ( int *) argv
		<< ", const int *:" << (const int *) argv
		<< ", constant:" << 1000 
		<< ", constant:" << 100.12345678
		<< ", bool:" << true
		<< ", show hex data:" << BinaryBlock("1234567890abcdefghigklmnopqrstuvwxyz_zyw_zsummer_log4z", 50));


	std::string str;
	str.resize(3000, 's');


	// cannot support VC6 or VS2003
	LOGFMTI("begin test format log big string more than buff size input....");
	LOGFMTI("begin test format log input....");
	LOGFMTD("format input *** %s *** %d ***", "LOGFMTD", 123456);
	LOGFMTI("format input *** %s *** %d ***", "LOGFMTI", 123456);
	LOGFMTW("format input *** %s *** %d ***", "LOGFMTW", 123456);
	LOGFMTE("format input *** %s *** %d ***", "LOGFMTE", 123456);
	LOGFMTA("format input *** %s *** %d ***", "LOGFMTA", 123456);
	LOGFMTF("format input *** %s *** %d ***", "LOGFMTF", 123456);
	LOGFMT_DEBUG(LOG4Z_MAIN_LOGGER_ID, "%s", str.c_str());
	// end

	LOGI("begin test stream log big string more than buff size input....");
	LOGD(str);
	LOGA("main quit ...");
	return 0;
}



如何编译


直接使用方式


将log4z.cpp和log4z.h文件添加到现有项目中一起编译。

制作库使用


在Windows上
 
Open and compile log4z/vc2005/log4z.vcproj


在Linux上
1
2
3
cd log4z/g++
cmake .
make


发布下载链接:https://github.com/zsummer/log4z/releases

附件:[log4z-2.5.0.zip]