Documentation ¶
Index ¶
- func AsStdLogger(l logrus.FieldLogger, level logrus.Level, prefix string, flag int) *log.Logger
- func AsStdLoggerWithLevel(l logrus.FieldLogger, level logrus.Level) *log.Logger
- func DefaultSlogHook(entry *logrus.Entry) error
- func SlogHook(h slog.Handler) func(entry *logrus.Entry) error
- func StandardWriter(level logrus.Level) io.Writer
- func ToSlogLevel(l logrus.Level) slog.Level
- func Writer(l logrus.FieldLogger, level logrus.Level) io.Writer
- type Factory
- type FactoryConfig
- type Format
- type HookFunc
- type HookMap
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsStdLogger ¶
AsStdLogger returns *log.Logger from logrus.FieldLogger
func AsStdLoggerWithLevel ¶
AsStdLoggerWithLevel is only a helper of AsStdLogger
func DefaultSlogHook ¶ added in v1.2.99
DefaultSlogHook returns a logrus Hook by slog.Default, followed even if slog.SetDefault changes default slog logger.
func SlogHook ¶ added in v1.2.99
SlogHook returns a logrus Hook by slog.Handler
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
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) 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 日志格式
func ParseFormat ¶ added in v1.2.6
ParseFormat takes a string format and returns the Logrus log format constant.
func (Format) MarshalText ¶ added in v1.2.6
func (Format) String ¶ added in v1.2.6
Convert the Level to a string. E.g. FormatJson becomes "json".
func (*Format) UnmarshalText ¶ added in v1.2.6
UnmarshalText implements encoding.TextUnmarshaler.