转载自:http://www.cppblog.com/pizzx/archive/2014/06/18/207320.html
GLOG版本0.3.3 google开源的一个c++日志库,很小巧,使用也很方便。
1.
日志文件名格式为:
basename+时间搓(年月日-HH:MM:SS.主线程ID)
要为不同级别日志文件设置不同的basename,不能相同。
google::SetLogDestination(google::INFO,"c:\\log");
注:这里log是文件名的一部分,并不是指定文件夹log。最终日志文件名类似:log20140611-095200.9520
2.
程序每次重新启动后,会重新生成新的日志文件。
3.
日志是分级别存放的,低级别的日志文件包含高级别的日志信息。
如INFO级别的日志包含其他高级别的所有日志,ERROR级别的日志只包含ERROR和FATAL两个级别。
4.
LOG(LEVEL)使用方式是线程安全。
缺点:
1. 没有回转覆写功能,日志文件一直增长,需要其他方式清理过期日志?
=========================================
sample:测试多线程写日志
1
#include
<
boost
/
array.hpp
>
2
#include
<
boost
/
thread.hpp
>
3
#include
<
boost
/
bind.hpp
>
4
#include
<
iostream
>
5
6
7
#include
"
glog/logging.h
"
8
9
#ifdef _WIN32
10
11
#ifndef WIN32_LEAN_AND_MEAN
12
#define
WIN32_LEAN_AND_MEAN /* We always want minimal includes */
13
#endif
14
15
#include
<
windows.h
>
16
#endif
17
18
using
namespace
std;
19
20
21
22
void
thread1_test()
23
{
24
for (int i = 0; i< 1000; i++)
25
{
26
LOG(INFO)<<i;
27
//Sleep(1000);
28
}
29
}
30
31
void
thread2_test()
32
{
33
for (int i = 1000; i< 2000; i++)
34
{
35
LOG(INFO)<<i;
36
//Sleep(1000);
37
}
38
}
39
40
int
main(
int
argc,
char
**
argv)
41
{
42
google::InitGoogleLogging(argv[0]);
43
44
//为不同级别的日志设置不同的文件basename。
45
google::SetLogDestination(google::INFO,"D:\\glogfile\\loginfo");
46
google::SetLogDestination(google::WARNING,"D:\\glogfile\\logwarn");
47
google::SetLogDestination(google::GLOG_ERROR,"D:\\glogfile\\logerror");
48
49
//缓存的最大时长,超时会写入文件
50
FLAGS_logbufsecs = 60;
51
52
//单个日志文件最大,单位M
53
FLAGS_max_log_size =10;
54
55
//设置为true,就不会写日志文件了
56
FLAGS_logtostderr = false;
57
58
boost::thread t1(boost::bind(&thread1_test));
59
boost::thread t2(boost::bind(&thread2_test));
60
61
t1.join();
62
t2.join();
63
64
//LOG(FATAL)<<"exit";
65
66
google::ShutdownGoogleLogging();
67
68
return 0;
69
}
#include
<
boost
/
array.hpp
>
2
#include
<
boost
/
thread.hpp
>
3
#include
<
boost
/
bind.hpp
>
4
#include
<
iostream
>
5

6

7
#include
"
glog/logging.h
"
8

9
#ifdef _WIN3210

11
#ifndef WIN32_LEAN_AND_MEAN12
#define
WIN32_LEAN_AND_MEAN /* We always want minimal includes */
13
#endif
14

15
#include
<
windows.h
>
16
#endif
17

18
using
namespace
std;19

20

21

22
void
thread1_test()23
{24
for (int i = 0; i< 1000; i++)25
{26
LOG(INFO)<<i;27
//Sleep(1000); 28
}29
}
30

31
void
thread2_test()32
{33
for (int i = 1000; i< 2000; i++)34
{35
LOG(INFO)<<i; 36
//Sleep(1000); 37
}38
}
39

40
int
main(
int
argc,
char
**
argv)41
{42
google::InitGoogleLogging(argv[0]);43

44
//为不同级别的日志设置不同的文件basename。45
google::SetLogDestination(google::INFO,"D:\\glogfile\\loginfo"); 46
google::SetLogDestination(google::WARNING,"D:\\glogfile\\logwarn"); 47
google::SetLogDestination(google::GLOG_ERROR,"D:\\glogfile\\logerror"); 48

49
//缓存的最大时长,超时会写入文件50
FLAGS_logbufsecs = 60; 51
52
//单个日志文件最大,单位M53
FLAGS_max_log_size =10;54

55
//设置为true,就不会写日志文件了56
FLAGS_logtostderr = false;57

58
boost::thread t1(boost::bind(&thread1_test));59
boost::thread t2(boost::bind(&thread2_test));60

61
t1.join();62
t2.join();63

64
//LOG(FATAL)<<"exit";65

66
google::ShutdownGoogleLogging();67

68
return 0;69
}
本文介绍了GLOG版本0.3.3的日志库特点,包括日志文件的命名规则、不同级别的日志如何配置、日志文件的生成机制及线程安全性等内容,并通过一个多线程写日志的例子展示了其使用方法。

1855

被折叠的 条评论
为什么被折叠?



