forked from lisongmin/zlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsersGuide-CN.lyx
7535 lines (5869 loc) · 153 KB
/
UsersGuide-CN.lyx
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#LyX 2.0 created this file. For more info see http://www.lyx.org/
\lyxformat 413
\begin_document
\begin_header
\textclass extbook
\begin_preamble
\usepackage[cm-default]{fontspec}
\usepackage{xunicode}
\usepackage[utf8]{inputenc}
\usepackage{xltxtra}
\setmainfont[ItalicFont=KaiTi]{SimSun}
%\setmainfont[ItalicFont=KaiTi]{YaHei Consolas Hybrid}
%\setmainfont[BoldFont=KaiTi,ItalicFont=KaiTi]{SimSun}
%\setsansfont[BoldFont=KaiTi]{KaiTi}
%\setmonofont{KaiTi}
%其他中文设置%
\XeTeXlinebreaklocale “zh”%中文断行
\XeTeXlinebreakskip = 0pt plus 1pt minus 0.1pt%左右弹性间距
\usepackage{indentfirst}%段落首行缩进
\setlength{\parindent}{2em}%缩进两个字符
%编号语言、样式设置%
\numberwithin{equation}{section}%设置公式按章节进行编号
\numberwithin{figure}{section}% 按章节编号
\end_preamble
\use_default_options true
\begin_modules
theorems-ams
eqs-within-sections
figs-within-sections
\end_modules
\maintain_unincluded_children false
\language chinese-simplified
\language_package default
\inputencoding utf8-plain
\fontencoding global
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize 12
\spacing single
\use_hyperref true
\pdf_title "zlog Users Guide CN"
\pdf_author "Hardy Simpson"
\pdf_bookmarks true
\pdf_bookmarksnumbered true
\pdf_bookmarksopen true
\pdf_bookmarksopenlevel 1
\pdf_breaklinks true
\pdf_pdfborder true
\pdf_colorlinks true
\pdf_backref false
\pdf_pdfusetitle true
\pdf_quoted_options "unicode=false"
\papersize a4paper
\use_geometry true
\use_amsmath 1
\use_esint 1
\use_mhchem 1
\use_mathdots 1
\cite_engine basic
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\use_refstyle 0
\index 索引
\shortcut idx
\color #008000
\end_index
\rightmargin 2cm
\secnumdepth 2
\tocdepth 2
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 2
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header
\begin_body
\begin_layout Title
zlog
\begin_inset Foot
status collapsed
\begin_layout Plain Layout
星星之火,可以燎原——毛泽东
\end_layout
\end_inset
使用手册
\end_layout
\begin_layout Author
难易 著
\begin_inset Foot
status collapsed
\begin_layout Plain Layout
适用于 zlog v1.2.*
\end_layout
\end_inset
\begin_inset Foot
status collapsed
\begin_layout Plain Layout
有问题,在github上
\begin_inset CommandInset href
LatexCommand href
name "开个issue"
target "https://github.com/HardySimpson/zlog/issues/new"
type "mailto:"
\end_inset
,或者写邮件到
\begin_inset CommandInset href
LatexCommand href
name "[email protected]"
target "[email protected]"
type "mailto:"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset CommandInset toc
LatexCommand tableofcontents
\end_inset
\end_layout
\begin_layout Chapter
zlog是什么?
\end_layout
\begin_layout Standard
zlog是一个高可靠性、高性能、线程安全、灵活、概念清晰的纯C日志函数库。
\end_layout
\begin_layout Standard
事实上,在C的世界里面没有特别好的日志函数库(就像JAVA里面的的log4j,或者C++的log4cxx)。C程序员都喜欢用自己的轮子。printf就是个挺好的
轮子,但没办法通过配置改变日志的格式或者输出文件。syslog是个系统级别的轮子,不过速度慢,而且功能比较单调。
\end_layout
\begin_layout Standard
所以我写了zlog。
\end_layout
\begin_layout Standard
zlog在效率、功能、安全性上大大超过了log4c,并且是用c写成的,具有比较好的通用性。
\end_layout
\begin_layout Standard
zlog有这些特性:
\end_layout
\begin_layout Itemize
syslog分类模型,比log4j模型更加直接了当
\end_layout
\begin_layout Itemize
日志格式定制,类似于log4j的pattern layout
\end_layout
\begin_layout Itemize
多种输出,包括动态文件、静态文件、stdout、stderr、syslog、用户自定义输出函数
\end_layout
\begin_layout Itemize
运行时手动、自动刷新配置文件(同时保证安全)
\end_layout
\begin_layout Itemize
高性能,在我的笔记本上达到25万条日志每秒, 大概是syslog(3)配合rsyslogd的1000倍速度
\end_layout
\begin_layout Itemize
用户自定义等级
\end_layout
\begin_layout Itemize
多线程和多进程环境下保证安全转档
\end_layout
\begin_layout Itemize
精确到微秒
\end_layout
\begin_layout Itemize
简单调用包装dzlog(一个程序默认只用一个分类)
\end_layout
\begin_layout Itemize
MDC,线程键-值对的表,可以扩展用户自定义的字段
\end_layout
\begin_layout Itemize
自诊断,可以在运行时输出zlog自己的日志和配置状态
\end_layout
\begin_layout Itemize
不依赖其他库,只要是个POSIX系统就成(当然还要一个C99兼容的vsnprintf)
\end_layout
\begin_layout Standard
相关链接:
\end_layout
\begin_layout Standard
主页:
\begin_inset CommandInset href
LatexCommand href
name "http://hardysimpson.github.com/zlog/"
target "http://hardysimpson.github.com/zlog/"
\end_inset
\end_layout
\begin_layout Standard
下载:
\begin_inset CommandInset href
LatexCommand href
target "https://github.com/HardySimpson/zlog/releases"
\end_inset
\end_layout
\begin_layout Standard
邮箱:
\begin_inset CommandInset href
LatexCommand href
name "[email protected]"
target "[email protected]"
type "mailto:"
\end_inset
\end_layout
\begin_layout Section
兼容性说明
\end_layout
\begin_layout Enumerate
zlog是基于POSIX的。目前我手上有的环境只有AIX和linux。在其他的系统下(FreeBSD, NetBSD, OpenBSD, OpenSolaris
, Mac OS X...)估计也能行,有问题欢迎探讨。
\end_layout
\begin_layout Enumerate
zlog使用了一个C99兼容的vsnprintf。也就是说如果缓存大小不足,vsnprintf将会返回目标字符串应有的长度(不包括'
\backslash
0')。如果在你的系统上vsnprintf不是这么运作的,zlog就不知道怎么扩大缓存。如果在目标缓存不够的时候vsnprintf返回-1,zlog就会认为这次
写入失败。幸运的是目前大多数c标准库符合C99标准。glibc 2.1,libc on AIX, libc on freebsd...都是好的,不过glibc2.0不是。
在这种情况下,用户需要自己来装一个C99兼容的vsnprintf,来crack这个函数库。我推荐
\begin_inset CommandInset href
LatexCommand href
name "ctrio"
target "http://sourceforge.net/projects/ctrio/"
\end_inset
, 或者
\begin_inset CommandInset href
LatexCommand href
name "C99-snprintf"
target "http://www.jhweiss.de/software/snprintf.html"
\end_inset
。只要改buf.c就行,祝好运!
\end_layout
\begin_layout Enumerate
有网友提供了如下版本,方便其他平台上安装编译,非常感谢!
\end_layout
\begin_deeper
\begin_layout Standard
auto tools版本:
\begin_inset CommandInset href
LatexCommand href
target "https://github.com/bmanojlovic/zlog"
\end_inset
\end_layout
\begin_layout Standard
cmake版本:
\begin_inset CommandInset href
LatexCommand href
target "https://github.com/lisongmin/zlog"
\end_inset
\end_layout
\begin_layout Standard
windows版本:
\begin_inset CommandInset href
LatexCommand href
target "https://github.com/lopsd07/WinZlog"
\end_inset
\end_layout
\end_deeper
\begin_layout Section
zlog 1.2 发布说明
\end_layout
\begin_layout Enumerate
zlog 1.2 新增了这些功能
\end_layout
\begin_deeper
\begin_layout Enumerate
对管道的支持,从此zlog可以外接cronolog这样的日志过滤程序来输出
\end_layout
\begin_layout Enumerate
全面的日志转档支持,详见
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:文件转档"
\end_inset
\end_layout
\begin_layout Enumerate
其他兼容性的代码改动
\end_layout
\end_deeper
\begin_layout Enumerate
zlog 1.2 在库方面是和zlog 1.0/1.1二进制兼容的,区别在于:
\end_layout
\begin_deeper
\begin_layout Enumerate
所有的宏改为小写,ZLOG_INFO->zlog_info,方便开发者手工输入。这是一个巨大的改变,如果zlog1.1/1.0的用户要用zlog 1.2的话,需要写一
个脚本,把源代码中的大写批量替换为小写,然后重新编译你的程序。我提供了一个脚本:
\end_layout
\begin_deeper
\begin_layout LyX-Code
sed -i -e 's/
\backslash
b
\backslash
w*ZLOG
\backslash
w*
\backslash
b/
\backslash
L&
\backslash
E/g' aa.c
\end_layout
\end_deeper
\begin_layout Enumerate
取消了auto tools的使用,也就是说,不论你在任何平台,都需要gcc和gnu make才能编译安装zlog。主流的操作系统(Aix, OpenSolari
s..)都能安装gcc和gnu make。当然也可以自行修改makefile来完成编译,对于平台稍有经验的Geek都可以自行完成!
\end_layout
\end_deeper
\begin_layout Chapter
zlog不是什么?
\end_layout
\begin_layout Standard
zlog的目标是成为一个简而精的日志函数库,不会直接支持网络输出或者写入数据库,不会直接支持日志内容的过滤和解析。
\end_layout
\begin_layout Standard
原因很明显,日志库是被应用程序调用的,所有花在日志库上的时间都是应用程序运行时间的一部分,而上面说的这些操作都很费时间,会拖慢应用程序的速度。这些事儿应该在别的
进程或者别的机器上做。
\end_layout
\begin_layout Standard
如果你需要这些特性,我建议使用rsyslog、zLogFabric、Logstash,这些日志搜集、过滤、存储软件,当然这是单独的进程,不是应用程序的一部分。
\end_layout
\begin_layout Standard
目前zlog已经支持
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:用户自定义输出"
\end_inset
,可以自己实现一个输出函数,自由的把日志输出到其他进程或者其他机器。而把日志的分类匹配、日志格式成型的工作交给zlog。
\end_layout
\begin_layout Standard
目前我的想法是实现一个zlog-redis客户端,用自定义输出功能,把日志存储到本机或者远程的redis服务器内,然后用其他进程(也使用zlog库)来把日志写到
文件里面,不知大家以为这个想法如何?欢迎和我联系探讨。
\end_layout
\begin_layout Chapter
Hello World
\end_layout
\begin_layout Section
编译和安装zlog
\end_layout
\begin_layout Standard
下载
\begin_inset CommandInset href
LatexCommand href
name "zlog-latest-stable.tar.gz"
target "https://github.com/HardySimpson/zlog/archive/latest-stable.tar.gz"
type "file:"
\end_inset
\end_layout
\begin_layout LyX-Code
$ tar -zxvf zlog-latest-stable.tar.gz
\end_layout
\begin_layout LyX-Code
$ cd zlog-latest-stable/
\end_layout
\begin_layout LyX-Code
$ make
\end_layout
\begin_layout LyX-Code
$ sudo make install
\end_layout
\begin_layout LyX-Code
or
\end_layout
\begin_layout LyX-Code
$ sudo make PREFIX=/usr/local/ install
\end_layout
\begin_layout Standard
PREFIX指明了安装的路径,安转完之后为了让你的程序能找到zlog动态库
\end_layout
\begin_layout LyX-Code
$ sudo vi /etc/ld.so.conf
\end_layout
\begin_layout LyX-Code
/usr/local/lib
\end_layout
\begin_layout LyX-Code
$ sudo ldconfig
\end_layout
\begin_layout Standard
在你的程序运行之前,保证libzlog.so在系统的动态链接库加载器可以找到的目录下。上面的命令适用于linux,别的系统自己想办法。
\end_layout
\begin_layout Itemize
除了一般的make以外,还可以
\end_layout
\begin_layout LyX-Code
$ make 32bit # 32bit version on 64bit machine, libc6-dev-i386 is needed
\end_layout
\begin_layout LyX-Code
$ make noopt # without gcc optimization
\end_layout
\begin_layout LyX-Code
$ make doc # lyx and hevea is needed
\end_layout
\begin_layout LyX-Code
$ make test # test code, which is also good example for zlog
\end_layout
\begin_layout Itemize
makefile是用GNU make的格式写的,所以在你的平台上需要预装gnu make和gcc。或者,手工修改一个自己平台的makefile也行。
\end_layout
\begin_layout Section
应用程序调用和链接zlog
\end_layout
\begin_layout Standard
应用程序使用zlog很简单,只要在C文件里面加一行。
\end_layout
\begin_layout LyX-Code
#include "zlog.h"
\end_layout
\begin_layout Standard
链接zlog需要pthread库,命令是:
\end_layout
\begin_layout LyX-Code
$ cc -c -o app.o app.c -I/usr/local/include
\end_layout
\begin_layout LyX-Code
# -I[where zlog.h is put]
\end_layout
\begin_layout LyX-Code
$ cc -o app app.o -L/usr/local/lib -lzlog -lpthread
\end_layout
\begin_layout LyX-Code
# -L[where libzlog.so is put]
\end_layout
\begin_layout Section
Hello World 代码
\begin_inset CommandInset label
LatexCommand label
name "sec:Hello-World-代码"
\end_inset
\end_layout
\begin_layout Standard
这些代码在$(top_builddir)/test/test_hello.c, test_hello.conf
\end_layout
\begin_layout Enumerate
写一个C文件:
\end_layout
\begin_deeper
\begin_layout LyX-Code
$ vi test_hello.c
\end_layout
\begin_layout LyX-Code
#include <stdio.h>
\end_layout
\begin_layout LyX-Code
#include "zlog.h"
\end_layout
\begin_layout LyX-Code
\end_layout
\begin_layout LyX-Code
int main(int argc, char** argv)
\end_layout
\begin_layout LyX-Code
{
\end_layout
\begin_deeper
\begin_layout LyX-Code
int rc;
\end_layout
\begin_layout LyX-Code
zlog_category_t *c;
\end_layout
\begin_layout LyX-Code
\end_layout
\begin_layout LyX-Code
rc = zlog_init("test_hello.conf");
\end_layout
\begin_layout LyX-Code
if (rc) {
\end_layout
\begin_deeper
\begin_layout LyX-Code
printf("init failed
\backslash
n");
\end_layout
\begin_layout LyX-Code
return -1;
\end_layout
\end_deeper
\begin_layout LyX-Code
}
\end_layout
\begin_layout LyX-Code
\end_layout
\begin_layout LyX-Code
c = zlog_get_category("my_cat");
\end_layout
\begin_layout LyX-Code
if (!c) {
\end_layout
\begin_layout LyX-Code
printf("get cat fail
\backslash
n");
\end_layout
\begin_deeper
\begin_layout LyX-Code
zlog_fini();
\end_layout
\begin_layout LyX-Code
return -2;
\end_layout
\end_deeper
\begin_layout LyX-Code
}
\end_layout
\begin_layout LyX-Code
\end_layout
\begin_layout LyX-Code
zlog_info(c, "hello, zlog");
\end_layout
\begin_layout LyX-Code
zlog_fini();
\end_layout
\begin_layout LyX-Code
return 0;
\end_layout
\end_deeper
\begin_layout LyX-Code
}
\end_layout
\end_deeper
\begin_layout Enumerate
写一个配置文件,放在和test_hello.c同样的目录下:
\end_layout
\begin_deeper
\begin_layout LyX-Code
$ vi test_hello.conf
\end_layout
\begin_layout LyX-Code
[formats]
\end_layout
\begin_layout LyX-Code
simple = "%m%n"
\end_layout
\begin_layout LyX-Code
[rules]
\end_layout
\begin_layout LyX-Code
my_cat.DEBUG >stdout; simple
\end_layout
\end_deeper
\begin_layout Enumerate
编译、然后运行!
\end_layout
\begin_deeper
\begin_layout LyX-Code
$ cc -c -o test_hello.o test_hello.c -I/usr/local/include
\end_layout
\begin_layout LyX-Code
$ cc -o test_hello test_hello.o -L/usr/local/lib -lzlog
\end_layout
\begin_layout LyX-Code
$ ./test_hello
\end_layout
\begin_layout LyX-Code
hello, zlog
\end_layout
\end_deeper
\begin_layout Section
更简单的Hello World
\end_layout
\begin_layout Standard
这个例子在$(top_builddir)/test/test_default.c, test_default.conf.
源代码是:
\end_layout
\begin_layout LyX-Code
#include <stdio.h>
\end_layout
\begin_layout LyX-Code
#include "zlog.h"
\end_layout
\begin_layout LyX-Code
int main(int argc, char** argv)
\end_layout
\begin_layout LyX-Code
{
\end_layout
\begin_deeper
\begin_layout LyX-Code
int rc;
\end_layout
\begin_layout LyX-Code
rc = dzlog_init("test_default.conf", "my_cat");
\end_layout
\begin_layout LyX-Code
if (rc) {
\end_layout
\begin_deeper
\begin_layout LyX-Code
printf("init failed
\backslash
n");
\end_layout
\begin_layout LyX-Code
return -1;
\end_layout
\end_deeper
\begin_layout LyX-Code
}
\end_layout
\begin_layout LyX-Code
dzlog_info("hello, zlog");
\end_layout
\begin_layout LyX-Code
zlog_fini();
\end_layout
\begin_layout LyX-Code
return 0;
\end_layout
\end_deeper
\begin_layout LyX-Code
}
\end_layout
\begin_layout Standard
配置文件是test_default.conf,和test_hello.conf一模一样,最后执行程序的输出也一样。区别在于这里用了dzlog API,内含一个默认的
zlog_category_t。详见
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:dzlog接口"
\end_inset
。
\end_layout
\begin_layout Chapter
Syslog 模型
\end_layout
\begin_layout Section
分类(Category)、规则(Rule)和格式(Format)
\end_layout
\begin_layout Standard
zlog有3个重要的概念:分类(Category)、规则(Rule)和格式(Format)。
\end_layout
\begin_layout Standard
分类(Category)用于区分不同的输入。代码中的分类变量的名字是一个字符串,在一个程序里面可以通过获取不同的分类名的category用来后面输出不同分类的日
志,用于不同的目的。
\end_layout
\begin_layout Standard
格式(Format)是用来描述输出日志的格式,比如是否有带有时间戳,是否包含文件位置信息等,上面的例子里面的格式simple就是简单的用户输入的信息+换行符。
\end_layout
\begin_layout Standard
规则(Rule)则是把分类、级别、输出文件、格式组合起来,决定一条代码中的日志是否输出,输出到哪里,以什么格式输出。
\end_layout
\begin_layout Standard
所以,当程序执行下面的语句的时候
\end_layout
\begin_layout LyX-Code
zlog_category_t *c;
\end_layout
\begin_layout LyX-Code
c = zlog_get_category("my_cat");
\end_layout
\begin_layout LyX-Code
zlog_info(c, "hello, zlog");
\end_layout
\begin_layout Standard
zlog会找到c的名字是"my_cat",对应的配置文件中的规则是
\end_layout
\begin_layout LyX-Code
[rules]
\end_layout
\begin_layout LyX-Code
my_cat.DEBUG >stdout; simple
\end_layout
\begin_layout Standard
然后库会检查,目前这条日志的级别是否符合规则中的级别来决定是否输出。因为INFO>=DEBUG,所以这条日志会被输出。并且根据这条规则,会被输出到stdout(
标准输出) ,输出的格式是simple,在配置文件中定义是
\end_layout
\begin_layout LyX-Code
[formats]
\end_layout
\begin_layout LyX-Code
simple = "%m%n"
\end_layout
\begin_layout Standard
最后在屏幕上打印
\end_layout
\begin_layout LyX-Code
hello, zlog
\end_layout
\begin_layout Standard
这就是整个过程。用户要做就是写自己的信息。日志往哪里输出,以什么格式输出,都是库和配置文件来完成的。
\end_layout
\begin_layout Section
syslog模型和log4j模型的区别
\end_layout
\begin_layout Standard
好,那么目前这个模型和syslog有什么关系呢?至今为止,这个模型还是比较像log4j。log4j的模型里面有logger, appender和layout。区
别在于,在log4j里面,代码中的logger和配置中的logger是一一对应的,并且一个logger有唯一的级别。一对一关系是log4j, log4cxx,
log4cpp, log4cplus, log4net的唯一选择。
\end_layout
\begin_layout Standard
但这种模型是不灵活的,他们发明了过滤器(filters)来弥补,但这只能把事情弄得更加混乱。所以让我们把目光转回syslog的模型,这是一个设计的很简易正确的模
型。
\end_layout
\begin_layout Standard
继续上一节的例子,如果在zlog的配置文件中有这么2行规则:
\end_layout
\begin_layout LyX-Code
[rules]
\end_layout
\begin_layout LyX-Code
my_cat.DEBUG >stdout; simple
\end_layout
\begin_layout LyX-Code
my_cat.INFO >stdout;
\end_layout
\begin_layout Standard
然后,一行代码会产生两行输出:
\end_layout
\begin_layout LyX-Code
hello, zlog
\end_layout
\begin_layout LyX-Code
2012-05-29 10:41:36 INFO [11288:test_hello.c:41] hello, zlog
\end_layout
\begin_layout Standard
现在一个代码中的分类对应配置文件中的两条规则。log4j的用户可能会说:"这很好,但是只要在log4j里面放两个appender也能做的一样。"所以继续看下一个
例子:
\end_layout
\begin_layout LyX-Code
[rules]
\end_layout
\begin_layout LyX-Code
my_cat.WARN "/var/log/aa.log"
\end_layout
\begin_layout LyX-Code
my_cat.DEBUG "/var/log/bb.log"
\end_layout
\begin_layout Standard
代码是:
\end_layout
\begin_layout LyX-Code
zlog_info(c, "info, zlog");
\end_layout
\begin_layout LyX-Code
zlog_debug(c, "debug, zlog");
\end_layout
\begin_layout Standard
最后,在aa.log中只有一条日志
\end_layout
\begin_layout LyX-Code
2012-05-29 10:41:36 INFO [11288:test_hello.c:41] info, zlog
\end_layout
\begin_layout Standard
但在bb.log里面有两条
\end_layout
\begin_layout LyX-Code
2012-05-29 10:41:36 INFO [11288:test_hello.c:41] info, zlog
\end_layout