Categorygithub.com/things-go/logger
modulepackage
0.2.3
Repository: https://github.com/things-go/logger.git
Documentation: pkg.go.dev

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

# Packages

No description provided by the author

# Functions

AddCallerSkip add the number of callers skipped by caller annotation.
AddCallerSkipPackage add the caller skip package.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Caller returns a Valuer that returns a pkg/file:line description of the caller.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
DefaultCaller caller.
DefaultCallerFile caller file.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Enabled returns true if the given level is at or above this level.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
File returns a Valuer that returns a pkg/file:line description of the caller.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
GetLevel returns the minimum enabled log level.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Logger return internal logger.
No description provided by the author
No description provided by the author
No description provided by the author
Named adds a sub-scope to the logger's name.
No description provided by the author
No description provided by the author
New constructs a new Log.
NewAtomicLevel creates an AtomicLevel with InfoLevel and above logging enabled.
NewAtomicLevelAt is a convenience function that creates an AtomicLevel and then calls SetLevel with the given level.
No description provided by the author
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设置跳过的包名, 默认空.
NewLoggerWith new logger with zap logger and atomic level.
No description provided by the author
Package returns a Valuer that returns an immutable Valuer which key is pkg.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ParseAtomicLevel parses an AtomicLevel based on a lowercase or all-caps ASCII representation of the log level.
PoolGet selects an arbitrary item from the field Pool, removes it from the field Pool, and returns it to the caller.
PoolPut adds x to the pool.
No description provided by the author
ReplaceGlobals replaces the global Log only once.
No description provided by the author
SetCallerLevel set the caller level.
SetDefaultValuer set default field function, which hold always until you call XXXContext.
SetLevel alters the logging level.
SetLevelWithText alters the logging level.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Sugar wraps the Logger to provide a more ergonomic, but slightly slower, API.
Sync flushes any buffered log entries.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
UnderlyingCallerLevel get underlying caller level.
UnderlyingLogger underlying global logger.
No description provided by the author
UseExternalCallerLevel use external caller level, which controller by user.
V returns true if the given level is at or above this level.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
With adds a variadic number of fields to the logging context.
WithAdapter with adapter file,console(default),multi,custom,file-custom,console-custom,multi-custom writer: 当 adapter=custom 使用,如果为writer为空,将使用os.Stdout.
WithConfig with config.
WithEnableCompress with compress 是否使用gzip压缩文件, 采用默认不压缩.
WithEnableLocalTime with local time 是否格式化时间戳, 默认UTC时间.
WithEncodeLevel with EncodeLevel LowercaseLevelEncoder(default): 小写编码器 LowercaseColorLevelEncoder: 小写编码器带颜色 CapitalLevelEncoder: 大写编码器 CapitalColorLevelEncoder: 大写编码器带颜色.
EncoderConfig 如果配置该项,则 EncodeLevel 将被覆盖.
WithFilename with filename 空字符使用默认, 默认<processname>-lumberjack.log.
WithFormat with format json(default) or console.
WithLevel with level debug(default),info,warn,error,dpanic,panic,fatal.
WithMaxAge with max age 日志文件保存天数, 默认0 不删除.
WithMaxBackups with max backup 日志文件保存备份数, 默认0 都保存.
WithMaxSize with max size 每个日志文件最大尺寸(MB), 默认100MB.
WithNewValuer return log with new Valuer function without default Valuer.
WithPath with path 日志保存路径, 默认 empty, 即当前路径.
WithStack with stack Stack 是否使能栈调试输出, 默认false.
WithValuer with field function.

# Constants

console.
console and custom io.Writer.
custom io.Writer.
file.
file and custom io.Writer.
file and console.
file, console and custom io.Writer.
log level defined.
log level defined.
大写编码器.
大写编码器带颜色.
小写编码器.
小写编码器带颜色.
log level defined.
log level defined.
format defined.
format defined.
log level defined.
log level defined.
log level defined.

# Structs

No description provided by the author
Config 日志配置.
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.
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Option An Option configures a Log.
Valuer is returns a log value.