logger

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: MIT Imports: 14 Imported by: 0

README

logger

zap logger with lumberjack

Go.Dev reference codecov Tests Go Report Card Licence Tag

Features

Usage

example

Installation

Use go get.

    go get github.com/things-go/logger

Then import the package into your own code.

    import "github.com/things-go/logger"
示例
package main

import (
	"context"

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

type ctxKey struct{}

func ExampleHook(ctx context.Context) logger.Field {
	s, ok := ctx.Value(ctxKey{}).(string)
	if !ok {
		return logger.Skip()
	}
	return logger.String("ctx_key1", s)
}
func TmpHook(ctx context.Context) logger.Field {
	return logger.String("tmp_key1", "tmp_val1")
}

func main() {
	l := logger.NewLogger(
		logger.WithLevel(logger.DebugLevel.String()),
		logger.WithFormat(logger.FormatJson),
	).
		SetCallerLevel(logger.WarnLevel)
	logger.ReplaceGlobals(l)
	logger.SetDefaultValuer(ExampleHook)

	ctx := context.WithValue(context.Background(), ctxKey{}, "ctx_val1")

	logger.WithValuer(TmpHook).DebugContext(ctx, "Debug1")
	logger.WithNewValuer(TmpHook).DebugContext(ctx, "Debug2")

	logger.SetLevel(logger.WarnLevel)
	logger.DebugContext(ctx, "Debug3")

	err := logger.SetLevelWithText(logger.DebugLevel.String())
	_ = err
	logger.DebugContext(ctx, "Debug4")

	// 先改成warn等级方便测试
	logger.SetLevel(logger.WarnLevel)
	if logger.Enabled(logger.InfoLevel) {
		logger.InfoContext(ctx, "Info1")
	}
	if logger.V(logger.InfoLevel) {
		logger.InfoContext(ctx, "Info2")
	}
	logger.SetLevel(logger.DebugLevel)

	logger.Named("叫个名字").DebugContext(ctx, " Debug5")

	logger.With(
		logger.String("name", "jack"),
		logger.Int("age", 18),
	).InfoContext(ctx, " Debug6")

	// `log.Print`风格的日志
	logger.DebugContext(ctx, "Debug")
	logger.InfoContext(ctx, "Info")
	logger.WarnContext(ctx, "Warn")
	logger.ErrorContext(ctx, "Error")
	logger.DPanicContext(ctx, "DPanic")

	// `log.Printf`风格的日志
	logger.DebugfContext(ctx, "Debugf: %s", "debug")
	logger.InfofContext(ctx, "Infof: %s", "info")
	logger.WarnfContext(ctx, "Warnf: %s", "warn")
	logger.ErrorfContext(ctx, "Errorf: %s", "error")
	logger.DPanicfContext(ctx, "DPanicf: %s", "dPanic")

	// 松散键值对风格的日志
	logger.DebugwContext(ctx, "Debugw", "k1", "v1", logger.String("k2", "v2"))
	logger.InfowContext(ctx, "Infow", "k1", "v1", logger.String("k2", "v2"))
	logger.WarnwContext(ctx, "Warnw", "k1", "v1", logger.String("k2", "v2"))
	logger.InfowContext(ctx, "Infow", "k1", "v1", logger.String("k2", "v2"))
	logger.ErrorwContext(ctx, "Errorw", "k1", "v1", logger.String("k2", "v2"))
	logger.DPanicwContext(ctx, "DPanicw", "k1", "v1", logger.String("k2", "v2"))

	// 纯结构型的日志
	logger.DebugxContext(ctx, "Debugx", logger.String("k1", "v1"), logger.String("k2", "v2"))
	logger.InfoxContext(ctx, "Infox", logger.String("k1", "v1"), logger.String("k2", "v2"))
	logger.WarnxContext(ctx, "Warnx", logger.String("k1", "v1"), logger.String("k2", "v2"))
	logger.InfoxContext(ctx, "Infox", logger.String("k1", "v1"), logger.String("k2", "v2"))
	logger.ErrorxContext(ctx, "Errorx", logger.String("k1", "v1"), logger.String("k2", "v2"))
	logger.DPanicxContext(ctx, "DPanicx", logger.String("k1", "v1"), logger.String("k2", "v2"))
}

License

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

Documentation

Index

Constants

View Source
const (
	DebugLevel  = zap.DebugLevel
	InfoLevel   = zap.InfoLevel
	WarnLevel   = zap.WarnLevel
	ErrorLevel  = zap.ErrorLevel
	DPanicLevel = zap.DPanicLevel
	PanicLevel  = zap.PanicLevel
	FatalLevel  = zap.FatalLevel
)

log level defined

View Source
const (
	AdapterConsole       = "console"        // console
	AdapterFile          = "file"           // file
	AdapterMulti         = "multi"          // file and console
	AdapterCustom        = "custom"         // custom io.Writer
	AdapterConsoleCustom = "console-custom" // console and custom io.Writer
	AdapterFileCustom    = "file-custom"    // file and custom io.Writer
	AdapterMultiCustom   = "multi-custom"   // file, console and custom io.Writer
)

adapter defined

View Source
const (
	FormatJson    = "json"
	FormatConsole = "console"
)

format defined

View Source
const (
	EncodeLevelLowercase      = "LowercaseLevelEncoder"      // 小写编码器
	EncodeLevelLowercaseColor = "LowercaseColorLevelEncoder" // 小写编码器带颜色
	EncodeLevelCapital        = "CapitalLevelEncoder"        // 大写编码器
	EncodeLevelCapitalColor   = "CapitalColorLevelEncoder"   // 大写编码器带颜色
)

encode level defined

Variables

This section is empty.

Functions

func DPanic

func DPanic(args ...any)

func DPanicContext

func DPanicContext(ctx context.Context, args ...any)

func DPanicf

func DPanicf(template string, args ...any)

func DPanicfContext

func DPanicfContext(ctx context.Context, template string, args ...any)

func DPanicw

func DPanicw(msg string, keysAndValues ...any)

func DPanicwContext

func DPanicwContext(ctx context.Context, msg string, keysAndValues ...any)

func DPanicx

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

func DPanicxContext

func DPanicxContext(ctx context.Context, msg string, fields ...Field)

func Debug

func Debug(args ...any)

func DebugContext

func DebugContext(ctx context.Context, args ...any)

func Debugf

func Debugf(template string, args ...any)

func DebugfContext

func DebugfContext(ctx context.Context, template string, args ...any)

func Debugw

func Debugw(msg string, keysAndValues ...any)

func DebugwContext

func DebugwContext(ctx context.Context, msg string, keysAndValues ...any)

func Debugx

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

func DebugxContext

func DebugxContext(ctx context.Context, msg string, fields ...Field)

func Enabled

func Enabled(lvl Level) bool

Enabled returns true if the given level is at or above this level.

func Error

func Error(args ...any)

func ErrorContext

func ErrorContext(ctx context.Context, args ...any)

func Errorf

func Errorf(template string, args ...any)

func ErrorfContext

func ErrorfContext(ctx context.Context, template string, args ...any)

func Errorw

func Errorw(msg string, keysAndValues ...any)

func ErrorwContext

func ErrorwContext(ctx context.Context, msg string, keysAndValues ...any)

func Errorx

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

func ErrorxContext

func ErrorxContext(ctx context.Context, msg string, fields ...Field)

func Fatal

func Fatal(args ...any)

func FatalContext

func FatalContext(ctx context.Context, args ...any)

func Fatalf

func Fatalf(template string, args ...any)

func FatalfContext

func FatalfContext(ctx context.Context, template string, args ...any)

func Fatalw

func Fatalw(msg string, keysAndValues ...any)

func FatalwContext

func FatalwContext(ctx context.Context, msg string, keysAndValues ...any)

func Fatalx

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

func FatalxContext

func FatalxContext(ctx context.Context, msg string, fields ...Field)

func Info

func Info(args ...any)

func InfoContext

func InfoContext(ctx context.Context, args ...any)

func Infof

func Infof(template string, args ...any)

func InfofContext

func InfofContext(ctx context.Context, template string, args ...any)

func Infow

func Infow(msg string, keysAndValues ...any)

func InfowContext

func InfowContext(ctx context.Context, msg string, keysAndValues ...any)

func Infox

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

func InfoxContext

func InfoxContext(ctx context.Context, msg string, fields ...Field)

func Logger

func Logger() *zap.Logger

Logger return internal logger

func Panic

func Panic(args ...any)

func PanicContext

func PanicContext(ctx context.Context, args ...any)

func Panicf

func Panicf(template string, args ...any)

func PanicfContext

func PanicfContext(ctx context.Context, template string, args ...any)

func Panicw

func Panicw(msg string, keysAndValues ...any)

func PanicwContext

func PanicwContext(ctx context.Context, msg string, keysAndValues ...any)

func Panicx

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

func PanicxContext

func PanicxContext(ctx context.Context, msg string, fields ...Field)

func PoolGet

func PoolGet() *fieldContainer

PoolGet selects an arbitrary item from the field Pool, removes it from the field Pool, and returns it to the caller. PoolGet may choose to ignore the field pool and treat it as empty. Callers should not assume any relation between values passed to PoolPut and the values returned by PoolGet.

NOTE: This function should be call PoolPut to give back. NOTE: You should know `sync.Pool` work principle ```go

fc := logger.PoolGet() defer logger.PoolPut(fc) fc.Fields = append(fc.Fields, logger.String("k1", "v1")) ... use fc.Fields

```

func PoolPut

func PoolPut(c *fieldContainer)

PoolPut adds x to the pool. NOTE: See PoolGet.

func ReplaceGlobals

func ReplaceGlobals(logger *Log)

ReplaceGlobals replaces the global Log only once.

func SetLevelWithText

func SetLevelWithText(text string) error

SetLevelWithText alters the logging level. ParseAtomicLevel set the logging level based on a lowercase or all-caps ASCII representation of the log level. If the provided ASCII representation is invalid an error is returned.

func Sugar

func Sugar() *zap.SugaredLogger

Sugar wraps the Logger to provide a more ergonomic, but slightly slower, API. Sugaring a Logger 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 Sync

func Sync() error

Sync flushes any buffered log entries.

func V

func V(lvl Level) bool

V returns true if the given level is at or above this level. same as Enabled

func Warn

func Warn(args ...any)

func WarnContext

func WarnContext(ctx context.Context, args ...any)

func Warnf

func Warnf(template string, args ...any)

func WarnfContext

func WarnfContext(ctx context.Context, template string, args ...any)

func Warnw

func Warnw(msg string, keysAndValues ...any)

func WarnwContext

func WarnwContext(ctx context.Context, msg string, keysAndValues ...any)

func Warnx

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

func WarnxContext

func WarnxContext(ctx context.Context, msg string, fields ...Field)

Types

type AtomicLevel

type AtomicLevel = zap.AtomicLevel

func New

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

New constructs a new Log

func NewAtomicLevel

func NewAtomicLevel() AtomicLevel

NewAtomicLevel creates an AtomicLevel with InfoLevel and above logging enabled.

func NewAtomicLevelAt

func NewAtomicLevelAt(l Level) AtomicLevel

NewAtomicLevelAt is a convenience function that creates an AtomicLevel and then calls SetLevel with the given level.

func ParseAtomicLevel

func ParseAtomicLevel(text string) (AtomicLevel, error)

ParseAtomicLevel parses an AtomicLevel based on a lowercase or all-caps ASCII representation of the log level. If the provided ASCII representation is invalid an error is returned.

func UnderlyingCallerLevel

func UnderlyingCallerLevel() AtomicLevel

UnderlyingCallerLevel get underlying caller level.

type CallerCore

type CallerCore struct {
	Skip         int
	SkipPackages []string
	Caller       func(depth int, skipPackages ...string) Field
	// contains filtered or unexported fields
}

func NewCallerCore

func NewCallerCore() *CallerCore

func (*CallerCore) AddSkip

func (c *CallerCore) AddSkip(callerSkip int) *CallerCore

AddSkip add the number of callers skipped by caller annotation.

func (*CallerCore) AddSkipPackage

func (c *CallerCore) AddSkipPackage(vs ...string) *CallerCore

AddSkipPackage add the caller skip package.

func (*CallerCore) Enabled

func (c *CallerCore) Enabled(lvl Level) bool

Enabled returns true if the given level is at or above this level.

func (*CallerCore) Level

func (c *CallerCore) Level() Level

Level returns the minimum enabled log level.

func (*CallerCore) SetLevel

func (c *CallerCore) SetLevel(lv Level) *CallerCore

SetLevel set the caller level.

func (*CallerCore) UnderlyingLevel

func (c *CallerCore) UnderlyingLevel() AtomicLevel

UnderlyingLevel get underlying level.

func (*CallerCore) UseExternalLevel

func (c *CallerCore) UseExternalLevel(l AtomicLevel) *CallerCore

UseExternalLevel use external level, which controller by user.

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,file-custom,console-custom,multi-custom 默认 console
	Adapter string `yaml:"adapter" json:"adapter"`
	// Stack 是否使能栈调试输出, 默认false
	Stack bool `yaml:"stack" json:"stack"`
	// Writer 输出
	// 当adapter有附带custom时, 如果为writer为空, 将使用os.Stdout
	Writer []io.Writer `yaml:"-" json:"-"`
	// EncoderConfig 如果配置该项,则 EncodeLevel 将被覆盖
	EncoderConfig *zapcore.EncoderConfig `yaml:"-" json:"-"`
	// 文件配置, 仅Adapter有file时有效
	File LumberjackFile `yaml:"file" json:"file"`
}

