log

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

README

chaoslog

一个结合了logrous和file-rotatelogs功能的日志库 这里只是简单的封装了一下,并没有做太多的功能 (调用效率问题还有待验证)

1. 支持日志自定义时间自动切分,过期自动删除
2. 支持日志级别
3. 支持日志调用者信息打印
4. 支持日志级别颜色

Documentation

Overview

Package log 模块名: 模块名 功能描述: 描述 作者: yr 2023/5/22 0022 21:49 最后更新: yr 2023/5/22 0022 21:49

Package log 模块名: 日志格式化 功能描述: 日志格式化 作者: yr 2024/3/1 0001 11:12 最后更新: yr 2024/3/1 0001 11:12

Package log 模块名: 日志接口定义 功能描述: 日志接口 作者: yr 2024/3/1 0001 11:12 最后更新: yr 2024/3/1 0001 11:12

Package log 模块名: 模块名 功能描述: 描述 作者: yr 2024/3/2 0002 18:57 最后更新: yr 2024/3/2 0002 18:57

Index

Constants

View Source
const (
	ColorRed     = "1;31m"  // 红色
	ColorGreen   = "1;32m"  // 绿色
	ColorYellow  = "1;33m"  // 黄色
	ColorBlue    = "1;34m"  // 蓝色
	ColorMagenta = "1;35m"  // 紫色
	ColorCyan    = "1;36m"  // 天蓝色
	ColorWhite   = "1;37m"  // 白色
	ColorRedBg   = "41;37m" // 红底白字
)
View Source
const (
	PanicLevelStr = "panic"
	FatalLevelStr = "fatal"
	ErrorLevelStr = "error"
	WarnLevelStr  = "warn"
	InfoLevelStr  = "info"
	DebugLevelStr = "debug"
	TraceLevelStr = "trace"
)

Variables

View Source
var (
	DefaultRotationTimeErr = errors.New("rotationTime must >= 1min and <= 24hour")
	DefaultLogLevelErr     = errors.New("log level must <= 6")
)

Functions

func Release

func Release(logger ILogger) error

Types

type BufferPool

type BufferPool interface {
	Put(*bytes.Buffer)
	Get() *bytes.Buffer
}

type CallerHook

type CallerHook struct{}

CallerHook 增加调用者信息的钩子(由于调用者被集成到了Format函数中,所以这里暂时废弃)

func (CallerHook) Fire

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

func (CallerHook) Levels

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

type Fields

type Fields = logrus.Fields

type FormatFunction added in v1.0.4

type FormatFunction = logrus.LogFunction

type Formatter

type Formatter struct {
	logrus.Formatter

	Mu *sync.Mutex
	// FieldsOrder - default: fields sorted alphabetically
	FieldsOrder []string

	// TimestampFormat - default: time.StampMilli = "2006-01-02 15:04:05.000"
	TimestampFormat string

	// HideKeys - show [fieldValue] instead of [fieldKey:fieldValue]
	HideKeys bool

	// Colors - enable colors, default is disable
	Colors bool

	// TrimMessages - trim whitespaces on messages
	TrimMessages bool

	// NoCaller - disable print caller info
	NoCaller bool

	// FullCaller - print full caller info
	FullCaller bool

	// CustomCallerFormatter - set custom formatter for caller info
	CustomCallerFormatter func(*runtime.Frame) string
	// contains filtered or unexported fields
}

Formatter - logrus formatter, implements logrus.Formatter

func (*Formatter) Format

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

Format a log entry (2006-01-02 15:04:05.000 [DEBUG] (test.go:5 func test) aaa=1 bbb=2 this is message)

func (*Formatter) SetCallerDisable

func (f *Formatter) SetCallerDisable(status bool)

SetCallerDisable 关闭调用者信息打印(默认开启)

func (*Formatter) SetColors

func (f *Formatter) SetColors(colors bool)

SetColors 是否启用颜色(默认不启动)

