logrusKit

package
v2.0.12 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultTextFormatter = NewTextFormatter("")
)

Functions

func DisableQuoteTemporarily

func DisableQuoteTemporarily(logger *logrus.Logger, callback func())

DisableQuoteTemporarily

@param logger (1) 可以为nil

(2) 只有当formatter为 *logrus.TextFormatter 类型,DisableQuote才会生效

func DisposeLogger

func DisposeLogger(logger *logrus.Logger) error

DisposeLogger 释放资源(主要针对文件日志)

func MustSetUp added in v2.0.12

func MustSetUp(config *Config)

func NewFileLogger

func NewFileLogger(filePath string, formatter logrus.Formatter, level logrus.Level, toConsoleFlag bool) (*logrus.Logger, error)

NewFileLogger

PS: 如果 logger.Out 被释放后继续调用 logger 进行输出,会失败(e.g. 控制台os.Stderr有输出: Failed to write to log, write /Users/richelieu/Downloads/a.txt: file already closed).

@param filePath 内部会做处理:

(1) 会尝试创建父级目录
(2) 文件不存在,会自动创建
(3) 文件存在:是个文件,追加在最后;是个目录,返回error

@param formatter 可以为nil,此时将采用默认值 @param toConsoleFlag true: 输出到日志文件的同时,也输出到控制台; false: 只输出到文件日志

func NewLogger

func NewLogger(options ...LoggerOption) *logrus.Logger

NewLogger

@param options 可以什么都不配置

func NewLumberjackLogger deprecated added in v2.0.12

func NewLumberjackLogger(lumberjackOptions []ioKit.LumberjackOption, options ...LoggerOption) (*logrus.Logger, error)

NewLumberjackLogger

Deprecated: 建议组合使用 ioKit.NewLumberjackWriteCloser() && logrusKit.NewLogger()

@param lumberjackOptions 不能为nil(至少要配置filePath) @param options 可以不传(此时为nil)

e.g.

logger, err := logrusKit.NewLumberjackLogger([]ioKit.LumberjackOption{
	ioKit.WithFilePath("a.log"),
	ioKit.WithConsole(true),
	//ioKit.WithMaxSize(1),
	ioKit.WithCompress(true),
})
if err != nil {
	logrus.Fatal(err)
}
for {
	logger.Info(666)
}

func NewRotateFileLogger deprecated

func NewRotateFileLogger(filePath string, rotationTime, maxAge time.Duration, softLinkFlag bool, formatter logrus.Formatter, level logrus.Level, toConsoleFlag bool) (*logrus.Logger, error)

NewRotateFileLogger

Deprecated: 不推荐使用.

PS: 如果 logger.Out 被释放后继续调用 logger 进行输出,会失败(e.g. 控制台os.Stderr有输出: Failed to write to log, invalid argument).

@param filePath 内部会做处理:

(1) 会尝试创建父级目录
(2) 文件不存在,会自动创建
(3) 文件存在:是个文件,追加在最后;是个目录,返回error

func NewTextFormatter

func NewTextFormatter(timestampFormat string) *logrus.TextFormatter

NewTextFormatter

PS: 外部在调用此方法后,建议调用: Logger.SetReportCaller(true)!!!

@param timestampFormat 可以为""(将采用默认值)

e.g. 日志输出 time=2023-03-23 16:46:23.398+08:00 level=info msg=[CHIMERA, PROCESS] pid: [8579]. func=PrintBasicDetails(logrusKit/basicDetails.go:17) time=2023-03-23 16:46:23.398+08:00 level=info msg=[CHIMERA, OS] os: [darwin]. func=PrintBasicDetails(logrusKit/basicDetails.go:20) time=2023-03-23 16:46:23.398+08:00 level=info msg=[CHIMERA, OS] arch: [arm64]. func=PrintBasicDetails(logrusKit/basicDetails.go:21)

func ParseLevel added in v2.0.12

func ParseLevel(str string) (logrus.Level, error)

ParseLevel string => logrus.Level

func PrintBasicDetails

func PrintBasicDetails()

PrintBasicDetails 输出服务器的基本信息(以便于甩锅)

func SetLevel

func SetLevel(logger *logrus.Logger, level logrus.Level)

func SetUp

func SetUp(config *Config) (err error)

SetUp

@param config 可以为nil

Types

type Config

type Config struct {
	Level      string `json:"level,default=debug"`
	PrintBasic bool   `json:"printBasic,default=true"`
}

type LoggerOption added in v2.0.12

type LoggerOption func(opts *loggerOptions)

func WithFormatter added in v2.0.12

func WithFormatter(formatter logrus.Formatter) LoggerOption

func WithLevel added in v2.0.12

func WithLevel(level logrus.Level) LoggerOption

func WithOutput added in v2.0.12

func WithOutput(output io.Writer) LoggerOption

func WithReportCaller added in v2.0.12

func WithReportCaller(reportCaller bool) LoggerOption

Jump to

Keyboard shortcuts

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