log

package module
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2022 License: MIT Imports: 8 Imported by: 32

README

使用注意事项:

  1. TimePrecision 参数缺省时,由环境变量 CONF_LOG_TIME_FORMAT 用于设置日期格式,默认为: 2006-01-02T15:04:05.000
  2. 生产环境日志策略需调用 ProductionDefault 来设置,或者 参照此方法根据需要自己修改合适的日志参数
  3. 使用 ProductionDefault 进行生产环境日志设置时,环境变量 CONF_LOG_PATH 用于设置日志路径,默认为执行程序的当前目录下的 logs 目录
  4. 开发调试时,无需任何设置,可直接使用 log.Info() log.Debugf() 等方法

使用示例

package main

import (
	"github.com/hrygo/log"
	"go.uber.org/zap"
)

func main() {
	defer log.Sync() // 确保程序结束前日志flush存储

	hook := zap.Hooks(log.StdoutHooker)        // 添加日志钩子,注意用于异步存储日志到ElasticSearch等日志存储库,需自定义,此处仅为示例
	trace := zap.AddStacktrace(zap.ErrorLevel) // 添加调用栈,Error级别以上会打印
	caller := log.WithCaller(true)             // 添加调用方信息

	// 如果需求与log.ProductionDefault则可以直接调用 log.ProductionDefault(hook, caller, trace) 无需自定义ProductionDefault函数
	ProductionDefault(hook, caller, trace)

	log.Info("test", log.String("hello", "world"))
	log.Infof("hello %s", "world")
}

// ProductionDefault 参照 log.ProductionDefault 自定义的初始化函数
func ProductionDefault(opts ...log.Option) {
	var tops = []log.TeeOption{
		{
			Filename:      log.BasePath() + "all.log",
			TextFormat:    log.JsonFormat,
			TimePrecision: log.TimePrecisionMillisecond,
			Ropt: log.RotateOptions{
				MaxSize:    100,
				MaxAge:     30,
				MaxBackups: 100,
				Compress:   true,
			},
			Lef: func(lvl log.Level) bool {
				return lvl <= log.FatalLevel && lvl > log.DebugLevel
			},
		},
		{
			Filename:      log.BasePath() + "error.log",
			TextFormat:    log.ConsoleFormat,
			TimePrecision: log.TimePrecisionMillisecond,
			Ropt: log.RotateOptions{
				MaxSize:    10,
				MaxAge:     7,
				MaxBackups: 10,
				Compress:   false,
			},
			Lef: func(lvl log.Level) bool {
				return lvl > log.InfoLevel
			},
		},
	}

	logger := log.NewTeeWithRotate(tops, opts...)
	log.ResetDefault(logger)
}

Documentation

Index

Constants

View Source
const (
	InfoLevel   = zap.InfoLevel   // 0, default level
	WarnLevel   = zap.WarnLevel   // 1
	ErrorLevel  = zap.ErrorLevel  // 2
	DPanicLevel = zap.DPanicLevel // 3, PanicLevel used in development log
	PanicLevel  = zap.PanicLevel  // 4, PanicLevel logs a message, then panics
	FatalLevel  = zap.FatalLevel  // 5, FatalLevel logs a message, then calls os.Exit(1)
	DebugLevel  = zap.DebugLevel  // -1

	JsonFormat               = "json"
	ConsoleFormat            = "console"
	TimePrecisionSecond      = "second"
	TimePrecisionMillisecond = "millisecond"
)

Variables

View Source
var (
	Skip        = zap.Skip
	Binary      = zap.Binary
	Bool        = zap.Bool
	Boolp       = zap.Boolp
	ByteString  = zap.ByteString
	Complex128  = zap.Complex128
	Complex128p = zap.Complex128p
	Complex64   = zap.Complex64
	Complex64p  = zap.Complex64p
	Float64     = zap.Float64
	Float64p    = zap.Float64p
	Float32     = zap.Float32
	Float32p    = zap.Float32p
	Int         = zap.Int
	Intp        = zap.Intp
	Int64       = zap.Int64
	Int64p      = zap.Int64p
	Int32       = zap.Int32
	Int32p      = zap.Int32p
	Int16       = zap.Int16
	Int16p      = zap.Int16p
	Int8        = zap.Int8
	Int8p       = zap.Int8p
	String      = zap.String
	Stringp     = zap.Stringp
	Uint        = zap.Uint
	Uintp       = zap.Uintp
	Uint64      = zap.Uint64
	Uint64p     = zap.Uint64p
	Uint32      = zap.Uint32
	Uint32p     = zap.Uint32p
	Uint16      = zap.Uint16
	Uint16p     = zap.Uint16p
	Uint8       = zap.Uint8
	Uint8p      = zap.Uint8p
	Uintptr     = zap.Uintptr
	Uintptrp    = zap.Uintptrp
	Reflect     = zap.Reflect
	Namespace   = zap.Namespace
	Stringer    = zap.Stringer
	Time        = zap.Time
	Timep       = zap.Timep
	Stack       = zap.Stack
	StackSkip   = zap.StackSkip
	Duration    = zap.Duration
	Durationp   = zap.Durationp
	Any         = zap.Any

	WithCaller    = zap.WithCaller
	AddStacktrace = zap.AddStacktrace

	Info   = std.Info
	Warn   = std.Warn
	Error  = std.Error
	DPanic = std.DPanic
	Panic  = std.Panic
	Fatal  = std.Fatal
	Debug  = std.Debug

	Infof   = std.Sugar().Infof
	Warnf   = std.Sugar().Warnf
	Errorf  = std.Sugar().Errorf
	DPanicf = std.Sugar().DPanicf
	Panicf  = std.Sugar().Panicf
	Fatalf  = std.Sugar().Fatalf
	Debugf  = std.Sugar().Debugf
)

Functions

func BasePath

func BasePath() (path string)

func Default

func Default() *zap.Logger

func New

func New(writer io.Writer, level Level, opts ...Option) *zap.Logger

New create a new logger (not support log rotating).

func NewTeeWithRotate

func NewTeeWithRotate(tops []TeeOption, opts ...Option) *zap.Logger

func ProductionDefault

func ProductionDefault(opts ...Option)

ProductionDefault 设置默认生产日志策略 参照此方法根据需要自己修改合适的日志参数, 编写自己的初始化方法

func ResetDefault

func ResetDefault(l *zap.Logger)

ResetDefault not safe for concurrent use

func StdoutHooker

func StdoutHooker(entry zapcore.Entry) error

StdoutHooker 系统运行日志钩子函数示例 单条日志就是一个结构体格式,本函数拦截每一条日志,您可以进行后续处理,例如:推送到 ElasticSearch 日志库等

func Sync

func Sync()

Types

type Field

type Field = zap.Field

type Level

type Level = zapcore.Level

type LevelEnablerFunc

type LevelEnablerFunc func(lvl Level) bool

type Option

type Option = zap.Option

type RotateOptions

type RotateOptions struct {
	MaxSize    int  // 单个文件最大大小, 单位MB
	MaxAge     int  // 文件最长保存天数
	MaxBackups int  // 最大文件个数
	Compress   bool // 是否开启压缩
}

type TeeOption

type TeeOption struct {
	Filename      string               // 日志文件名
	TimePrecision string               // 记录日志时,相关的时间精度,该参数选项:second、millisecond,分别表示 秒 和 毫秒 ,默认为毫秒级别
	TextFormat    string               // 日志文本格式 console or json
	Ropt          RotateOptions        // 日志分隔轮转配置
	Level         zapcore.LevelEnabler // 日志级别生效级别
}

Jump to

Keyboard shortcuts

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