Config 日志配置

type Field

type Field = zap.Field

func Any

func Any(key string, val any) Field

func Binary

func Binary(key string, val []byte) Field

func Bool

func Bool(key string, val bool) Field

func Boolp

func Boolp(key string, val *bool) Field

func ByteString

func ByteString(key string, val []byte) Field

func Complex128

func Complex128(key string, val complex128) Field

func Complex128p

func Complex128p(key string, val *complex128) Field

func Complex64

func Complex64(key string, val complex64) Field

func Complex64p

func Complex64p(key string, val *complex64) Field

func DefaultCaller

func DefaultCaller(depth int, skipPackages ...string) Field

DefaultCaller caller.

func DefaultCallerFile

func DefaultCallerFile(depth int, skipPackages ...string) Field

DefaultCallerFile caller file.

func Dict

func Dict(key string, val ...Field) Field

func Duration

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

func Durationp

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

func Err

func Err(val error) Field

func Errors

func Errors(key string, val []error) Field

func Float32

func Float32(key string, val float32) Field

func Float32p

func Float32p(key string, val *float32) Field

func Float64

func Float64(key string, val float64) Field

func Float64p

func Float64p(key string, val *float64) Field

func Inline

func Inline(val ObjectMarshaler) Field

func Int

func Int(key string, val int) Field

