log

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Skip        = zap.Skip
	Binary      = zap.Binary
	Bool        = zap.Bool
	Boolp       = zap.Boolp
	ByteString  = zap.ByteString
	Complex128  = zap.Complex128
	Complex128p = zap.Complex128p
	Complex64   = zap.Complex64
	Complex64p  = zap.Complex64p
	Float64     = zap.Float64
	Float64p    = zap.Float64p
	Float32     = zap.Float32
	Float32p    = zap.Float32p
	Int         = zap.Int
	Intp        = zap.Intp
	Int64       = zap.Int64
	Int64p      = zap.Int64p
	Int32       = zap.Int32
	Int32p      = zap.Int32p
	Int16       = zap.Int16
	Int16p      = zap.Int16p
	Int8        = zap.Int8
	Int8p       = zap.Int8p
	String      = zap.String
	Stringp     = zap.Stringp
	Uint        = zap.Uint
	Uintp       = zap.Uintp
	Uint64      = zap.Uint64
	Uint64p     = zap.Uint64p
	Uint32      = zap.Uint32
	Uint32p     = zap.Uint32p
	Uint16      = zap.Uint16
	Uint16p     = zap.Uint16p
	Uint8       = zap.Uint8
	Uint8p      = zap.Uint8p
	Uintptr     = zap.Uintptr
	Uintptrp    = zap.Uintptrp
	Reflect     = zap.Reflect
	Namespace   = zap.Namespace
	Stringer    = zap.Stringer
	Time        = zap.Time
	Timep       = zap.Timep
	Stack       = zap.Stack
	StackSkip   = zap.StackSkip
	Duration    = zap.Duration
	Durationp   = zap.Durationp
	Any         = zap.Any
)

Functions

func AddHook added in v0.3.0

func AddHook(hook LogHook)

AddHook 添加全局日志钩子 使用示例:

type MyHook struct {}
func (h *MyHook) Levels() []zapcore.Level {
    return []zapcore.Level{zapcore.ErrorLevel, zapcore.FatalLevel}
}
func (h *MyHook) Fire(entry *log.LogEntry) error {
    // 发送到飞书、企业微信等
    return nil
}
log.AddHook(&MyHook{})

func CtxError added in v0.1.0

func CtxError(msg string, fields ...Field)

func CtxInfo added in v0.1.0

func CtxInfo(msg string, fields ...Field)

func DPanic

func DPanic(msg string, fields ...Field)

func Debug

func Debug(msg string, fields ...Field)

func Error

func Error(msg string, fields ...Field)

func Fatal

func Fatal(msg string, fields ...Field)

func Ginzap

func Ginzap(logger *zap.Logger, timeFormat string, utc bool) gin.HandlerFunc

Ginzap returns a gin.HandlerFunc (middleware) that logs requests using uber-go/zap.

Requests with errors are logged using zap.Error(). Requests without errors are logged using zap.Info().

It receives:

  1. A time package format string (e.g. time.RFC3339).
  2. A boolean stating whether to use UTC time zone or local.

func Info

func Info(msg string, fields ...Field)

包级日志函数:未初始化时降级为控制台输出,任何时候调用都不会 panic 直接调用底层 zap logger(基础 CallerSkip=1 恰好越过本函数),caller 指向业务代码

func InitAccessLogger

func InitAccessLogger(filename, level string, maxSize, maxBackups, maxAge int) *zap.Logger

InitAccessLogger 创建 access 日志 logger(独立实例,不修改全局 logger)

func InitLogger

func InitLogger(filename, level string, maxSize, maxBackups, maxAge int, debug bool) (err error)

InitLogger 初始化全局 Logger

func LevelToNum

func LevelToNum(text string) zapcore.Level

func NewAccessLogger added in v0.3.0

func NewAccessLogger(conf *config.Config) *zap.Logger

NewAccessLogger 从配置创建 access 日志 logger(独立实例,不影响全局 logger)

func Panic

func Panic(msg string, fields ...Field)

func RecoveryWithZap

func RecoveryWithZap(stack bool) gin.HandlerFunc

RecoveryWithZap returns a gin.HandlerFunc (middleware) that recovers from any panics and logs requests using uber-go/zap. All errors are logged using zap.Error(). stack means whether output the stack info. The stack info is easy to find where the error occurs but the stack info is too large.

func RemoveAllHooks added in v0.3.0

func RemoveAllHooks()

RemoveAllHooks 移除所有钩子

func SetLevel added in v0.4.0

func SetLevel(level string) error

SetLevel 动态调整全局日志级别(无需重启)。 level 支持 debug/info/warn/error 等,解析失败时返回错误且不改变当前级别。 igo.NewApp 会自动把它挂到配置热重载回调上:改配置文件里的 local.logger.level 即时生效。

func Sync

func Sync() error

func Warn

func Warn(msg string, fields ...Field)

Types

type Field

type Field = zap.Field

type Level

type Level = zapcore.Level
const (
	InfoLevel   Level = zap.InfoLevel   // 0, default level
	WarnLevel   Level = zap.WarnLevel   // 1
	ErrorLevel  Level = zap.ErrorLevel  // 2
	DPanicLevel Level = zap.DPanicLevel // 3, used in development log
	// PanicLevel logs a message, then panics
	PanicLevel Level = zap.PanicLevel // 4
	// FatalLevel logs a message, then calls os.Exit(1).
	FatalLevel Level = zap.FatalLevel // 5
	DebugLevel Level = zap.DebugLevel // -1
)

func GetLevel added in v0.4.0

func GetLevel() Level

GetLevel 返回当前生效的日志级别

type Log

type Log struct {
}

Log

func NewLog

func NewLog(conf *config.Config) (*Log, error)

NewLog

type LogConfig

type LogConfig struct {
	Level      string `json:"level"`
	Filename   string `json:"filename"`
	MaxSize    int    `json:"maxsize"`
	MaxAge     int    `json:"max_age"`
	MaxBackups int    `json:"max_backups"`
}

type LogEntry added in v0.3.0

type LogEntry struct {
	Level     zapcore.Level  // 日志级别
	Message   string         // 日志消息
	Fields    map[string]any // 日志字段
	Timestamp time.Time      // 时间戳
	TraceID   string         // 追踪ID(如果存在)
}

LogEntry 日志条目

type LogHook added in v0.3.0

type LogHook interface {
	// Levels 返回该钩子关注的日志级别
	Levels() []zapcore.Level
	// Fire 当有匹配级别的日志时触发
	// 注意:该方法会在独立的goroutine中异步执行,不会阻塞日志记录
	Fire(entry *LogEntry) error
}

LogHook 日志钩子接口 业务层可以实现此接口来处理特定级别的日志,例如发送到飞书、企业微信等

type Logger

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

func (*Logger) DPanic

func (l *Logger) DPanic(msg string, fields ...Field)

func (*Logger) Debug

func (l *Logger) Debug(msg string, fields ...Field)

func (*Logger) Error

func (l *Logger) Error(msg string, fields ...Field)

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, fields ...Field)

func (*Logger) Info

func (l *Logger) Info(msg string, fields ...Field)

func (*Logger) Panic

func (l *Logger) Panic(msg string, fields ...Field)

func (*Logger) Sync

func (l *Logger) Sync() error

func (*Logger) Warn

func (l *Logger) Warn(msg string, fields ...Field)

Jump to

Keyboard shortcuts

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