logger

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2024 License: MIT Imports: 8 Imported by: 0

README

logger

Logger library wrapped in zap.

  • Support for terminal printing and log saving.
  • Support for automatic log file cutting.
  • Support for json format and console log format output.
  • Support Debug, Info, Warn, Error, Panic, Fatal, also supports fmt.Printf-like log printing, Debugf, Infof, Warnf, Errorf, Panicf, Fatalf.

Example of use

    import  "gitee.com/the_sun_light/common_pkg/logger"

// (1) used directly, it will be initialised by default
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) Initialize and then use
logger.Init(
logger.WithLevel("info"), // set the data logging level, the default is debug
logger.WithFormat("json"), // set output format, default console
logger.WithSave(true,         // set whether to save the log locally, default false
//    logger.WithFileName("my.log"),      // file name, default is "out.log"
//    logger.WithFileMaxSize(5),              // maximum file size (MB), default 10
//     logger.WithFileMaxBackups(5),        // maximum number of old files, default 100
//     logger.WithFileMaxAge(10),             // maximum number of days for old documents, default 30
//     logger.WithFileIsCompression(true), // whether to compress and archive old files, default false
)
)
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"))

// (3) with hooks
logger.Init(
logger.WithLevel("info"),
logger.WithHooks(func (entry zapcore.Entry) error {
if strings.Contains(entry.Message, "error") {
fmt.Println("it contains error message")
}
return nil
}),
)
logger.Error("this is error", logger.Err(err), logger.String("foo", "bar"))

Documentation

Overview

Package logger is log library encapsulated in https://github.com/uber-go/zap

Support for terminal printing and log saving. Support for automatic log file cutting. Support for json format and console log format output. Supports Debug, Info, Warn, Error, Panic, Fatal, also supports fmt.Printf-like log printing, 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 Get

func Get() *zap.Logger

Get logger

func GetWithSkip

func GetWithSkip(skip int) *zap.Logger

GetWithSkip get defaultLogger, set the skipped caller value, customize the number of lines of code displayed

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 initial log settings print the debug level log in the terminal, example: Init() print the info level log in the terminal, example: Init(WithLevel("info")) print the json format, debug level log in the terminal, example: Init(WithFormat("json")) log with hooks, example: Init(WithHooks(func(zapcore.Entry) error{return nil})) output the log to the file out.log, using the default cut log-related parameters, debug-level log, example: Init(WithSave()) output the log to the specified file, custom set the log file cut log parameters, json format, debug level log, example: 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 Warn

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

Warn level information

func Warnf

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

Warnf format level information

func WithFields

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

WithFields carrying field information

Types

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 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 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

type Option

type Option func(*options)

Option set the logger options.

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