zap 打印到控制台,kafka
------
log4
1.使用%v打印错误,
log.Error("net.ListenTCP(\"tcp4\", \"%s\") error(%v)", bind, err)
type TestV struct{
iday int64
strInfo string
}
testV := &TestV{iday : 50, strInfo : "xiaoyu"}
log.Error("%v", *testV)
2.日志文件开关介绍
1. stdout/console 为true的时候,打开控制台开关,会在控制台打印所有级别的日志(但是不会创建文件),即便其他级别开关为false
<filter enabled="true">
<tag>stdout</tag>
<type>console</type>
<!-- level is (:?FINEST|FINE|DEBUG|TRACE|INFO|WARNING|ERROR) -->
<level>INFO</level>
</filter>
debug级别的日志里,会包含debug 级别以上的所有日志(哪怕其他级别的日志为false),当debug日志为true,info等其他级别日志为 true的时候,会创建多个日志文件
info级别的日志里,会包含info 级别以上的所有日志,为false的时候,不创建该级别日志
3.thinkboy的库,最多打印1000个文件,就不回滚了
rotate true 按天分隔日志
maxsize 默认0M表示没有最大文件大小限制, 最好配置个最大大小
logrus
package main
import (
"os"
"io"
log "github.com/sirupsen/logrus"
)
func init() {
// 设置日志格式为json格式
log.SetFormatter(&log.JSONFormatter{})
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
//FullTimestamp: true,
TimestampFormat : "2006-01-02 15:04:05.000", //设置精确到毫秒
})
f, err := os.OpenFile("logrusdemo.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return
}
// 设置将日志输出到标准输出(默认的输出为stderr,标准错误)
// 日志消息输出可以是任意的io.writer类型
log.SetOutput(f)
mw := io.MultiWriter(os.Stdout, f)
log.SetOutput(mw)
//log.SetOutput(os.Stdout) //只输出到std
// 设置日志级别为warn以上
log.SetLevel(log.DebugLevel)
}
func main() {
log.WithFields(log.Fields{
"animal": "walrus",
"size": 10,
}).Info("A group of walrus emerges from the ocean")
}