func Int16

func Int16(key string, val int16) Field

func Int16p

func Int16p(key string, val *int16) Field

func Int32

func Int32(key string, val int32) Field

func Int32p

func Int32p(key string, val *int32) Field

func Int64

func Int64(key string, val int64) Field

func Int64p

func Int64p(key string, val *int64) Field

func Int8

func Int8(key string, val int8) Field

func Int8p

func Int8p(key string, val *int8) Field

func Intp

func Intp(key string, val *int) Field

func NamedError

func NamedError(key string, val error) Field

func Namespace

func Namespace(key string) Field

func Object

func Object(key string, val ObjectMarshaler) Field

func Reflect

func Reflect(key string, val any) Field

func Skip

func Skip() Field

func Stack

func Stack(key string) Field

func StackSkip

func StackSkip(key string, skip int) Field

func String

func String(key string, val string) Field

func Stringer

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

func Stringp

func Stringp(key string, val *string) Field

func Time

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

func Timep

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

func Uint

func Uint(key string, val uint) Field

func Uint16

func Uint16(key string, val uint16) Field

func Uint16p

func Uint16p(key string, val *uint16) Field

func Uint32

func Uint32(key string, val uint32) Field

func Uint32p

func Uint32p(key string, val *uint32) Field

func Uint64

