logger

package
v1.1.12 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 16 Imported by: 2

README

Zap Logger Wrapper

本封装提供一个基本的 zap logger 封装,其目的在于统一部分项目之间的日志形式。

基本使用

在具体模块中使用
package abc

import (
	"github.com/GuanceCloud/cliutils/logger"
)

var (
	// 最好在模块中将 log 初始化一下,这样比较保险
	log = logger.DefaultSLogger("abc")
)

// 模块初始化
func Init() {
	log = logger.SLogger("abc")
}

func foo() {
	log.Debug("this is debug message")
	log.Infof("this is info message from %d", 1024)
}
在项目中使用

一般而言,我在项目中使用,需要初始化一个 root-logger,该 root-logger 定义了全局的日志存放路径、日志等级等属性:

package main

import (
	"github.com/GuanceCloud/cliutils/logger"
)

func main() {
	r, err := logger.InitRoot(&logger.Option{
		Path: "/path/to/app/log", // 如果不填写路径,日志将出到 stdout
		Level: logger.DEBUG,      // 默认为 DEBUG
		Flags: logger.OPT_DEFAULT,// 开启了自动切割
	})
}

关于自动切割

默认情况下,会按照 32MB(大约) 一个文件来切割,最大保持 5 个切片,保存时长为 30 天。

提供环境变量来配置日志路径

调用 InitRoot() 时,如果传入的路径为空字符串,那么会尝试从 LOGGER_PATH 这个环境变量中获取有效的日志路径。某些情况下,可以将该路径设置成 /dev/null(UNIX) 或 nul(windows),用来屏蔽日志输出。

Documentation

Overview

Package logger wrapped zap as a basic logging implement.

Index

Constants

View Source
const (
	// 禁用 JSON 形式输出.
	OPT_ENC_CONSOLE = 1 //nolint:golint,stylecheck
	// 显示代码路径时,不显示全路径.
	OPT_SHORT_CALLER = 2 //nolint:stylecheck,golint
	// 日志写到 stdout.
	OPT_STDOUT = 4 //nolint:stylecheck,golint
	// 日志内容中追加颜色.
	OPT_COLOR = 8 //nolint:stylecheck,golint
	// 日志自动切割.
	OPT_ROTATE = 32 //nolint:stylecheck,golint
	// 默认日志 flags.
	OPT_DEFAULT = OPT_ENC_CONSOLE | OPT_SHORT_CALLER | OPT_ROTATE //nolint:stylecheck,golint

	DEBUG  = "debug"
	INFO   = "info"
	WARN   = "warn"
	ERROR  = "error"
	PANIC  = "panic"
	DPANIC = "dpanic"
	FATAL  = "fatal"
)
View Source
const (
	NameKeyMod   = "mod"
	NameKeyMsg   = "msg"
	NameKeyLevel = "lev"
	NameKeyTime  = "ts"
	NameKeyPos   = "pos"
)
View Source
const (
	STDOUT = "stdout" // log output to stdout
)

Variables

View Source
var (
	MaxSize    = 32 // MB
	MaxBackups = 5
	MaxAge     = 30 // day

)
View Source
var (
	SchemeTCP = "tcp"
	SchemeUDP = "udp"
)
View Source
var (
	StdoutColor bool
	StdoutLevel = DEBUG
)

Functions

func Close

func Close()

func InitCustomizeRoot added in v0.1.5

func InitCustomizeRoot(opt *Option) (*zap.Logger, error)

func InitRoot

func InitRoot(opt *Option) error

InitRoot used to setup global root logger, include

  • log level
  • log path
  • set to disk file(with or without rotate)
  • set to some remtoe TCP/UDP server
  • a bounch of other OPT_XXXs

func NewLoggerCtx added in v0.1.6

func NewLoggerCtx(opts ...CtxOption) *loggerCtx

func Reset

func Reset()

func SetGlobalRootLogger

func SetGlobalRootLogger(fpath, level string, options int) error

SetGlobalRootLogger deprecated, use InitRoot() instead.

func TotalSLoggers

func TotalSLoggers() int64

Types

type CtxOption added in v0.1.6

type CtxOption interface {
	// contains filtered or unexported methods
}

func EnableTrace added in v0.1.6

func EnableTrace() CtxOption

func WithParseTrace added in v0.1.6

func WithParseTrace(f ExtractTrace) CtxOption

func WithTraceKey added in v0.1.6

func WithTraceKey(tn string, sn string) CtxOption

func WithZapCore added in v0.1.6

func WithZapCore(core zapcore.Core) CtxOption

func WithZapOpts added in v0.1.6

func WithZapOpts(opts ...zap.Option) CtxOption

type ExtractTrace added in v0.1.6

type ExtractTrace func(ctx context.Context) Trace

type Logger

type Logger struct {
	*zap.SugaredLogger
}

func DefaultSLogger

func DefaultSLogger(name string) *Logger

func SLogger

func SLogger(name string) *Logger

type LoggerCtx added in v0.1.6

type LoggerCtx interface {
	Debugf(template string, args ...interface{})
	Infof(template string, args ...interface{})
	Warnf(template string, args ...interface{})
	Errorf(template string, args ...interface{})
	Debug(args ...interface{})
	Info(args ...interface{})
	Warn(args ...interface{})
	Error(args ...interface{})

	DebugfCtx(ctx context.Context, template string, args ...interface{})
	InfofCtx(ctx context.Context, template string, args ...interface{})
	WarnfCtx(ctx context.Context, template string, args ...interface{})
	ErrorfCtx(ctx context.Context, template string, args ...interface{})
	DebugCtx(ctx context.Context, template string, args ...interface{})
	InfoCtx(ctx context.Context, template string, args ...interface{})
	WarnCtx(ctx context.Context, template string, args ...interface{})
	ErrorCtx(ctx context.Context, template string, args ...interface{})

	Named(name string) LoggerCtx
	With(fields ...zap.Field) LoggerCtx
}

type Option

type Option struct {
	Path     string
	Level    string
	MaxSize  int
	Flags    int
	Compress bool
}

type Trace added in v0.1.6

type Trace struct {
	TraceID string
	SpanID  string
}

Jump to

Keyboard shortcuts

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