logger

package
v1.4.13 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package logger 是封装在 https://github.com/uber-go/zap 中的日志库

支持终端打印和日志保存 支持自动日志文件切割 支持 JSON 格式和控制台日志格式输出 支持 Debug, Info, Warn, Error, Panic, Fatal,也支持类似 fmt.Printf 的日志打印,Debugf, Infof, Warnf, Errorf, Panicf, Fatalf

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

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

Debug level information

func Debugf

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

Debugf format level information

func Error

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

Error level information

func Errorf

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

Errorf format level information

func Fatal

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

Fatal level information

func Fatalf

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

Fatalf format level information

func GGetFieldValue added in v1.3.12

func GGetFieldValue(field Field) interface{}

func Get

func Get() *zap.Logger

Get 获取日志记录器

func GetWithSkip

func GetWithSkip(skip int) *zap.Logger

GetWithSkip 获取 defaultLogger,设置跳过的调用者值,自定义显示的代码行数

func Info

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

Info level information

func Infof

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

Infof format level information

func Init

func Init(opts ...Option) (*zap.Logger, error)

Init 初始化日志设置 在终端打印 debug 级别日志,示例: Init() 在终端打印 info 级别日志,示例: Init(WithLevel("info")) 在终端打印 json 格式,debug 级别日志,示例: Init(WithFormat("json")) 带钩子的日志,示例: Init(WithHooks(func(zapcore.Entry) error{return nil})) 输出日志到文件 out.log,使用默认切割日志相关参数,debug 级别日志,示例: Init(WithSave()) 输出日志到指定文件,自定义设置日志文件切割参数,json 格式,debug 级别日志,示例: Init(

  WithFormat("json"),
  WithSave(true,

		WithFileName("my.log"),
		WithFileMaxSize(5),
		WithFileMaxBackups(5),
		WithFileMaxAge(10),
		WithFileIsCompression(true),
	))

func Panic

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

Panic level information

func ReplaceGRPCLoggerV2

func ReplaceGRPCLoggerV2(l *zap.Logger)

ReplaceGRPCLoggerV2 replace grpc logger v2

func Sync

func Sync() error

Sync flushing any buffered log entries, applications should take care to call Sync before exiting.

func ToJSON added in v1.3.20

func ToJSON(fields []zap.Field) string

func Warn

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

Warn level information

func Warnf

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

Warnf format level information

Types

type CustomHook added in v1.3.12

type CustomHook func(entry zapcore.Entry, fields []Field) error

CustomHook defines a custom hook function that can access log level, message and fields

type Field

type Field = zapcore.Field

Field type

func Any

func Any(key string, val interface{}) Field

Any type, if it is a composite type such as object, slice, map, etc., use Any

func Bool

func Bool(key string, val bool) Field

Bool type

func Duration

func Duration(key string, val time.Duration) Field

Duration type

func Err

func Err(err error) Field

Err type

func Float64

func Float64(key string, val float64) Field

Float64 type

func Int

func Int(key string, val int) Field

Int type

func Int32 added in v1.0.5

func Int32(key string, val int32) Field

Int32 type

func Int64

func Int64(key string, val int64) Field

Int64 type

func String

func String(key string, val string) Field

String type

func Stringer

func Stringer(key string, val fmt.Stringer) Field

Stringer type

func Time

func Time(key string, val time.Time) Field

Time type

func Uint

func Uint(key string, val uint) Field

Uint type

func Uint32 added in v1.0.5

func Uint32(key string, val uint32) Field

Uint32 type

func Uint64

func Uint64(key string, val uint64) Field

Uint64 type

func Uintptr

func Uintptr(key string, val uintptr) Field

Uintptr type

type FileOption

type FileOption func(*fileOptions)

FileOption set the file options.

func WithFileIsCompression

func WithFileIsCompression(isCompression bool) FileOption

WithFileIsCompression set whether to compress log files

func WithFileMaxAge

func WithFileMaxAge(maxAge int) FileOption

WithFileMaxAge set maximum number of days for old documents

func WithFileMaxBackups

func WithFileMaxBackups(maxBackups int) FileOption

WithFileMaxBackups set maximum number of old files

func WithFileMaxSize

func WithFileMaxSize(maxSize int) FileOption

WithFileMaxSize set maximum file size (MB)

func WithFileName

func WithFileName(filename string) FileOption

WithFileName set log filename

func WithLocalTime added in v1.1.18

func WithLocalTime(isLocalTime bool) FileOption

WithLocalTime set whether to use local time

func WithNoPrint added in v1.3.2

func WithNoPrint(noPrint bool) FileOption

func WithSaveDay added in v1.1.50

func WithSaveDay(isSaveDay bool) FileOption

type Option

type Option func(*options)

Option set the logger options.

func WithAsync added in v1.4.3

func WithAsync(enabled bool) Option

WithAsync enables asynchronous logging

func WithAsyncBufferSize added in v1.4.3

func WithAsyncBufferSize(size int) Option

WithAsyncBufferSize sets the buffer size for asynchronous logging (in bytes)

func WithAsyncFlushInterval added in v1.4.3

func WithAsyncFlushInterval(interval time.Duration) Option

WithAsyncFlushInterval sets the flush interval for asynchronous logging

func WithCustomHooks added in v1.3.12

func WithCustomHooks(hooks ...CustomHook) Option

WithCustomHooks sets custom hooks that can access fields data

func WithFormat

func WithFormat(format string) Option

WithFormat set the output log format, console or json

func WithHooks

func WithHooks(hooks ...func(zapcore.Entry) error) Option

WithHooks set the log hooks

func WithLevel

func WithLevel(levelName string) Option

WithLevel setting the log level

func WithSave

func WithSave(isSave bool, opts ...FileOption) Option

WithSave save log to file

Jump to

Keyboard shortcuts

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