zaplog

package
v0.0.0-...-643218b Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: MIT Imports: 7 Imported by: 9

README

zaplog

zap作为golang的日志库,以其卓越的性能已经被众多知名项目采用。但是我在使用zap的时候,官方使用方式总觉得很不习惯,并且还需要自己配置日志切割。因此为了方便日后的使用,依赖zap库的一些代码的方法,对zap库做了一层包装。

1. 日志输出到stdout

NewLogger(&LoggerConfiguration{
		Level:       "debug",
		Development: false,
		Encoding:    "text",
		OutputPath:  StdOut,
	})
log:
  level: "debug"
  development: false
  encoding: "text"
  output_path: "stdout"

2. 日志输出到文件,并支持日志轮转

NewLogger(&LoggerConfiguration{
		Level:       "debug",
		Development: false,
		Encoding:    "json",
		OutputPath:  "/data/logs/std.log",
		Rotate: LogRotate{
			MaxSizeMB:  1,
			MaxBackups: 3,
			Compress:   true,
		},
	})
log:
  level: "debug"
  development: false
  encoding: "text"
  output_path: "/data/logs/std.log"
  rotate:
    max_size_mb: 1
    max_backups: 3
    compress: true

Documentation

Index

Constants

View Source
const (
	DebugLevel = "debug"
	InfoLevel  = "info"
	WarnLevel  = "warn"
	ErrorLevel = "error"
	FatalLevel = "fatal"
)
View Source
const StdOut = "stdout"

Variables

This section is empty.

Functions

func Build

func Build(cfg zap.Config, rotate LogRotate, opts ...zap.Option) (*zap.Logger, error)

Build constructs a logger from the Config and Options.

func BuildEncoder

func BuildEncoder(encoding string, encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error)

func BuildOptions

func BuildOptions(cfg zap.Config) []zap.Option

func Close

func Close() error

NOTICE: Failure to call the change function will result in log loss Close calls the underlying Core's Sync method, flushing any buffered log entries. Applications should take care to call Sync before exiting.

func Debug

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

Debug logs a message at DebugLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func Debugf

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

Debugf uses fmt.Sprintf to log a templated message.

func Debugw

func Debugw(msg string, keysAndValues ...interface{})

Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

When debug-level logging is disabled, this is much faster than

s.With(keysAndValues).Debug(msg)

func Error

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

Error logs a message at ErrorLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func Errorf

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

Errorf uses fmt.Sprintf to log a templated message.

func Errorw

func Errorw(msg string, keysAndValues ...interface{})

Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func Fatal

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

Fatal logs a message at FatalLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

The logger then calls os.Exit(1), even if logging at FatalLevel is disabled.

func Fatalf

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

Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.

func Fatalw

func Fatalw(msg string, keysAndValues ...interface{})

Fatalw logs a message with some additional context, then calls os.Exit. The variadic key-value pairs are treated as they are in With.

func Info

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

Info logs a message at InfoLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func Infof

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

Infof uses fmt.Sprintf to log a templated message.

func Infow

func Infow(msg string, keysAndValues ...interface{})

Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func NewLogger

func NewLogger(config *LoggerConfiguration) error

func Warn

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

Warn logs a message at WarnLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func Warnf

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

Warnf uses fmt.Sprintf to log a templated message.

func Warnw

func Warnw(msg string, keysAndValues ...interface{})

Warnw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func WithOptions

func WithOptions(opts ...zap.Option)

Types

type Encoding

type Encoding string
const (
	TextEncoding Encoding = "text"
	JsonEncoding Encoding = "json"
)

type Level

type Level string

type LogRotate

type LogRotate struct {
	MaxSizeMB  int  `yaml:"max_size_mb"`
	MaxBackups int  `yaml:"max_backups"`
	Compress   bool `yaml:"compress"`
}

type LoggerConfiguration

type LoggerConfiguration struct {
	Level       Level     `yaml:"level"`
	Development bool      `yaml:"development"`
	Encoding    Encoding  `yaml:"encoding"`
	OutputPath  string    `yaml:"output_path"`
	Rotate      LogRotate `yaml:"rotate"`
}

func (*LoggerConfiguration) Default

func (config *LoggerConfiguration) Default()

func (*LoggerConfiguration) Validate

func (config *LoggerConfiguration) Validate() error

Jump to

Keyboard shortcuts

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