func Uint64(key string, val uint64) Field

func Uint64p

func Uint64p(key string, val *uint64) Field

func Uint8

func Uint8(key string, val uint8) Field

func Uint8p

func Uint8p(key string, val *uint8) Field

func Uintp

func Uintp(key string, val *uint) Field

func Uintptr

func Uintptr(key string, val uintptr) Field

func Uintptrp

func Uintptrp(key string, val *uintptr) Field

type Level

type Level = zapcore.Level

func GetLevel

func GetLevel() Level

GetLevel returns the minimum enabled log level.

type Log

type Log struct {
	// contains filtered or unexported fields
}

Log wrap zap logger - methods named after the log level or ending in "Context" for log.Print-style logging - methods ending in "w" or "wContext" for loosely-typed structured logging - methods ending in "f" or "fContext" for log.Printf-style logging - methods ending in "x" or "xContext" for structured logging

func AddCallerSkip

func AddCallerSkip(callerSkip int) *Log

AddCallerSkip add the number of callers skipped by caller annotation.

func AddCallerSkipPackage

func AddCallerSkipPackage(vs ...string) *Log

AddCallerSkipPackage add the caller skip package.

func Named

func Named(name string) *Log

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

func NewLogger

func NewLogger(opts ...Option) *Log

NewLogger new logger 默认配置:

Level: 日志等级, 默认warn Format: 编码格式, 默认json EncodeLevel: 编码器类型, 默认LowercaseLevelEncoder Adapter: 默认输出适合器, 默认console` Stack: 是否使能栈调试输出, 默认false Path: 日志保存路径, 默认当前路径 Writer: 当adapter有附带custom时, 如果为writer为空, 将使用os.Stdout EncoderConfig: 如果配置该项,则 EncodeLevel 将被覆盖

文件日志切割配置(启用file时生效) Filename 空字符使用默认, 默认<processname>-lumberjack.log MaxSize 每个日志文件最大尺寸(MB), 默认100MB MaxAge 日志文件保存天数, 默认0 不删除 MaxBackups 日志文件保存备份数, 默认0 都保存 LocalTime 是否格式化时间戳, 默认UTC时间 Compress 是否使用gzip压缩文件, 采用默认不压缩

Caller相关 callerLevel caller日志级别, 默认warn callerSkip caller设置跳过深度, 默认0 callerSkipPackages caller设置跳过的包名, 默认空

func NewLoggerWith

func NewLoggerWith(logger *zap.Logger, lv AtomicLevel) *Log

NewLoggerWith new logger with zap logger and atomic level

func SetCallerLevel

func SetCallerLevel(lv Level) *Log

SetCallerLevel set the caller level.

func SetDefaultValuer

func SetDefaultValuer(vs ...Valuer) *Log

SetDefaultValuer set default field function, which hold always until you call XXXContext. suggest

func SetLevel

func SetLevel(lv Level) *Log

SetLevel alters the logging level.

func UseExternalCallerLevel

func UseExternalCallerLevel(lvl AtomicLevel) *Log

UseExternalCallerLevel use external caller level, which controller by user.

func With

func With(fields ...Field) *Log

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"),
   "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.

func WithNewValuer

func WithNewValuer(fs ...Valuer) *Log

WithNewValuer return log with new Valuer function without default Valuer.

func WithValuer

func WithValuer(vs ...Valuer) *Log

WithValuer with field function.

func (*Log) AddCallerSkip

func (l *Log) AddCallerSkip(callerSkip int) *Log

AddCallerSkip add the number of callers skipped by caller annotation.

func (*Log) AddCallerSkipPackage

func (l *Log) AddCallerSkipPackage(vs ...string) *Log

AddCallerSkipPackage add the caller skip package.

func (*Log) DPanic

func (l *Log) DPanic(args ...any)

DPanic see DPanicContext

func (*Log) DPanicContext

func (l *Log) DPanicContext(ctx context.Context, args ...any)

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

func (*Log) DPanicf

func (l *Log) DPanicf(template string, args ...any)

DPanicf see DPanicfContext

func (*Log) DPanicfContext

func (l *Log) DPanicfContext(ctx context.Context, template string, args ...any)

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

func (*Log) DPanicw

func (l *Log) DPanicw(msg string, keysAndValues ...any)

DPanicw see DPanicwContext

func (*Log) DPanicwContext

func (l *Log) DPanicwContext(ctx context.Context, msg string, keysAndValues ...any)

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

func (*Log) DPanicx

func (l *Log) DPanicx(msg string, fields ...Field)

DPanic see DPanicContext

func (*Log) DPanicxContext

func (l *Log) DPanicxContext(ctx context.Context, msg string, fields ...Field)

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

func (*Log) Debug

func (l *Log) Debug(args ...any)

Debug (see DebugContext)

func (*Log) DebugContext

func (l *Log) DebugContext(ctx context.Context, args ...any)

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

func (*Log) Debugf

func (l *Log) Debugf(template string, args ...any)

Debugf see DebugfContext

func (*Log) DebugfContext

func (l *Log) DebugfContext(ctx context.Context, template string, args ...any)

DebugfContext uses fmt.Sprintf to log a templated message.

func (*Log) Debugw

func (l *Log) Debugw(msg string, keysAndValues ...any)

Debugw see DebugwContext

func (*Log) DebugwContext

func (l *Log) DebugwContext(ctx context.Context, msg string, keysAndValues ...any)

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

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

s.With(fields).Debug(msg)

func (*Log) Debugx

func (l *Log) Debugx(msg string, fields ...Field)

Debug (see DebugContext)

func (*Log) DebugxContext

func (l *Log) DebugxContext(ctx context.Context, msg string, fields ...Field)

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

func (*Log) Enabled

func (l *Log) Enabled(lvl Level) bool

Enabled returns true if the given level is at or above this level.

func (*Log) Error

func (l *Log) Error(args ...any)

Error see ErrorContext

func (*Log) ErrorContext

func (l *Log) ErrorContext(ctx context.Context, args ...any)

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

func (*Log) Errorf

func (l *Log) Errorf(template string, args ...any)

Errorf see ErrorfContext

func (*Log) ErrorfContext

func (l *Log) ErrorfContext(ctx context.Context, template string, args ...any)

ErrorfContext uses fmt.Sprintf to log a templated message.

func (*Log) Errorw

func (l *Log) Errorw(msg string, keysAndValues ...any)

Errorw see ErrorwContext

func (*Log) ErrorwContext

func (l *Log) ErrorwContext(ctx context.Context, msg string, keysAndValues ...any)

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

func (*Log) Errorx

func (l *Log) Errorx(msg string, fields ...Field)

Error see ErrorContext

func (*Log) ErrorxContext

func (l *Log) ErrorxContext(ctx context.Context, msg string, fields ...Field)

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

func (*Log) Fatal

func (l *Log) Fatal(args ...any)

Fatal see FatalContext

func (*Log) FatalContext

func (l *Log) FatalContext(ctx context.Context, args ...any)

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

func (*Log) Fatalf

func (l *Log) Fatalf(template string, args ...any)

Fatalf see FatalfContext

func (*Log) FatalfContext

func (l *Log) FatalfContext(ctx context.Context, template string, args ...any)

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

func (*Log) Fatalw

func (l *Log) Fatalw(msg string, keysAndValues ...any)

func (*Log) FatalwContext

func (l *Log) FatalwContext(ctx context.Context, msg string, keysAndValues ...any)

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

func (*Log) Fatalx

func (l *Log) Fatalx(msg string, fields ...Field)

Fatal see FatalContext

func (*Log) FatalxContext

func (l *Log) FatalxContext(ctx context.Context, msg string, fields ...Field)

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

func (*Log) GetLevel

func (l *Log) GetLevel() Level

GetLevel returns the minimum enabled log level.

func (*Log) Info

func (l *Log) Info(args ...any)

Info see InfoContext

func (*Log) InfoContext

func (l *Log) InfoContext(ctx context.Context, args ...any)

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

func (*Log) Infof

func (l *Log) Infof(template string, args ...any)

Infof see InfofContext

func (*Log) InfofContext

func (l *Log) InfofContext(ctx context.Context, template string, args ...any)

InfofContext uses fmt.Sprintf to log a templated message.

func (*Log) Infow

func (l *Log) Infow(msg string, keysAndValues ...any)

Infow see InfowContext

func (*Log) InfowContext

func (l *Log) InfowContext(ctx context.Context, msg string, keysAndValues ...any)

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

func (*Log) Infox

func (l *Log) Infox(msg string, fields ...Field)

Info see InfoContext

func (*Log) InfoxContext

func (l *Log) InfoxContext(ctx context.Context, msg string, fields ...Field)

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

func (*Log) Log

func (l *Log) Log(ctx context.Context, level Level, args ...any)

func (*Log) Logf

func (l *Log) Logf(ctx context.Context, level Level, template string, args ...any)

func (*Log) Logger

func (l *Log) Logger() *zap.Logger

Logger return internal logger

func (*Log) Logw

func (l *Log) Logw(ctx context.Context, level Level, msg string, keysAndValues ...any)

func (*Log) Logx

func (l *Log) Logx(ctx context.Context, level Level, msg string, fields ...Field)

func (*Log) Named

func (l *Log) Named(name string) *Log

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

func (*Log) Panic

func (l *Log) Panic(args ...any)

Panic see PanicContext

func (*Log) PanicContext

func (l *Log) PanicContext(ctx context.Context, args ...any)

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

func (*Log) Panicf

func (l *Log) Panicf(template string, args ...any)

Panicf see PanicfContext

func (*Log) PanicfContext

func (l *Log) PanicfContext(ctx context.Context, template string, args ...any)

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

func (*Log) Panicw

func (l *Log) Panicw(msg string, keysAndValues ...any)

Panicw see PanicwContext

func (*Log) PanicwContext

func (l *Log) PanicwContext(ctx context.Context, msg string, keysAndValues ...any)

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

func (*Log) Panicx

func (l *Log) Panicx(msg string, fields ...Field)

Panic see PanicContext

func (*Log) PanicxContext

func (l *Log) PanicxContext(ctx context.Context, msg string, fields ...Field)

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

func (*Log) SetCaller

func (l *Log) SetCaller(f func(depth int, skipPackages ...string) Field) *Log

SetCaller set the caller function.

func (*Log) SetCallerLevel

func (l *Log) SetCallerLevel(lv Level) *Log

SetCallerLevel set the caller level.

func (*Log) SetDefaultValuer

func (l *Log) SetDefaultValuer(fs ...Valuer) *Log

SetDefaultValuer set default Valuer function, which hold always until you call XXXContext.

func (*Log) SetLevel

func (l *Log) SetLevel(lv Level) *Log

SetLevel alters the logging level.

func (*Log) SetLevelWithText

func (l *Log) SetLevelWithText(text string) error

SetLevelWithText alters the logging level. ParseAtomicLevel set the logging level based on a lowercase or all-caps ASCII representation of the log level. If the provided ASCII representation is invalid an error is returned. see zapcore.Level

func (*Log) Sugar

func (l *Log) Sugar() *zap.SugaredLogger

Sugar wraps the Logger to provide a more ergonomic, but slightly slower, API. Sugaring a Logger 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 (*Log) Sync

func (l *Log) Sync() error

Sync flushes any buffered log entries.

func (*Log) UnderlyingCallerLevel

func (l *Log) UnderlyingCallerLevel() AtomicLevel

UnderlyingCallerLevel get underlying caller level.

func (*Log) UseExternalCallerLevel

func (l *Log) UseExternalCallerLevel(lvl AtomicLevel) *Log

UseExternalCallerLevel use external caller level, which controller by user.

func (*Log) V

func (l *Log) V(lvl Level) bool

V returns true if the given level is at or above this level. same as Enabled

func (*Log) Warn

func (l *Log) Warn(args ...any)

Warn see WarnContext

func (*Log) WarnContext

func (l *Log) WarnContext(ctx context.Context, args ...any)

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

func (*Log) Warnf

func (l *Log) Warnf(template string, args ...any)

Warnf see WarnfContext

func (*Log) WarnfContext

func (l *Log) WarnfContext(ctx context.Context, template string, args ...any)

WarnfContext uses fmt.Sprintf to log a templated message.

func (*Log) Warnw

func (l *Log) Warnw(msg string, keysAndValues ...any)

Warnw see WarnwContext

func (*Log) WarnwContext

func (l *Log) WarnwContext(ctx context.Context, msg string, keysAndValues ...any)

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

func (*Log) Warnx

func (l *Log) Warnx(msg string, fields ...Field)

Warn see WarnContext

func (*Log) WarnxContext

func (l *Log) WarnxContext(ctx context.Context, msg string, fields ...Field)

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

func (*Log) With

func (l *Log) With(fields ...Field) *Log

With creates a child logger and adds structured context to it. Fields added to the child don't affect the parent, and vice versa.

func (*Log) WithNewValuer

func (l *Log) WithNewValuer(fs ...Valuer) *Log

WithNewValuer return log with new Valuer function without default Valuer.

func (*Log) WithValuer

func (l *Log) WithValuer(fs ...Valuer) *Log

WithValuer with Valuer function.

type LumberjackFile added in v0.2.0

type LumberjackFile struct {
	// Path 日志保存路径, 默认 empty, 即当前路径
	Path string `yaml:"path" json:"path"`
	// see https://github.com/natefinch/lumberjack
	// lumberjack.Log
	// 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"`
}

type ObjectMarshaler

type ObjectMarshaler = zapcore.ObjectMarshaler

type Option

type Option func(c *Config)

Option An Option configures a Log.

func WithAdapter

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

WithAdapter with adapter file,console(default),multi,custom,file-custom,console-custom,multi-custom writer: 当 adapter=custom 使用,如果为writer为空,将使用os.Stdout

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(default): 小写编码器 LowercaseColorLevelEncoder: 小写编码器带颜色 CapitalLevelEncoder: 大写编码器 CapitalColorLevelEncoder: 大写编码器带颜色

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(default) or console

func WithLevel

func WithLevel(level string) Option

WithLevel with level debug(default),info,warn,error,dpanic,panic,fatal

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

type Valuer

type Valuer func(ctx context.Context) Field

Valuer is returns a log value.

func App

func App(v string) Valuer

func Caller

func Caller(depth int, skipPackages ...string) Valuer

Caller returns a Valuer that returns a pkg/file:line description of the caller.

func Component

func Component(v string) Valuer

func File

func File(depth int, skipPackages ...string) Valuer

File returns a Valuer that returns a pkg/file:line description of the caller.

func FromAny

func FromAny(key string, vf func(context.Context) any) Valuer

func FromBinary

func FromBinary(key string, vf func(context.Context) []byte) Valuer

