logrus

package module
v1.2.117 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: MIT Imports: 12 Imported by: 3

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsStdLogger

func AsStdLogger(l logrus.FieldLogger, level logrus.Level, prefix string, flag int) *log.Logger

AsStdLogger returns *log.Logger from logrus.FieldLogger

func AsStdLoggerWithLevel

func AsStdLoggerWithLevel(l logrus.FieldLogger, level logrus.Level) *log.Logger

AsStdLoggerWithLevel is only a helper of AsStdLogger

func DefaultSlogHook added in v1.2.99

func DefaultSlogHook(entry *logrus.Entry) error

DefaultSlogHook returns a logrus Hook by slog.Default, followed even if slog.SetDefault changes default slog logger.

func SlogHook added in v1.2.99

func SlogHook(h slog.Handler) func(entry *logrus.Entry) error

SlogHook returns a logrus Hook by slog.Handler

func StandardWriter

func StandardWriter(level logrus.Level) io.Writer

func ToSlogLevel added in v1.2.86

func ToSlogLevel(l logrus.Level) slog.Level

func Writer

func Writer(l logrus.FieldLogger, level logrus.Level) io.Writer

Types

type Factory added in v1.2.6

type Factory struct {
	// contains filtered or unexported fields
}

func NewFactory added in v1.2.6

func NewFactory(fc FactoryConfig) Factory

func NewFactoryFromFile added in v1.2.6

func NewFactoryFromFile(name string, unmarshal func(data []byte, v any) error) (Factory, error)

NewFactoryFromFile reads factory from file, parsed by unmarshal

Example
fromFile, err := NewFactoryFromFile("./testdata/log.json", json.Unmarshal)
//NewFactoryFromFile("./testdata/log.yaml", yaml.Unmarshal)
if err != nil {
	log.Fatalf("read file failed: %s", err)
	return
}
err = fromFile.Apply()
if err != nil {
	log.Fatalf("read file failed: %s", err)
}

// [INFO ][20230814 00:28:27.055471] [70427] [logrus.factory.slog.go:94](Apply) add rotation wrapper for log, path=./testdata/log/example, mute_directly_output=true, rotate_size_in_byte=0, duration=1h0m0s, max_age=24h0m0s, max_count=0
// [INFO ][20230814 00:28:27.056335] [70427] [example_test.go:26](ExampleNewFactoryFromFile) Hello World
// [WARN ][20230814 00:28:27.056381] [70427] [example_test.go:27](ExampleNewFactoryFromFile) Hello World
logrus.Infof("Hello World")
logrus.Warnf("Hello World")
Output:

func (Factory) Apply added in v1.2.6

func (f Factory) Apply() error

func (Factory) Config added in v1.2.6

func (f Factory) Config() FactoryConfig

type FactoryConfig added in v1.2.6

type FactoryConfig struct {
	Level                   logrus.Level  `json:"level,omitempty" yaml:"level"`                                           // sets the logger level, E.g. panic, fatal, error, warn, info, debug, trace
	Format                  Format        `json:"format,omitempty" yaml:"format"`                                         // sets the logger format, E.g. json, text, glog, glog_human
	Path                    string        `json:"path,omitempty" yaml:"path"`                                             // sets the log file path prefix, E.g. "./log/" + filepath.Base(os.Args[0]).
	RotationDuration        time.Duration `json:"rotation_duration,omitempty" yaml:"rotation_duration"`                   // Rotate files are rotated until RotateInterval expired before being removed, E.g.
	RotationSizeInByte      int64         `json:"rotation_size_in_byte,omitempty" yaml:"rotation_size_in_byte"`           // 日志循环最大分片大小,单位为Byte
	RotationMaxCount        int           `json:"rotation_max_count,omitempty" yaml:"rotation_max_count"`                 // 日志循环覆盖保留分片个数
	RotationMaxAge          time.Duration `json:"rotation_max_age,omitempty" yaml:"rotation_max_age"`                     // 文件最大保存时间
	ReportCaller            bool          `json:"report_caller,omitempty" yaml:"report_caller"`                           // 调用者堆栈
	MuteDirectlyOutput      bool          `json:"mute_directly_output,omitempty" yaml:"mute_directly_output"`             // warn及更高级别日志是否打印到标准输出
	MuteDirectlyOutputLevel logrus.Level  `json:"mute_directly_output_level,omitempty" yaml:"mute_directly_output_level"` // 标准输出日志最低打印等级
	TruncateMessageSizeTo   int           `json:"truncate_message_size_to,omitempty" yaml:"truncate_message_size_to"`     // 日志 message 最大长度,超长则截断; 当前仅glog和glog_human模式生效
	TruncateKeySizeTo       int           `json:"truncate_key_size_to,omitempty" yaml:"truncate_key_size_to"`             // 日志键值对的key最大长度,超长则截断; 当前仅glog和glog_human模式生效
	TruncateValueSizeTo     int           `json:"truncate_value_size_to,omitempty" yaml:"truncate_value_size_to"`         // 日志键值对的value最大长度,超长则截断; 当前仅glog和glog_human模式生效
}

FactoryConfig 日志工厂函数配置

func (*FactoryConfig) SetDefaults added in v1.2.6

func (fc *FactoryConfig) SetDefaults()

SetDefaults sets sensible values for unset fields in config. This is exported for testing: Configs passed to repository functions are copied and have default values set automatically.

type Format added in v1.2.6

type Format int32

Format 日志格式

const (
	FormatJson      Format = 0
	FormatText      Format = 1
	FormatGlog      Format = 2
	FormatGlogHuman Format = 3
)

func ParseFormat added in v1.2.6

func ParseFormat(lvl string) (Format, error)

ParseFormat takes a string format and returns the Logrus log format constant.

func (Format) MarshalText added in v1.2.6

func (f Format) MarshalText() ([]byte, error)

func (Format) String added in v1.2.6

func (f Format) String() string

Convert the Level to a string. E.g. FormatJson becomes "json".

func (*Format) UnmarshalText added in v1.2.6

func (f *Format) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type HookFunc

type HookFunc func(entry *logrus.Entry) error

func (HookFunc) Fire

func (f HookFunc) Fire(entry *logrus.Entry) error

func (HookFunc) Levels

func (f HookFunc) Levels() []logrus.Level

type HookMap

type HookMap map[logrus.Level]func(entry *logrus.Entry) error

func (HookMap) Fire

func (hooks HookMap) Fire(entry *logrus.Entry) error

func (HookMap) Levels

func (hooks HookMap) Levels() []logrus.Level

Jump to

Keyboard shortcuts

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