logrus_hook.go

package logrus_hook

import (
    "runtime"
    "strings"
    "path/filepath"
    log "github.com/Sirupsen/logrus"
)

type ContextHook struct {
}

func (hook ContextHook)Levels() []log.Level {
    return log.AllLevels
}

func (hook ContextHook)Fire(entry *log.Entry) error {
    pc := make([]uintptr, 10)
       //表示自身栈中跳过6个栈帧数  并且把栈中剩余信息写入pc中。
             //0表示Callers自身的调用栈,1表示Callers所在的调用栈
    runtime.Callers(6, pc)
       //
    frames := runtime.CallersFrames(pc)
    frame, _ := frames.Next()

    funcName := frame.Func.Name()
    funcName = funcName[strings.LastIndexByte(funcName, filepath.Separator) + 1 :]
    fileName := frame.File[strings.LastIndexByte(frame.File, filepath.Separator) + 1:]

    entry.Data["file"] = fileName
    entry.Data["func"] = funcName
    entry.Data["line"] = frame.Line

    //for {
    //    frame, more := frames.Next()
    //    println(frame.File)
    //    println(frame.Func.Name())
    //    println(frame.Line)
    //    println("")
    //
    //    if !more{
    //        break
    //    }
    //}

    return nil
}

func init() {
    log.AddHook(ContextHook{})
}
				
时间: 2024-10-10 13:27:40

logrus_hook.go的相关文章