func FromBool

func FromBool(key string, vf func(context.Context) bool) Valuer

func FromBoolp

func FromBoolp(key string, vf func(context.Context) *bool) Valuer

func FromByteString

func FromByteString(key string, vf func(context.Context) []byte) Valuer

func FromComplex128

func FromComplex128(key string, vf func(context.Context) complex128) Valuer

func FromComplex128p

func FromComplex128p(key string, vf func(context.Context) *complex128) Valuer

func FromComplex64

func FromComplex64(key string, vf func(context.Context) complex64) Valuer

func FromComplex64p

func FromComplex64p(key string, vf func(context.Context) *complex64) Valuer

func FromDuration

func FromDuration(key string, vf func(context.Context) time.Duration) Valuer

func FromDurationp

func FromDurationp(key string, vf func(context.Context) *time.Duration) Valuer

func FromErr

func FromErr(vf func(context.Context) error) Valuer

func FromErrors

func FromErrors(key string, vf func(context.Context) []error) Valuer

func FromFloat32

func FromFloat32(key string, vf func(context.Context) float32) Valuer

func FromFloat32p

func FromFloat32p(key string, vf func(context.Context) *float32) Valuer

func FromFloat64

func FromFloat64(key string, vf func(context.Context) float64) Valuer

func FromFloat64p

func FromFloat64p(key string, vf func(context.Context) *float64) Valuer

func FromInt

func FromInt(key string, vf func(context.Context) int) Valuer

func FromInt16

func FromInt16(key string, vf func(context.Context) int16) Valuer

func FromInt16p

func FromInt16p(key string, vf func(context.Context) *int16) Valuer

func FromInt32

func FromInt32(key string, vf func(context.Context) int32) Valuer

func FromInt32p

func FromInt32p(key string, vf func(context.Context) *int32) Valuer

func FromInt64

func FromInt64(key string, vf func(context.Context) int64) Valuer

func FromInt64p

func FromInt64p(key string, vf func(context.Context) *int64) Valuer

func FromInt8

func FromInt8(key string, vf func(context.Context) int8) Valuer

func FromInt8p

func FromInt8p(key string, vf func(context.Context) *int8) Valuer

func FromIntp

func FromIntp(key string, vf func(context.Context) *int) Valuer

func FromNamedError

func FromNamedError(key string, vf func(context.Context) error) Valuer

func FromReflect

func FromReflect(key string, vf func(context.Context) any) Valuer

func FromString

func FromString(key string, vf func(context.Context) string) Valuer

func FromStringer

func FromStringer(key string, vf func(context.Context) fmt.Stringer) Valuer

func FromStringp

func FromStringp(key string, vf func(context.Context) *string) Valuer

func FromTime

func FromTime(key string, vf func(context.Context) time.Time) Valuer

func FromTimep

func FromTimep(key string, vf func(context.Context) *time.Time) Valuer

func FromUint

func FromUint(key string, vf func(context.Context) uint) Valuer

func FromUint16

func FromUint16(key string, vf func(context.Context) uint16) Valuer

func FromUint16p

func FromUint16p(key string, vf func(context.Context) *uint16) Valuer

func FromUint32

func FromUint32(key string, vf func(context.Context) uint32) Valuer

func FromUint32p

func FromUint32p(key string, vf func(context.Context) *uint32) Valuer

func FromUint64

func FromUint64(key string, vf func(context.Context) uint64) Valuer

func FromUint64p

func FromUint64p(key string, vf func(context.Context) *uint64) Valuer

func FromUint8

func FromUint8(key string, vf func(context.Context) uint8) Valuer

