本文主要是介绍seelog学习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
安装
日志类型
Synchronous(同步)
Asynchronous loop
Asynchronous timer
Asynchronour adaptive
格式化类型
消息上下文
日期和时间
特殊符号
预定义格式
接收者
file writer
console writer
rolling file writer (or rotation file writer)
formatid
filename
type
namemode
maxrolls
archivetype
archiveexploded
archivepath
Buffered writer
smtp writer
顶级元素:smtp
子元素
conn writer
调度器
splitter dispatcher
filter dispatcher
https://github.com/cihub/seelog/wiki#reference
安装
go get github.com/cihub/seelog
日志类型
是seelog元素的属性type,包含以下几种类型
Synchronous(同步)
在调用日志函数的同一个goroutine里面处理日志信息
属性值:sync
<seelog type="sync">...
</seelog>
Asynchronous loop
在单独的goroutine中处理日志消息,在“for”循环中从消息队列获取消息。
属性值:asyncloop(是seelog的type属性的默认值,可以忽略type属性)
<seelog>....
<seelog>与以下的配置是相同作用<seelog type="asyncloop">...
</seelog>
Asynchronous timer
在单独的goroutine里处理日志消息,根据指定的时间间隔从消息队列获取消息
属性值:asynctimer
额外属性:asyncinterval (属性值为纳秒)
<seelog type="asynctimer" astncinterval="5000">...
<seelog>
Asynchronour adaptive
类似于asynctimer,但是时间间隔取决于停留在消息队列中的消息数量。创建这种类型的日志是为了避免消息队列溢出:队列中的消息越多,获取消息的速度越快。
属性值:adaptive
额外属性:
mininterval:最小间隔(单位:纳秒)
maxinterval:最大间隔(单位:纳秒)
critmsgcount:关键消息数量
<seelog type="adaptive" mininterval="2000000" maxinterval="1000000000" critmsgcount="500"><outputs formatid="msg"><console/></outputs><formats><format id="msg" format="%Time: %Msg%n"/></formats>
</seelog>
格式化类型
消息上下文
%Level:日志级别log level(Trace, Debug, Info, Warn, Error, Critical)
%Lev: 简短日志级别short log level(Trc, Dbg, Inf, Wrn, Err, Crt)
%LEVEL:大写的日志级别capitalized log level(TRACE, DEBUG, INFO, WARN, ERROR, CRITICAL)
%LEV:简短的大写日志级别short capitalized log level:(TRC, DBG, INF, WRN, ERR, CRT)
%l:超小型日志级别super compact log level(t, d, i, w, e, c)
%Msg:消息文本
%FullPath:完整的调用文件路径
%File:调用文件名
%RelFile:相对于程序运行时目录的调用路径
%Func:调用方法名
%FuncShort:最后一个点后面的调用函数名部分
%Line:日志行号
日期和时间
%Ns:time.Now().UnixNano()
%Date:2006-01-02 格式的日期
%Time: 15:04:05的时间格式
%Date(...):日期格式,指定匹配模式。比如%Date(2006-01-02)
%UTCNs :time.Now().UTC().UnixNano()
%UTCDate: UTC格式的2006-01-02
%UTCTime:UTC格式的15:04:05
%UTCDate(...)
特殊符号
%EscN:terminal ANSI CSI n [;k] m escape
%n: 换行
%t:tab键
预定义格式
xml-debug: <time>%Ns</time><lev>%Lev</lev><msg>%Msg</msg><path>%RelFile</path><func>%Func</func>
xml-debug-short: <t>%Ns</t><l>%l</l><m>%Msg</m><p>%RelFile</p><f>%Func</f>
xml: <time>%Ns</time><lev>%Lev</lev><msg>%Msg</msg>
xml-short: <t>%Ns</t><l>%l</l><m>%Msg</m>
json-debug: {"time":%Ns,"lev":"%Lev","msg":"%Msg","path":"%RelFile","func":"%Func"}
json-debug-short: {"t":%Ns,"l":"%Lev","m":"%Msg","p":"%RelFile","f":"%Func"}
json: {"time":%Ns,"lev":"%Lev","msg":"%Msg"}
json-short: {"t":%Ns,"l":"%Lev","m":"%Msg"}
debug: [%LEVEL] %RelFile:%Func %Date %Time %Msg%n
debug-short: [%LEVEL] %Date %Time %Msg%n
fast: %Ns %l %Msg%n
接收者
file writer
把消息写入到文件
元素名:file
可选的属性:
formatid: 接收者将使用的格式
path:日志文件路径
<seelog><outputs><file path="log.log"></outputs>
</seelog>
1.文件名不能使用特殊符号
2.不要再多进程中使用相同的日志文件,避免日志不一致
console writer
把日志打印到控制台
元素名:console
属性名:formatid
<seelog><outputs><console/></outputs>
</seelog>
rolling file writer (or rotation file writer)
把日志写入到文件,直到日期变了或者文件大小超过了指定的限制。日志文件就会被重命名写入到一个新的日志文件。
如果按照文件大小,可以为这些重命名的文件设置数量限制,当文件数量超过指定的限制数量时,旧的滚动文件将会被删除。
元素名:rollingfile
支持的属性:
formatid
filename
type
可选值:
date
当type值为date的时候,可选的属性值:
datepattern:time.Now().Format()格式
fullname,bool类型,当为true,当前的文件名将会使用与滚动日志命名相同的规则。默认为false
size
当type类型为size ,可选的属性:maxsize,滚动文件大小限制(以字节为单位)
namemode
可选值
postfix:日志文件后缀(这个是默认值)
prefix:日志文件前缀
maxrolls
重命名文件的最大数量,当超过这个数量,旧的文件就会被删除,当然maxrolls》=0
archivetype
使用该属性来避免旧的文件被删除,而是使用特定的文件格式存下来。可选值:none, zip, gzip。当值为none的时候,旧的文件直接就被删掉。
archiveexploded
指定将日志分解还是分组到同一个归档文件中。
archivepath
当archivetype不是none的时候,指定旧的文件存放的路径
<seelog><outputs><rollingfile type="size" filename="logs/roll.log" maxsize="1000" maxrolls="5" /></outputs>
</seelog>
<seelog><outputs><rollingfile type="date" filename="logs/roll.log" datepattern="02.01.2006" maxrolls="7" /></outputs>
</seelog>
Buffered writer
缓冲区输出。把数据存在内存中,在刷新周期或者缓冲区满的时候刷新。
元素名:buffered
属性:formatid, size(缓冲区大小,字节为单位),flushperiod(缓冲刷新的间隔,毫秒为单位)
<seelog><outputs><buffered size="10000" flushperiod="1000"><file path="bufFile.log"/></buffered></outputs>
</seelog>
<seelog><outputs><buffered size="10000" flushperiod="1000" formatid="someFormat"><rollingfile type="date" filename="logs/roll.log" datepattern="02.01.2006" /></buffered></outputs><formats>...</formats>
</seelog>
smtp writer
在给定的邮件服务器上,使用密码保护的账号向指定的收件人发送邮件。
顶级元素:smtp
属性:
senderaddress:发件人地址
sendername:发件人名称
hostname:邮件服务器host
hostport:邮件服务器端口
username:登录邮件服务器的账号
password:登录邮件服务器的密码
subject:邮件主题
直接把账号密码写出来是不安全的。
子元素
recipient(接收者,收件人)
属性:address,收件人地址
header
属性:name , value
cacertdirpath
属性:path
<seelog><outputs><filter levels="error,critical"><smtp senderaddress="nns@none.org" sendername="ANS" hostname="mail.none.org" hostport="587" username="nns" password="123" subject="test"><cacertdirpath path="cacdp1"/><recipient address="hans-meier@none.com"/><header name="Priority" value="Urgent" /><header name="Importance" value="high" /><header name="Sensitivity" value="Company-Confidential" /><header name="Auto-Submitted" value="auto-generated" /></smtp></filter></outputs></seelog>
conn writer
把接收到的消息写入打网络连接里。
元素:conn
属性:
formatid
net("tcp", "udp", "tcp4", "udp4", ....)
addr:网络地址(":10000", "127.0.0.1:8888", ...)
reconnectonmsg:默认false:在首次写入时打开。true:连接在每次写入时打开。
usetls:默认false.true:使用TLS。
insecureskipverify:如果使用了useTLS,则设置tls.Config的InsecureSkipVerify标志。
<seelog type="sync"><outputs><conn net="tcp" addr=":8888" /></outputs>
</seelog>
<outputs><conn formatid="syslog" net="tcp4" addr="server.address:5514" tls="true" insecureskipverify="true" />
</outputs>
<formats><format id="syslog" format="%CustomSyslogHeader(20) %Msg%n"/>
</formats>
调度器
splitter dispatcher
把消息发送到所有的孩子节点
元素名:splitter
属性:formatid
outputs也是一个splitter
<seelog><outputs><splitter formatid="format1"><file path="log.log"/><file path="log2.log"/></splitter><splitter formatid="format2"><file path="log3.log"/><file path="log4.log"/></splitter></outputs><formats>...</formats>
</seelog>
filter dispatcher
把消息发送到所有的孩子节点,当接收到的日志级别在允许的列表中时。
元素名:filter
属性:
formatid
levels
<seelog><outputs><filter levels="trace,debug"><file path="filter.log"/></filter><console /></outputs>
</seelog>
这篇关于seelog学习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!