zapl

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2022 License: MIT Imports: 7 Imported by: 0

README

zapl zap logger with lumberjack

GoDoc Go.Dev reference codecov Tests Go Report Card Licence Tag

Features

Usage

Installation

Use go get.

    go get github.com/things-go/zapl

Then import the package into your own code.

    import "github.com/things-go/zapl"
Example
package main

import (
	"go.uber.org/zap"

	"github.com/things-go/zapl"
)

func main() {
	l := zapl.New(zapl.WithLevel("debug"))
	zapl.ReplaceGlobals(l.Sugar().With(zap.String("main", "debug")))

	zapl.Debug("hello")
}

References

License

This project is under MIT License. See the LICENSE file for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DPanic

func DPanic(args ...interface{})

DPanic uses fmt.Sprint to construct and log a message. In development, the logger then panics. (See DPanicLevel for details.)

func DPanicf

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

DPanicf uses fmt.Sprintf to log a templated message. In development, the logger then panics. (See DPanicLevel for details.)

func DPanicw

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

DPanicw logs a message with some additional context. In development, the logger then panics. (See DPanicLevel for details.) The variadic key-value pairs are treated as they are in With.

func Debug

func Debug(args ...interface{})

Debug uses fmt.Sprint to construct and log a message.

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 Desugar

func Desugar() *zap.Logger

Desugar unwraps a SugaredLogger, exposing the original Logger. Desugaring is quite inexpensive, so it's reasonable for a single application to use both Loggers and SugaredLoggers, converting between them on the boundaries of performance-sensitive code.

func Error

func Error(args ...interface{})

Error uses fmt.Sprint to construct and log a message.

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(args ...interface{})

Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.

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(args ...interface{})

Info uses fmt.Sprint to construct and log a message.

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 Named

func Named(name string) *zap.SugaredLogger

Named adds a sub-scope to the logger's name. See Logger.Named for details.

func New

func New(opts ...Option) *zap.Logger

New constructs a new Logger

func Panic

func Panic(args ...interface{})

Panic uses fmt.Sprint to construct and log a message, then panics.

func Panicf

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

Panicf uses fmt.Sprintf to log a templated message, then panics.

func Panicw

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

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

func ReplaceGlobals

func ReplaceGlobals(s *zap.SugaredLogger)

ReplaceGlobals replaces the global SugaredLogger,

func Sync

func Sync() error

Sync flushes any buffered log entries.

func Warn

func Warn(args ...interface{})

Warn uses fmt.Sprint to construct and log a message.

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 With

func With(args ...interface{}) *zap.SugaredLogger

With adds a variadic number of fields to the logging context. It accepts a mix of strongly-typed Field objects and loosely-typed key-value pairs. When processing pairs, the first element of the pair is used as the field key and the second as the field value.

For example,

 sugaredLogger.With(
   "hello", "world",
   "failure", errors.New("oh no"),
   Stack(),
   "count", 42,
   "user", User{Name: "alice"},
)

is the equivalent of

unsugared.With(
  String("hello", "world"),
  String("failure", "oh no"),
  Stack(),
  Int("count", 42),
  Object("user", User{Name: "alice"}),
)

Note that the keys in key-value pairs should be strings. In development, passing a non-string key panics. In production, the logger is more forgiving: a separate error is logged, but the key-value pair is skipped and execution continues. Passing an orphaned key triggers similar behavior: panics in development and errors in production.

Types

type Config