func FromUint8p

func FromUint8p(key string, vf func(context.Context) *uint8) Valuer

func FromUintp

func FromUintp(key string, vf func(context.Context) *uint) Valuer

func FromUintptr

func FromUintptr(key string, vf func(context.Context) uintptr) Valuer

func FromUintptrp

func FromUintptrp(key string, vf func(context.Context) *uintptr) Valuer

func ImmutAny

func ImmutAny(key string, v any) Valuer

func ImmutBinary

func ImmutBinary(key string, v []byte) Valuer

func ImmutBool

func ImmutBool(key string, v bool) Valuer

func ImmutBoolp

func ImmutBoolp(key string, v *bool) Valuer

func ImmutByteString

func ImmutByteString(key string, v []byte) Valuer

func ImmutComplex128

func ImmutComplex128(key string, v complex128) Valuer

func ImmutComplex128p

func ImmutComplex128p(key string, v *complex128) Valuer

func ImmutComplex64

func ImmutComplex64(key string, v complex64) Valuer

func ImmutComplex64p

func ImmutComplex64p(key string, v *complex64) Valuer

func ImmutDict

func ImmutDict(key string, val ...Field) Valuer

func ImmutDuration

func ImmutDuration(key string, v time.Duration) Valuer

func ImmutDurationp

func ImmutDurationp(key string, v *time.Duration) Valuer

func ImmutErr

func ImmutErr(val error) Valuer

func ImmutErrors

func ImmutErrors(key string, val []error) Valuer

func ImmutFloat32

func ImmutFloat32(key string, v float32) Valuer

func ImmutFloat32p

func ImmutFloat32p(key string, v *float32) Valuer

func ImmutFloat64

func ImmutFloat64(key string, v float64) Valuer

func ImmutFloat64p

func ImmutFloat64p(key string, v *float64) Valuer

func ImmutInline

func ImmutInline(val ObjectMarshaler) Valuer

func ImmutInt

func ImmutInt(key string, v int) Valuer

func ImmutInt16

func ImmutInt16(key string, v int16) Valuer

func ImmutInt16p

func ImmutInt16p(key string, v *int16) Valuer

func ImmutInt32

func ImmutInt32(key string, v int32) Valuer

func ImmutInt32p

func ImmutInt32p(key string, v *int32) Valuer

func ImmutInt64

func ImmutInt64(key string, v int64) Valuer

func ImmutInt64p

func ImmutInt64p(key string, v *int64) Valuer

func ImmutInt8

func ImmutInt8(key string, v int8) Valuer

func ImmutInt8p

func ImmutInt8p(key string, v *int8) Valuer

func ImmutIntp

func ImmutIntp(key string, v *int) Valuer

func ImmutNamedError

func ImmutNamedError(key string, val error) Valuer

func ImmutNamespace

func ImmutNamespace(key string) Valuer

func ImmutObject

func ImmutObject(key string, val ObjectMarshaler) Valuer

func ImmutReflect

func ImmutReflect(key string, v any) Valuer

func ImmutStack

func ImmutStack(key string) Valuer

func ImmutStackSkip

func ImmutStackSkip(key string, skip int) Valuer

func ImmutString

func ImmutString(key string, v string) Valuer

func ImmutStringer

func ImmutStringer(key string, v fmt.Stringer) Valuer

func ImmutStringp

func ImmutStringp(key string, v *string) Valuer

func ImmutTime

func ImmutTime(key string, v time.Time) Valuer

func ImmutTimep

func ImmutTimep(key string, v *time.Time) Valuer

func ImmutUint

func ImmutUint(key string, v uint) Valuer

func ImmutUint16

func ImmutUint16(key string, v uint16) Valuer

func ImmutUint16p

func ImmutUint16p(key string, v *uint16) Valuer

func ImmutUint32

func ImmutUint32(key string, v uint32) Valuer

func ImmutUint32p

func ImmutUint32p(key string, v *uint32) Valuer

func ImmutUint64

func ImmutUint64(key string, v uint64) Valuer

func ImmutUint64p

func ImmutUint64p(key string, v *uint64) Valuer

func ImmutUint8

func ImmutUint8(key string, v uint8) Valuer

func ImmutUint8p

func ImmutUint8p(key string, v *uint8) Valuer

func ImmutUintp

func ImmutUintp(key string, v *uint) Valuer

func ImmutUintptr

func ImmutUintptr(key string, v uintptr) Valuer

func ImmutUintptrp

func ImmutUintptrp(key string, v *uintptr) Valuer

func Kind

func Kind(v string) Valuer

func Module

func Module(v string) Valuer

func Package

func Package(v string) Valuer

Package returns a Valuer that returns an immutable Valuer which key is pkg

func RequestId

func RequestId(f func(c context.Context) string) Valuer

func Source

func Source(f func(c context.Context) string) Valuer

func TraceId

func TraceId(f func(c context.Context) string) Valuer

func Type

func Type(v string) Valuer

func Unit

func Unit(v string) Valuer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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