caller

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2019 License: Apache-2.0 Imports: 6 Imported by: 0

README

Caller Hook for Logrus

使用hook的方式让Logrus拥有输出日志时输出源代码所在文件和行号功能

示例

默认
package main

import (
    log "github.com/sirupsen/logrus"
    caller "github.com/xdxiaodong/logrus-hook-caller"
)

func main() {
    log.SetFormatter(new(log.JSONFormatter))
    hook_caller := caller.NewHook(&caller.CallerHookOptions{
	//DisabledField: true,
	//EnableFile: true,
	//EnableLine: true,
	//Flags:      caller.Llongfile,
	})
    log.AddHook(hook_caller)
    log.Info("hello world")
}
源文件完整路径
package main

import (
    log "github.com/sirupsen/logrus"
    caller "github.com/xdxiaodong/logrus-hook-caller"
)

func main() {
    log.SetFormatter(new(log.JSONFormatter))
    hook_caller := caller.NewHook(&caller.CallerHookOptions{
	Flags:      caller.Llongfile,
	})
    log.AddHook(hook_caller)
    log.Info("hello world")
}

Documentation

Index

Constants

View Source
const (
	Ldate         = 1 << iota     // the date in the local time zone: 2009/01/23
	Ltime                         // the time in the local time zone: 01:23:23
	Lmicroseconds                 // microsecond resolution: 01:23:23.123123.  assumes Ltime.
	Llongfile                     // full file name and line number: /a/b/c/d.go:23
	Lshortfile                    // final file name element and line number: d.go:23. overrides Llongfile
	LUTC                          // if Ldate or Ltime is set, use UTC rather than the local time zone
	LstdFlags     = Ldate | Ltime // initial values for the standard logger
)

refer import "log"

Variables

View Source
var LogrusRegexp = regexp.MustCompile(`sirupsen/logrus(@.*)?/.*.go`)
View Source
var MiscRegexp = regexp.MustCompile(`zxfonline/misc(@.*)?/log/.*.go`)
View Source
var SuffixesToIgnoreArray = []*regexp.Regexp{
	MiscRegexp,
	LogrusRegexp,
}

Functions

This section is empty.

Types

type CallerHook

type CallerHook struct {
	CallerHookOptions *CallerHookOptions
}

func NewHook

func NewHook(options *CallerHookOptions) *CallerHook

NewHook creates a new caller hook with options. If options are nil or unspecified, options.Field defaults to "src" and options.Flags defaults to log.Llongfile

func (*CallerHook) Fire

func (hook *CallerHook) Fire(entry *logrus.Entry) error

func (*CallerHook) Levels

func (hook *CallerHook) Levels() []logrus.Level

type CallerHookOptions

type CallerHookOptions struct {
	// new
	FileAlias  string //default:file
	EnableFile bool
	LineAlias  string //default:line
	EnableLine bool
	// old
	// Field to display caller info in
	Field         string
	DisabledField bool
	// Stores the flags
	Flags int
}

CallerHookOptions stores caller hook options

func (*CallerHookOptions) HasFlag

func (options *CallerHookOptions) HasFlag(flag int) bool

HasFlag returns true if the report caller options contains the specified flag

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL