一、Basic:日志工具库
1、后台输出
package main
import (
"fmt"
)
func main(){
fmt.Println("------hello world-----")
}
2、There are no exceptions in Golang, only errors.
package main
import (
"log"
"errors"
"fmt"
)
func main() {
/* local variable definition */
...
/* function for division which return an error if divide by 0 */
ret,err = div(a, b)
if err != nil {
log.Fatal(err)
}
fmt.Println(ret)
}
3、写入日志文件:
package main
import (
"log"
"os"
)
func main(){
f,err :=os.OpenFile("test.log",os.O_WRONLY|os.O_CREATE|os.O_APPEND,0644)
if err !=nil{
log.Fatal(err)
}
defer f.Close()
log.SetOutput(f)
log.Println("==========works==============")
}
YRMacBook-Pro:go-log yanrui$ more test.log
2017/05/24 21:46:25 ==========works==============
二、格式化
推荐日志工具库:logrus
$ go get github.com/Sirupsen/logrus
1、JSON format
package main
import (
log "github.com/Sirupsen/logrus"
"github.com/logmatic/logmatic-go"
)
func main() {
// use JSONFormatter
log.SetFormatter(&logmatic.JSONFormatter{})
// log an event as usual with logrus
log.WithFields(log.Fields{"string": "foo", "int": 1, "float": 1.1 }).Info("My first ssl event from golang")
}
日志输出样式:
{
"@marker":["sourcecode","golang"],
"date":"2017-05-24T15:27:40+08:00",
"float":1.1,"int":1,"level":"info",
"message":"My first ssl event from golang",
"string":"foo"
}
三、附加上下文
通过logrus库可以加入一些上下文信息,例如:主机名称,程序名称或者会话参数等。
contextLogger := log.WithFields(log.Fields{
"common": "XXX common content XXX",
"other": "YYY special context YYY",
})
contextLogger.Info("AAAAAAAAAAAA")
contextLogger.Info("BBBBBBBBBBBB")
日志输出样式:
YRMacBook-Pro:go-log yanrui$ go run LogMatic.go
{"@marker":["sourcecode","golang"],"common":"XXX common content XXX","date":"2017-05-24T17:00:08+08:00","level":"info","message":"AAAAAAAAAAAA","other":"YYY special context YYY"}
{"@marker":["sourcecode","golang"],"common":"XXX common content XXX","date":"2017-05-24T17:00:08+08:00","level":"info","message":"BBBBBBBBBBBB","other":"YYY special context YYY"}
YRMacBook-Pro:go-log yanrui$
四、Hooks
我们还可以利用Hook机制实现日志功能扩展,例如Syslog hook,将输出的日志发送到指定的Syslog服务。
package main
import (
log "github.com/sirupsen/logrus"
"gopkg.in/gemnasium/logrus-airbrake-hook.v2" // the package is named "aibrake"
logrus_syslog "github.com/sirupsen/logrus/hooks/syslog"
"log/syslog"
)
func main(){
hook, err := logrus_syslog.NewSyslogHook("udp", "59.37.0.1:514", syslog.LOG_INFO, "")
if err != nil {
log.Error("Unable to connect to local syslog daemon")
} else {
log.AddHook(hook)
}
}
验证是否发送Syslog:
$ sudo tcpdump | grep 127.0.0.3
tcpdump: data link type PKTAP
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on pktap, link-type PKTAP (Apple DLT_PKTAP), capture size 262144 bytes
18:51:05.663612 IP 192.168.199.15.58819 > 127.0.0.3.syslog: SYSLOG kernel.info, length: 314
18:51:05.663657 IP 192.168.199.15.58819 > 127.0.0.3.syslog: SYSLOG kernel.info, length: 314
五、可视化
在真实场景中日志数据体量非常庞大,日志存储只是第一步,更多的情况是需要查看特定指标或者能够快速检索信息,此时日志分析平台就发挥作用了。以logmatic为例,可以在它的官网注册https://logmatic.io/,免费体验。
在使用logmatic之前,需要下载它的hook支持:
$ go get github.com/logmatic/logmatic-go
func main() {
// instantiate a new Logger with your Logmatic APIKey
// 国内访问比较慢
log.AddHook(logmatic.NewLogmaticHook("p53uTkOhSEqI3-116DynkQ"))
// ..........
}
效果如下:
有疑问加站长微信联系(非本文作者)