xlog

package
v0.0.0-...-d853fe5 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FieldKeyMsg         = logrus.FieldKeyMsg
	FieldKeyLevel       = logrus.FieldKeyLevel
	FieldKeyTime        = logrus.FieldKeyTime
	FieldKeyLogrusError = logrus.FieldKeyLogrusError
	FieldKeyFunc        = logrus.FieldKeyFunc
	FieldKeyFile        = logrus.FieldKeyFile
	FieldKeyLine        = "line"
	TagBL               = "["
	TagBR               = "]"
	TagColon            = ":"
	TagVBar             = "|"
)

Variables

View Source
var (
	// PrintProtoLen 设置打印pb报文的长度,超过这个长度将会对报文中的超长字段进行处理(截断)
	PrintProtoLen = 1024

	// PrintStringLen 设置打印报文中字符串的长度,超过这个长度将会对报文中的超长字段进行处理(截断)
	PrintStringLen = 64

	// PrintSliceLen 设置打印报文中的slice或者array的长度,超过这个长度将会对报文中的超长内容进行处理(截断)
	PrintSliceLen = 64

	// 处理string的方式,默认截断后面补上 "..."
	DealStringHook DealStringHookFunc = getStringSlice
)
View Source
var (
	FunctionNameLength = 25
	FileNameLength     = 20
)
View Source
var (
	AddedHooks []logrus.Hook
)

Functions

func AddHook

func AddHook(hooks ...logrus.Hook)

goroutine unsafe AddHook adds a hook to the standard logger hooks.

func Debug

func Debug(args ...any)

func Debugf

func Debugf(args ...any)

func Debugln

func Debugln(args ...any)

func Error

func Error(args ...any)

func Errorf

func Errorf(args ...any)

func Errorln

func Errorln(args ...any)

func Fatal

func Fatal(args ...any)

func Fatalf

func Fatalf(args ...any)

func Fatalln

func Fatalln(args ...any)

func FmtString

func FmtString(i any) string

String 把i转换成string

func GetStructFields

func GetStructFields(st any) (fields map[string]any, err error)

GetStructFields 把结构体转换成一个map

func Info

func Info(args ...any)

func Infof

func Infof(args ...any)

func Infoln

func Infoln(args ...any)

func Init

func Init(level string, organizeLog, reportCaller bool, out io.Writer, tags ...TagGetter)

对日志初始化,参数如下: level :设置日志级别, 方便从配置文件中直接获取,所以使用string organizeLog :是否整理日志, 本地调试或者不需要收集时建议true, 配合OrganizeLogMiddleware 使用 reportCaller :是否打印函数调用栈 out :日志打印到何处 tags :设置日志中获取上下文的接口

func JsonString

func JsonString(r any) string

把定义了Json字段的结构本转换为Json字符串输出

func Panic

func Panic(args ...any)

func Panicf

func Panicf(args ...any)

func Panicln

func Panicln(args ...any)

func Print

func Print(args ...any)

func Printf

func Printf(args ...any)

func Println

func Println(args ...any)

func ProtoToPrintString

func ProtoToPrintString(p proto.Message) string

ProtoToPrintString pb协议转换成可打印的json string,会对超长内容截断

func String

func String(i any) fmt.Stringer

use this

String 入参换成可打印的json string,包括protobuf和普通结构体,或者其他类型 返回 fmt.Stringer, 防止日志级别不够时,还执行这个很消耗资源的操作

func StructToString

func StructToString(p any) string

StructToPrintString 结构体转换成可打印的json string,会对超长内容截断

func Trace

func Trace(args ...any)

func Tracef

func Tracef(args ...any)

func Traceln

func Traceln(args ...any)

func Warn

func Warn(args ...any)

func Warnf

func Warnf(args ...any)

func Warning

func Warning(args ...any)

func Warningf

func Warningf(args ...any)

func Warningln

func Warningln(args ...any)

func Warnln

func Warnln(args ...any)

Types

type DealStringHookFunc

type DealStringHookFunc func(string) string

type DefaultLogTag

type DefaultLogTag struct {
}

func (DefaultLogTag) GetTags

func (DefaultLogTag) GetTags(c context.Context) map[string]string

type FieldMap

type FieldMap map[fieldKey]string

FieldMap allows customization of the key names for default fields.

type Formatter

type Formatter interface {
	FormatHook(*Hook) ([]byte, error)
}

type HandlerFormatFile

type HandlerFormatFile func(fileName string) string

HandlerFormatFile format file name

type HandlerFormatFunc

type HandlerFormatFunc func(funcName string) string

HandlerFormatFunc format function name

type Hook

