zoukankan      html  css  js  c++  java
  • 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{})
    }

  • 相关阅读:
    面向对象的继承关系体现在数据结构上时,如何表示
    codeforces 584C Marina and Vasya
    codeforces 602A Two Bases
    LA 4329 PingPong
    codeforces 584B Kolya and Tanya
    codeforces 584A Olesya and Rodion
    codeforces 583B Robot's Task
    codeforces 583A Asphalting Roads
    codeforces 581C Developing Skills
    codeforces 581A Vasya the Hipster
  • 原文地址:https://www.cnblogs.com/zhangboyu/p/7461550.html
Copyright © 2011-2022 走看看