logger

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package logger提供了功能丰富的日志记录功能。

Package rotator 提供了日志文件轮转的功能。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

func Debugf

func Debugf(format string, args ...interface{})

func Error

func Error(args ...interface{})

func Errorf

func Errorf(format string, args ...interface{})

func Fatal

func Fatal(args ...interface{})

func Fatalf

func Fatalf(format string, args ...interface{})

func Info

func Info(args ...interface{})

func Infof

func Infof(format string, args ...interface{})

func InitGlobalLogger

func InitGlobalLogger(c logger) (io.Closer, error)

initGlobalLogger 封装了创建和设置全局日志记录器的逻辑 它会配置默认的 logger,使其同时输出到控制台和轮转文件

func SetFormatter

func SetFormatter(formatter Formatter)

SetFormatter 设置默认 logger 的格式化器

func SetLevel

func SetLevel(level Level)

SetLevel 设置默认 logger 的级别

func SetOutput

func SetOutput(out io.Writer)

SetOutput 设置默认 logger 的输出

func Warn

func Warn(args ...interface{})

func Warnf

func Warnf(format string, args ...interface{})

Types

type Entry

type Entry struct {
	Logger  *Logger
	Time    time.Time
	Level   Level
	Message string
	Fields  Fields
	File    string
	Line    int
	Func    string
	// contains filtered or unexported fields
}

Entry 代表一个日志条目

func WithFields

func WithFields(fields Fields) *Entry

默认 logger 的快捷方法

func (*Entry) WithFields

func (e *Entry) WithFields(fields Fields) *Entry

WithFields 为日志条目添加结构化字段

type Fields

type Fields map[string]interface{}

Fields 是用于结构化日志的键值对类型

type Formatter

type Formatter interface {
	Format(*Entry) ([]byte, error)
}

Formatter 是日志格式化器的接口

type JSONFormatter

type JSONFormatter struct{}

JSONFormatter 将日志格式化为 JSON

func (*JSONFormatter) Format

func (f *JSONFormatter) Format(e *Entry) ([]byte, error)

Format 实现 Formatter 接口

type Level

type Level uint8

Level 定义了日志的级别类型

const (
	DebugLevel Level = iota
	InfoLevel
	WarnLevel
	ErrorLevel
	FatalLevel
)

定义了所有支持的日志级别

func (Level) String

func (l Level) String() string

levelToString 将日志级别转换为字符串

type LogRotator

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

LogRotator 实现了 io.WriteCloser 接口,用于按大小轮转日志文件。

func NewRotator

func NewRotator(filename string, maxSize int64, maxBackups int) (*LogRotator, error)

New 创建一个新的 LogRotator 实例。 filename: 日志文件的路径。 maxSize: 单个文件的最大大小(字节)。 maxBackups: 要保留的旧日志文件的最大数量。

func (*LogRotator) Close

func (r *LogRotator) Close() error

Close 实现了 io.Closer 接口。

func (*LogRotator) Write

func (r *LogRotator) Write(p []byte) (n int, err error)

Write 实现了 io.Writer 接口。

type Logger

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

Logger 是日志记录器的核心结构

func New

func New(opts ...Option) *Logger

New 创建一个新的 Logger 实例

func (*Logger) Debug

func (l *Logger) Debug(args ...interface{})

func (*Logger) Debugf

func (l *Logger) Debugf(format string, args ...interface{})

func (*Logger) Error

func (l *Logger) Error(args ...interface{})

func (*Logger) Errorf

func (l *Logger) Errorf(format string, args ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(args ...interface{})

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, args ...interface{})

func (*Logger) Info

func (l *Logger) Info(args ...interface{})

func (*Logger) Infof

func (l *Logger) Infof(format string, args ...interface{})

func (*Logger) Warn

func (l *Logger) Warn(args ...interface{})

func (*Logger) Warnf

func (l *Logger) Warnf(format string, args ...interface{})

func (*Logger) WithFields

func (l *Logger) WithFields(fields Fields) *Entry

WithFields 为 Logger 添加结构化字段,返回一个 Entry

type Option

type Option func(*Logger)

Option 是用于配置 Logger 的函数类型

func WithFormatter

func WithFormatter(formatter Formatter) Option

WithFormatter 设置格式化器

func WithLevel

func WithLevel(level Level) Option

WithLevel 设置日志级别

func WithOutput

func WithOutput(out io.Writer) Option

WithOutput 设置输出目标

type TextFormatter

type TextFormatter struct{}

TextFormatter 将日志格式化为纯文本

func (*TextFormatter) Format

func (f *TextFormatter) Format(e *Entry) ([]byte, error)

Format 实现 Formatter 接口

Jump to

Keyboard shortcuts

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