logger

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2022 License: MIT Imports: 7 Imported by: 2

README

logger

zap封装的日志库。

  • 支持终端打印和保存日志。
  • 支持日志文件自动切割。
  • 支持json格式和console日志格式输出。

使用示例

支持Debug、Info、Warn、Error、Panic、Fatal,也支持类似fmt.Printf打印日志,Debugf、Infof、Warnf、Errorf、Panicf、Fatalf

    // (1) 直接使用,默认会初始化
    logger.Info("this is info")
    logger.Warn("this is warn", logger.String("foo","bar"), logger.Int("size",10), logger.Any("obj",obj))
    logger.Error("this is error", logger.Err(err), logger.String("foo","bar"))

    // (2) 初始化后再使用
    logger.Init(
        logger.WithLevel("info"),     // 设置数据日志级别,默认是debug
        logger.WithFormat("json"),  // 设置输出格式,默认console
        logger.WithSave(true,         // 设置是否保存日志到本地,默认false
        //    logger.WithFileName("my.log"),      // 文件名称,默认"out.log"
        //    logger.WithFileMaxSize(5),              // 最大文件大小(MB),默认10
       //     logger.WithFileMaxBackups(5),        // 旧文件的最大个数,默认100
       //     logger.WithFileMaxAge(10),             // 旧文件的最大天数,默认30
       //     logger.WithFileIsCompression(true), // 是否压缩归档旧文件,默认false
        )
    )

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

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

Debug debug级别信息

func Debugf

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

Debugf 带格式化debug级别信息

func Error

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

Error error级别信息

func Errorf

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

Errorf 带格式化error级别信息

func Get

func Get() *zap.Logger

Get 获取logger对象

func GetWithSkip

func GetWithSkip(skip int) *zap.Logger

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

func Info

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

Info info级别信息

func Infof

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

Infof 带格式化info级别信息

func Init

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

Init 初始化打印设置 在终端打印debug级别日志示例:Init() 在终端打印info级别日志示例:Init(WithLevel("info")) 在终端打印json格式、debug级别日志示例:Init(WithFormat("json")) 把日志输出到文件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 panic级别信息

func Warn

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

Warn warn级别信息

func Warnf

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

Warnf 带格式化warn级别信息

func WithFields

func WithFields(fields ...Field) *zap.Logger

WithFields 携带字段信息

Types

type Field

type Field = zapcore.Field

Field 字段类型

func Any

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

Any 任意类型,如果是对象、slice、map等复合类型,使用Any

func Bool

func Bool(key string, val bool) Field

Bool bool类型

func Duration

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

Duration time.Duration类型

func Err

func Err(err error) Field

Err err类型

func Float64

func Float64(key string, val float64) Field

Float64 float64类型

func Int

func Int(key string, val int) Field

Int int类型

func Int64

func Int64(key string, val int64) Field

Int64 int64类型

func String

func String(key string, val string) Field

String string类型

func Stringer

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

Stringer stringer类型

func Time

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

Time time.Time类型

func Uint

func Uint(key string, val uint) Field

Uint uint类型

func Uint64

func Uint64(key string, val uint64) Field

Uint64 uint64类型

func Uintptr

func Uintptr(key string, val uintptr) Field

Uintptr uintptr类型

type FileOption

type FileOption func(*fileOptions)

FileOption set the file options.

func WithFileIsCompression

func WithFileIsCompression(isCompression bool) FileOption

WithFileIsCompression 自定义是否压缩归档旧文件

func WithFileMaxAge

func WithFileMaxAge(maxAge int) FileOption

WithFileMaxAge 保留旧文件的最大天数

func WithFileMaxBackups

func WithFileMaxBackups(maxBackups int) FileOption

WithFileMaxBackups 自定义保留旧文件的最大个数

func WithFileMaxSize

func WithFileMaxSize(maxSize int) FileOption

WithFileMaxSize 自定义最大文件大小(MB)

func WithFileName

func WithFileName(filename string) FileOption

WithFileName 自定义文件名称

type Option

type Option func(*options)

Option set the logger options.

func WithFormat

func WithFormat(format string) Option

WithFormat 设置输出日志格式,console或json

func WithLevel

func WithLevel(levelName string) Option

WithLevel 输出日志级别

func WithSave

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

WithSave 保存日志到指定文件

Jump to

Keyboard shortcuts

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