type Config struct {
	// Level 日志等级, debug,info,warn,error,dpanic,panic,fatal, 默认warn
	Level string `yaml:"level" json:"level"`
	// Format: 编码格式: json,console 默认json
	Format string `yaml:"format" json:"format"`
	// 编码器类型, 默认: LowercaseLevelEncoder
	// LowercaseLevelEncoder: 小写编码器
	// LowercaseColorLevelEncoder: 小写编码器带颜色
	// CapitalLevelEncoder: 大写编码器
	// CapitalColorLevelEncoder: 大写编码器带颜色
	EncodeLevel string `yaml:"encodeLevel" json:"encodeLevel"`
	// Adapter 输出适配器, file,console,multi,custom, 默认 console
	Adapter string `yaml:"adapter" json:"adapter"`
	// Stack 是否使能栈调试输出, 默认false
	Stack bool `yaml:"stack" json:"stack"`
	// Path 日志保存路径, 默认 empty, 即当前路径
	Path string `yaml:"path" json:"path"`
	// Writer 输出
	// 当 adapter=custom使用,如果为writer为空,将使用os.Stdout
	Writer []io.Writer `yaml:"-" json:"-"`
	// EncoderConfig 如果配置该项,则 EncodeLevel 将被覆盖
	EncoderConfig *zapcore.EncoderConfig `yaml:"-" json:"-"`

	// see https://github.com/natefinch/lumberjack
	// lumberjack.Logger
	// Filename 空字符使用默认, 默认<processname>-lumberjack.log
	Filename string `yaml:"filename" json:"filename"`
	// MaxSize 每个日志文件最大尺寸(MB), 默认100MB
	MaxSize int `yaml:"maxSize" json:"maxSize"`
	// MaxAge 日志文件保存天数, 默认0 不删除
	MaxAge int `yaml:"maxAge" json:"maxAge"`
	// MaxBackups 日志文件保存备份数, 默认0 都保存
	MaxBackups int `yaml:"maxBackups" json:"maxBackups"`
	// LocalTime 是否格式化时间戳, 默认UTC时间
	LocalTime bool `yaml:"localTime" json:"localTime"`
	// Compress 是否使用gzip压缩文件, 采用默认不压缩
	Compress bool `yaml:"compress" json:"compress"`
}

Config 日志配置

type Option

type Option func(c *Config)

Option An Option configures a Logger.

func WithAdapter

func WithAdapter(adapter string, writer ...io.Writer) Option

WithAdapter with adapter file,console,multi,custom writer: 当 adapter=custom使用,如果为writer为空,将使用os.Stdout 默认 console

func WithConfig

func WithConfig(cfg Config) Option

WithConfig with config

func WithEnableCompress

func WithEnableCompress() Option

WithEnableCompress with compress 是否使用gzip压缩文件, 采用默认不压缩

func WithEnableLocalTime

func WithEnableLocalTime() Option

WithEnableLocalTime with local time 是否格式化时间戳, 默认UTC时间

func WithEncodeLevel

func WithEncodeLevel(encodeLevel string) Option

WithEncodeLevel with EncodeLevel LowercaseLevelEncoder: 小写编码器 LowercaseColorLevelEncoder: 小写编码器带颜色 CapitalLevelEncoder: 大写编码器 CapitalColorLevelEncoder: 大写编码器带颜色 默认: LowercaseLevelEncoder

func WithEncoderConfig

func WithEncoderConfig(encoderConfig *zapcore.EncoderConfig) Option

EncoderConfig 如果配置该项,则 EncodeLevel 将被覆盖

func WithFilename

func WithFilename(filename string) Option

WithFilename with filename 空字符使用默认, 默认<processname>-lumberjack.log

func WithFormat

func WithFormat(format string) Option

WithFormat with format json,console 默认json

func WithLevel

func WithLevel(level string) Option

WithLevel with level debug,info,warn,error,dpanic,panic,fatal 默认warn

func WithMaxAge

func WithMaxAge(maxAge int) Option

WithMaxAge with max age 日志文件保存天数, 默认0 不删除

func WithMaxBackups

func WithMaxBackups(maxBackups int) Option

WithMaxBackups with max backup 日志文件保存备份数, 默认0 都保存

func WithMaxSize

func WithMaxSize(maxSize int) Option

WithMaxSize with max size 每个日志文件最大尺寸(MB), 默认100MB

func WithPath

func WithPath(path string) Option

WithPath with path 日志保存路径, 默认 empty, 即当前路径

func WithStack

func WithStack(stack bool) Option

WithStack with stack Stack 是否使能栈调试输出, 默认false

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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