func (*Formatter) SetFullCaller

func (f *Formatter) SetFullCaller(status bool)

SetFullCaller 开启详细调用者信息打印(默认关闭)

func (*Formatter) SetTimestampFormat

func (f *Formatter) SetTimestampFormat(timestampFormat string)

SetTimestampFormat 日期格式化样式(默认 2006-01-02 15:04:05.000)

type HookFunction added in v1.0.4

type HookFunction = logrus.Hook

type ILogger

type ILogger = logrus.ILogger

func New

func New(opts ...Option) ILogger

New creates a new Logger object.

func NewDefaultLogger

func NewDefaultLogger(filePath, fileName string, maxAge, rotationTime time.Duration, level string, withCaller, fullCaller, withColors, openStdout bool) (ILogger, error)

NewDefaultLogger 创建一个通用日志对象 filePath 日志输出目录 fileName 日志文件名(最终文件名会是 filePath/fileName_20060102150405.log)(fileName为空且开启标准输出的情况下默认输出到stdout,否则无任何输出) maxAge 最大存放时间(过期会自动删除) rotationTime 自动切分间隔(到期日志自动切换新文件) level 日志级别(小于设置级别的信息都会被记录打印,设置级别如果超出限制,默认日志等级为error,取值为panic fatal error warn info debug trace) withCaller 是否需要调用者信息 fullCaller 如果需要打印调用者信息,那么这个参数可以设置调用者信息的详细程度 withColors 是否需要信息的颜色(基本上只能用于linux的前台打印) openStdout 是否开启标准输出(如果fileName为空,且openStdout未开启,那么将不会有任何日志信息被记录) TODO 支持远程日志钩子函数,可以将日志发送到远程的日志记录器上(这个函数需要go出去执行,不能阻塞)

type Level

type Level = logrus.Level
const (
	// PanicLevel level, the highest level of severity. Logs and then calls panic with the
	// message passed to Debug, Info, ...
	PanicLevel Level = logrus.PanicLevel
	// FatalLevel level. Logs and then calls `Exit(1)`. It will exit even if the
	// logging level is set to Panic.
	FatalLevel Level = logrus.FatalLevel
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	// Commonly used for hooks to send errors to an error tracking service.
	ErrorLevel Level = logrus.ErrorLevel
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel Level = logrus.WarnLevel
	// InfoLevel level. General operational entries about what's going on inside the
	// application.
	InfoLevel Level = logrus.InfoLevel
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DebugLevel Level = logrus.DebugLevel
	// TraceLevel level. Designates finer-grained informational events than the Debug.
	TraceLevel Level = logrus.TraceLevel
)

These are the different logging levels. You can set the logging level to log on your instance of rusLogger, obtained with `logrus.New()`.

type Logger

type Logger = logrus.Logger

type Option

type Option func(*Logger)

func WithCaller

func WithCaller(caller bool) Option

WithCaller sets the rusLogger caller.

func WithColor

func WithColor(color bool) Option

WithColor sets the rusLogger color.

func WithFullCaller

func WithFullCaller(full bool) Option

WithFullCaller sets print full caller info

func WithHook

func WithHook(hook HookFunction) Option

func WithLevel

func WithLevel(level Level) Option

WithLevel sets the rusLogger level.

func WithOut

func WithOut(output io.Writer) Option

WithOut sets the rusLogger output.

func WithTimeFormat

func WithTimeFormat(formatStr string) Option

type ROption

type ROption func(o *options)

func WithMaxAge

func WithMaxAge(d time.Duration) ROption

WithMaxAge sets the Rotate max_age.

func WithPattern

func WithPattern(s string) ROption

WithPattern sets the Rotate pattern.

func WithRotationTime

func WithRotationTime(d time.Duration) ROption

WithRotationTime sets the Rotate rotation time.

type Rotate

type Rotate = rotatelogs.RotateLogs

Jump to

Keyboard shortcuts

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