golang第三方日志包seelog配置文件详解
开发任何项目,都离不开日志,配好自己的项目日志输出,往往是开发项目的前提。在golang中,seelog应该是比较有名的日志处理包了,功能非常强大,seelog官方文档
一、seelog主要功能
下面我们看看seelog有啥强大
设置不同级别的日志;
输出到终端或文件;
过滤指定级别日志;
定义多种不同的日志输出格式;
根据触发日志的文件名或者函数名来区别输出日志;
通过 SMTP 或 TCP 转发日志(网络转发日志);
滚动日志文件(过期日志自动清除)。
二、安装seelog
$ go get github.com/cihub/seelog
三、使用seelog
我们先看看一个官方的demo
package main
import (
log "github.com/cihub/seelog"
)
func main() {
defer log.Flush()
log.Info("Hello from Seelog!")
}
很简单是吧,导入包,就直接可以用了,下面我们看看如何通过配置文件高度定制我们的日志。
import (
log "github.com/cihub/seelog"
)
func SetupLogger() {
logger, err := log.LoggerFromConfigAsFile("seelog.xml")
if err != nil {
return
}
log.ReplaceLogger(logger)
}
seelog的配置文件一般用xml,下面我们看seelog.xml配置文件的大体框架
<seelog>
<exceptions>
<exception ... />
<exception ... />
...
</exceptions>
<outputs>
<splitter>
<console/>
<file ... />
<rollingfile ... />
<smtp>
...
</smtp>
<buffered>
...
</buffered>
<conn ... />
</splitter>
<filter>
...
</filter>
</outputs>
<formats>
<format ... />
<format ... />
...
</formats>
</seelog>
对框架有了解后,下面我们拿官方的实例配置文件来做说明:https://github.com/cihub/seelog/wiki/Example-config
<!-- type 设置记录器类型 https://github.com/cihub/seelog/wiki/Logger-types-reference
minlevel 设置日志最低级别; maxlevel 设置日志的最高级别
也可以通过 <seelog levels="trace,info,critical"> 设置日记级别 -->
<seelog type="asynctimer" asyncinterval="5000000" minlevel="debug" maxlevel="error">
<exceptions>
<!-- <exception> 是为特定的程序文件(filepattern)或函数(funcpattern)设定特殊的日志规则 -->
<exception funcpattern="*main.test*Something*" minlevel="info"/>
<exception filepattern="*main.go" minlevel="error"/>
</exceptions>
<!-- <outputs> formatid 指定日志输出的格式(格式在<formats>标签中定义) -->
<outputs formatid="main">
<!-- <console> 标签表示输出到终端 -->
<console/>
<!-- <splitter> 用于细分<outputs>日志格式,内部支持:file(文件), rollingfile(滚动文件,自动清除过期),
buffered(日志写到内存再写到文件), smtp(发送日志到邮件), con(网络转发) -->
<splitter formatid="format1">
<!-- log.log, log2.log将按<formats>标签中的id="format1"格式写入 -->
<file path="log.log"/>
<file path="log2.log"/>
</splitter>
<splitter formatid="format2">
<file path="log3.log"/>
<file path="log4.log"/>
</splitter>
<!-- <rollingfile>滚动文件(定期清除过期日志)
formatid: 指定日志格式; type="size" 按大小; maxsize: 单日志文件最大大小; maxrools: 最大文件数 -->
<rollingfile formatid="someformat" type="size" filename="./log/roll.log" maxsize="100" maxrolls="5" />
<!-- <buffered> 将日志先存在内存中,定期写入文件,适合日志并发量较大或 IO 比较紧张的场合
size: 缓存大小; flushperiod: 缓存间隔(毫秒) -->
<buffered formatid="testlevels" size="10000" flushperiod="1000">
<file path="./log/bufFileFlush.log"/>
</buffered>
<!-- <filter>用于单独处理某级别日志
过滤日志,把级别是error的通过邮件smtp方式发送出去(一般会发给相应的运维人员) -->
<filter levels="error">
<file path="./log/error.log"/>
<smtp senderaddress="noreply-notification-service@none.org"
sendername="Automatic notification service"
hostname="mail.none.org"
hostport="587"
username="nns"
password="123">
<recipient address="john-smith@none.com"/>
<recipient address="hans-meier@none.com"/>
</smtp>
<!-- 按tcp4网络协议发送日志 -->
<conn net="tcp4" addr="server.address:5514" tls="true" insecureskipverify="true" />
</filter>
</outputs>
<!-- <formats> 定制日志的输出格式
https://github.com/cihub/seelog/wiki/Format-reference -->
<formats>
<format id="main" format="%Date(2006 Jan 02/3:04:05.000000000 PM MST) [%Level] %Msg%n"/>
<format id="someformat" format="%Ns [%Level] %Msg%n"/>
<format id="testlevels" format="%Level %Lev %LEVEL %LEV %l %Msg%n"/>
<format id="usetags" format="<msg>%Msg</time>"/>
<format id="format1" format="%Date/%Time [%LEV] %Msg%n"/>
<format id="format2" format="%File %FullPath %RelFile %Msg%n"/>
</formats>
</seelog>
初次接触难免会觉得配置文件的内容有点多,难以理解,其实只要我们理解它配置文件的大体框架,然后自己试着更改配置文件,看看输出什么,就很快明白了。
<!-- 我只需要把日志按指定格式输出到终端 -->
<seelog type="asynctimer" asyncinterval="1000000" minlevel="debug" maxlevel="error">
<outputs formatid="main">
<!-- 仅输出到终端 -->
<console/>
</outputs>
<formats>
<!-- 设置格式 -->
<format id="main" format="%UTCDate %UTCTime - [%LEV] - %RelFile - l%Line - %Msg%n"/>
</formats>
</seelog>
<!-- ****** 我是分割线 ***** -->
<!-- 现在我想把日志输出到终端同时也把日志输出到文件 -->
<seelog type="asynctimer" asyncinterval="1000000" minlevel="debug" maxlevel="error">
<outputs formatid="main">
<console/>
<!-- 输出到文件,且不同于终端的日志格式 -->
<splitter formatid="format1">
<file path="log.log"/>
</splitter>
</outputs>
<formats>
<!-- 设置格式 -->
<format id="main" format="%UTCDate %UTCTime - [%LEV] - %RelFile - l%Line - %Msg%n"/>
<format id="format1" format="%Date(2006 Jan 02/3:04:05.000000000 PM MST) [%Level] %Msg%n"/>
</formats>
</seelog>
golang第三方日志包seelog配置文件详解的更多相关文章
- Java学习-007-Log4J 日志记录配置文件详解及实例源代码
此文主要讲述在初学 Java 时,常用的 Log4J 日志记录配置文件详解及实例源代码整理.希望能对初学 Java 编程的亲们有所帮助.若有不足之处,敬请大神指正,不胜感激!源代码测试通过日期为:20 ...
- 一、Nginx配置文件详解
配置文件介绍 主要有两部分:分别是 main:主体部分 http{}:虚拟主机配置部分 配置指令主要以分号结尾:配置语法:directive value1 [value2 ....] 支持使用的变量 ...
- 网卡配置文件详解 用户管理与文件权限篇 文件与目录权限 软连接 tar解压命令 killall命令 linux防火墙 dns解析设置 计划任务crond服务 软件包安装 阿里云 yum源 安装
Linux系统基础优化及常用命令 Linux基础系统优化 引言没有,只有一张图. Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. ...
- SaltStack系列(一)之环境部署、命令及配置文件详解
一.SaltStack介绍 1.1 saltstack简介: saltstack是基于python开发的一套C/S架构配置管理工具,它的底层使用ZeroMQ消息队列pub/sub方式通信,使用SSL证 ...
- Nginx简介及配置文件详解
http://blog.csdn.net/hzsunshine/article/details/63687054 一 Nginx简介 Nginx是一款开源代码的高性能HTTP服务器和反向代理服务 ...
- nginx之旅(第一篇):nginx下载安装、nginx启动与关闭、nginx配置文件详解、nginx默认网站
一.nginx下载安装 版本nginx 1.15.5 系统环境centos7.5(本机ip192.168.199.228) 关闭selinux 和防火墙firewall 1.下载 wget http: ...
- ubuntu nginx 安装以及配置文件详解
1.到nginx官网下载源码包.最好下载稳定版本,nginx官网http://www.nginx.org/ 2.安装nginx依赖包运行命令: sudo apt-get install libssl- ...
- Rsyslog配置文件详解
Rsyslog配置文件详解https://my.oschina.net/0757/blog/198329 # Save boot messages also to boot.log 启动的相关信息lo ...
- 【转】nginx服务器安装及配置文件详解
原文:http://seanlook.com/2015/05/17/nginx-install-and-config/ nginx服务器安装及配置文件详解 nginx在工作中已经有好几个环境在使用了, ...
随机推荐
- 关于域名如何指向WordPress homepage问题的解决
http://genuinelx.org/oldversion.php/archives/19为解决这个问题真的费了我半天的时间= = ,不写出来真的难以抒发苦闷. 下午VPS开通了,虽然有个ip被墙 ...
- 解决错误This message can only be sent over HTTPS.
在做基于Owin oauth客户端申请授权,调用如下代码: this.InitializeWebServerClient(); var userAuthorization = _webServerCl ...
- 【js与jquery】jquery循环滚动新闻
2.html代码: <h3>最新动态</h3> <div class="scrollNews" > <ul> <li>& ...
- Android Scrollview嵌套RecyclerView导致滑动卡顿问题解决
一个比较长的界面一般都是Scrollview嵌套RecyclerView来解决.不过这样的UI并不是我们开发人员想看到的,实际上嵌套之后.因为Scrollview和RecyclerView都是滑动控件 ...
- 计算机通信协议之OSI参考模型
OSI参考模型 在OSI参考模型之前人类对计算机结构的研究就已经进行了太多的讨论,最终通过了作为通信协议设计指标的OSI参考模型.这个协议将通信协议中必要的功能分成了七个部分.通过这些分层使得那些比较 ...
- unity 获得子节点
transform.FindChild("childName") transform.FindChild("childName/grandChildName") ...
- 【Android】7.3 GridLayout(网格布局)
分类:C#.Android.VS2015: 创建日期:2016-02-10 一.简介 Android 4.0(API 14)开始提供的GridLayout布局使用虚细线将布局划分为行.列和单元格,也支 ...
- 安卓getSystemService
getSystemService是Activity中的方法,依据传入的name来取得相应的服务对象,这些服务名称參数都是Context类中的常量 Name ...
- informatica 参数文件配置
Informatica 中 parameter file 参数文件配置规则: 参数文件的头部内容 [Global] All Integration Services, Integration Serv ...
- ssh 连 koding
2014.12.10更新可用方法 koding是一个在线的开发平台.让自己从开发平台中释放出来.除了提供在线编程功能之外,Koding还有强大的社区功能,允许开发者通过相互浏览.交换代码而达到项目协作 ...