1. logrus
Logrus is a structured logger for Golang
1.1. 获取logrus
1 |
go get github.com/sirupsen/logrus |
注意:
作者最近将这个包转移到了sirupsen/logrus里面,但是之前的名字是Sirupsen/logrus,所以在使用其他hooker包时,由于那个第三方包里依旧使用Sirupsen,可能会出现冲突.
1.2. log等级
logrus和go lib里面一样有6个等级,可以直接调用
1 2 3 4 5 6 |
logrus.Debug("Useful debugging information.") logrus.Info("Something noteworthy happened!") logrus.Warn("You should probably take a look at this.") logrus.Error("Something failed but I'm not quitting.") logrus.Fatal("Bye.") //log之后会调用os.Exit(1) logrus.Panic("I'm bailing.") //log之后会panic() |
1.3. 第一个example
这是一个使用了fields的例子,可以添加多对field
1 2 3 4 5 6 7 8 9 |
package main import ( log "github.com/sirupsen/logrus" ) func main() { log.WithFields(log.Fields{ "animal": "walrus", }).Info("A walrus appears") } |
1.4. 设置log的参数
func init() {
//设置输出样式,自带的只有两种样式logrus.JSONFormatter{}和logrus.TextFormatter{}
logrus.SetFormatter(&logrus.JSONFormatter{})
//设置output,默认为stderr,可以为任何io.Writer,比如文件*os.File
logrus.SetOutput(os.Stdout)
//设置最低loglevel
logrus.SetLevel(logrus.InfoLevel)
}
1.5. Logger
如果想在一个应用里面向多个地方log,可以创建Logger实例,下面这个例子是利用创建的logger向文件log
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package main import ( "os" "github.com/sirupsen/logrus" ) // 你可以创建很多instance var log = logrus.New() func main() { file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666) if err == nil { log.Out = file //log.SetOutput(file) } else { log.Info("Failed to log to file, using default stderr") } log.WithFields(logrus.Fields{ "filename": "123.txt", }).Info("打开文件失败") } |
我们可以看看Logger里面都有什么
1 2 3 4 5 6 7 8 9 10 11 |
type Logger struct { Out io.Writer Hooks LevelHooks Formatter Formatter //最小级别 Level Level //被用来同步写入,比如两个地方同时log.默认是被锁住的 mu MutexWrap // Reusable empty entry entryPool sync.Pool } |
我们可以发现,独立的Logger,拥有自己的各个参数,比如直接使用logrus.Panic("GG")这是使用默认的Logger,
上面提到的init函数里面的各项设置,是设置默认Logger的,不会对自己生成的Logger有影响
1.6. Fields
有时候我们需要固定的fields,不需要向每行都重复写,只需要生成一 logrus.Entry
entry := logrus.WithFields(log.Fields{"request_id": request_id, "user_ip": user_ip})
entry.Info("something happened on that request")
entry.Warn("something not great happened")
1.7. Entry
logrus.WithFields会自动返回一个 *Entry,Entry里面的有些变量会被自动加上
time:entry被创建时的时间戳
msg:在调用.Info()等方法时被添加
level
1.8. Thread safety
默认的logger在并发写的时候是被mutex保护的,比如当同时调用hook和写log时mutex就会被请求,有另外一种情况,文件是以appending mode打开的,
此时的并发操作就是安全的,可以用logger.SetNoLock()来关闭它
2. logrus hook的使用
2.1. 获取go-slack
go-slack实现bearchat提示
1 |
go get github.com/multiplay/go-slack |
2.2. 例子
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 |
import ( "github.com/sirupsen/logrus" "github.com/multiplay/go-slack/chat" "github.com/multiplay/go-slack/lrhook" ) func Bearychat(){ //cfg必须符合Config里面的变量, cfg := lrhook.Config{ MinLevel: logrus.InfoLevel, Message: chat.Message{ Text: "发生了错误", }, Attachment: chat.Attachment{ //Attach里面有很多字段,这里能够设置的只有title // Field Text - Will be set to that of log entry Message. // Field Fields - Will be created to match the log entry Fields.其实bearchart里面没有field字段 // Field Color - Will be set according to the LevelColors or UnknownColor if a match is not found.. Title: "123.txt", }, } h := lrhook.New(cfg, "https://hook.bearychat.com/=bw9Y1/incoming/********") logrus.SetFormatter(&logrus.JSONFormatter{}) logrus.AddHook(h) //这里就可以使用Warn了 logrus.Warn("") } |
- 问题一
但这里有个问题,那就是,lrhook里面config的变量与bearchat里面的变量不对应,导致bearchat定义的的字段不能有效设置
但使用lrhook的好处是,在发生log时会自动发送
解决方法:
使用webhook,构造与规定对应的json,并且可以处理macdown,只需在log发生时,手动调用即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
func Bearyweb() { c := webhook.New("https://**************") m := &chat.Message{ Text: "filename", Markdown: true, Attachments: []*chat.Attachment{ { Title : "world war 2", Text : "*go back* \n**macdown**\n>fjia", Color : "#ffa500", }, }, } m.Send(c) } |
- 问题二
bearchat里面都是设置对应字段,所以不能像email那样把log级别自动加上
解决方法:
在将某个字段手动设置为想要的log级别,比如把Attachments:title字段设置为“Warn”,
3. Hook-Email
email这里只需用NewMailAuthHook方法得到hook,再添加即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
func Email(){ logger:= logrus.New() //parameter"APPLICATION_NAME", "HOST", PORT, "FROM", "TO" //首先开启smtp服务,最后两个参数是smtp的用户名和密码 hook, err := logrus_mail.NewMailAuthHook("testapp", "smtp.163.com",25,"username@163.com","username@163.com","smtp_name","smtp_password") if err == nil { logger.Hooks.Add(hook) } //生成*Entry var filename="123.txt" contextLogger :=logger.WithFields(logrus.Fields{ "file":filename, "content": "GG", }) //设置时间戳和message contextLogger.Time=time.Now() contextLogger.Message="这是一个hook发来的邮件" //只能发送Error,Fatal,Panic级别的log contextLogger.Level=logrus.FatalLevel //使用Fire发送,包含时间戳,message hook.Fire(contextLogger) } |
转载自链接:https://www.jianshu.com/p/5fac8bed4505
来源:简书
微信赞赏
支付宝赞赏