type Hook struct {
	ReportCaller bool

	Formatter Formatter

	Out io.Writer

	// Contains all the fields set by the user.
	Data logrus.Fields

	// Time at which the log entry was created
	Time time.Time

	// Level the log entry was logged at: Trace, Debug, Info, Warn, Error, Fatal or Panic
	// This field will be set on entry firing and the value will be equal to the one in Logger struct field.
	Level logrus.Level

	// Calling method, with package name
	Caller *runtime.Frame

	// Message passed to Trace, Debug, Info, Warn, Error, Fatal or Panic
	Message string

	// Contains the context set by the user. Useful for hook processing etc.
	Context context.Context

	// When formatter is called in entry.log(), a Buffer may be set to entry
	Buffer *bytes.Buffer
	// contains filtered or unexported fields
}

goroutine unsafe, 里面的变量只能在初始化时赋值 Hook hook for log

func (*Hook) Dup

func (h *Hook) Dup() *Hook

func (*Hook) Fire

func (h *Hook) Fire(entry *logrus.Entry) error

func (*Hook) HasCaller

func (h *Hook) HasCaller() bool

func (*Hook) Levels

func (h *Hook) Levels() []logrus.Level

Levels giving the level you care, you can rewrite it

type JSONFormatter

type JSONFormatter struct {
	// TagGetter implement this to get all tag you want, like traceid , requestid etc...
	TagGetters []TagGetter

	// TimestampFormat sets the format used for marshaling timestamps.
	TimestampFormat string

	// DisableTimestamp allows disabling automatic timestamps in output
	DisableTimestamp bool

	// DisableHTMLEscape allows disabling html escaping in output
	DisableHTMLEscape bool

	// DataKey allows users to put all the log entry parameters into a nested dictionary at a given key.
	DataKey string

	// FieldMap allows users to customize the names of keys for default fields.
	// As an example:
	// formatter := &JSONFormatter{
	//   	FieldMap: FieldMap{
	// 		 FieldKeyTime:  "@timestamp",
	// 		 FieldKeyLevel: "@level",
	// 		 FieldKeyMsg:   "@message",
	// 		 FieldKeyFunc:  "@caller",
	//    },
	// }
	FieldMap FieldMap

	// CallerPrettyfier can be set by the user to modify the content
	// of the function and file keys in the json data when ReportCaller is
	// activated. If any of the returned value is the empty string the
	// corresponding key will be removed from json fields.
	CallerPrettyfier func(*runtime.Frame) (function string, file string)

	// PrettyPrint will indent all json logs
	PrettyPrint bool
}

JSONFormatter formats logs into parsable json

var (
	LogFileNameLen = 40

	JsonFormatter *JSONFormatter
)

func (*JSONFormatter) Format

func (f *JSONFormatter) Format(entry *logrus.Entry) ([]byte, error)

func (*JSONFormatter) FormatHook

func (f *JSONFormatter) FormatHook(entry *Hook) ([]byte, error)

Format renders a single log entry

type Level

type Level logrus.Level

func (Level) MarshalText

func (level Level) MarshalText() ([]byte, error)

func (Level) String

func (level Level) String() string

Convert the Level to a string. E.g. PanicLevel becomes "panic".

type TagGetter

type TagGetter interface {
	GetTags(c context.Context) map[string]string
}

TagGetter

type TagSourceFormatter

type TagSourceFormatter interface {
	Format(file, fun string, line int) (tag, value string)
}

type TextFormatter

type TextFormatter struct {
	// TagGetter implement this to get all tag you want, like traceid , requestid etc...
	TagGetters []TagGetter

	// TimestampFormat to use for display when a full timestamp is printed
	TimestampFormat string

	// QuoteEmptyFields will wrap empty fields in quotes if true
	QuoteEmptyFields bool
	NoQuoteFields    bool

	FormatFuncName HandlerFormatFunc
	FormatFileName HandlerFormatFile

	TagSource bool

	FieldKeys []string

	TagSourceFormatter
	// contains filtered or unexported fields
}

TextFormatter formats logs into text

var (
	StdFormatter        *TextFormatter
	SimpleFormatter     *TextFormatter
	SingleLineFormatter *TextFormatter
)

func (*TextFormatter) Format

func (f *TextFormatter) Format(entry *logrus.Entry) ([]byte, error)

func (*TextFormatter) FormatHook

func (f *TextFormatter) FormatHook(hook *Hook) ([]byte, error)

nolint

func (*TextFormatter) RegisterFields

func (f *TextFormatter) RegisterFields(args ...string)

func (*TextFormatter) SetFormat

func (f *TextFormatter) SetFormat(msgFmts ...string)

func (*TextFormatter) SetFormatAndTagSource

func (f *TextFormatter) SetFormatAndTagSource(sourceFmt TagSourceFormatter, msgFmts ...string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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