logger

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

README

日志组件

  • 基于go-micro原组件 asim/go-micro/plugins/logger/zerolog

  • 基于原logger组件接口编写,兼容原组件

  • 添加配置项

    • 指定日志输出文件名
    配置项: OutputFilePath
    参数不为空,就可以输出到对应文件(OutputFileName如果为空,则输出到os.Stdout)
    示例:logger.NewLogger(logger.OutputFilePath("debug")) 
    // 生成文件为:{OutputRootPath}/debug/20210603/15.log,格式:{OutputRootPath}/路径名/日期/小时.log
    
    • 指定日志根目录
    配置项:OutputRootPath
    默认为当前目录下log目录(不存在会新建)
    示例:logger.NewLogger(logger.OutputRootPath("./log/"))
    
    • 配置项结构体
    type Options struct {
    
        ······
    
        // Output file name
        // If set, output to a file
        OutputFilePath string
        // Output root path
        OutputRootPath string
    }
    
  • 原组件需要设置Option为:Development 时,才会显示debug的消息,不够灵活,重写一下

func (l *zeroLogger) Init(opts ...logger.Option) error {
······
	switch l.opts.Mode {
// 这里判断了环境
	case Development:
······
		//level = logger.DebugLevel
		l.zLog = zerolog.New(consOut).
// 开启了环境才输出debuglevel的日志
			Level(zerolog.DebugLevel).
			With().Timestamp().Stack().Logger()

// 默认是不输出debuglevel日志的
	default: // Production
		zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
		l.zLog = zerolog.New(l.opts.Out).
			Level(zerolog.InfoLevel).
			With().Timestamp().Stack().Logger()
	}
······
}

Documentation

Index

Constants

View Source
const FORMAT = "20060102"

FORMAT Date

Variables

View Source
var (
	// LevelTraceValue is the value used for the trace level field.
	LevelTraceValue = "trace"
	// LevelDebugValue is the value used for the debug level field.
	LevelDebugValue = "debug"
	// LevelInfoValue is the value used for the info level field.
	LevelInfoValue = "info"
	// LevelWarnValue is the value used for the warn level field.
	LevelWarnValue = "warn"
	// LevelErrorValue is the value used for the error level field.
	LevelErrorValue = "error"
	// LevelFatalValue is the value used for the fatal level field.
	LevelFatalValue = "fatal"
	// LevelPanicValue is the value used for the panic level field.
	LevelPanicValue = "panic"
)

Functions

func Debug

func Debug(args ...interface{})

func Debugf

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

func Error

func Error(args ...interface{})

func Errorf

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

func Fatal

func Fatal(args ...interface{})

func Fatalf

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

func Info

func Info(args ...interface{})

func Infof

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

func Init

func Init(opts ...Option) error

func Log

func Log(level Level, v ...interface{})

func Logf

func Logf(level Level, format string, v ...interface{})

func String

func String() string

func Trace

func Trace(args ...interface{})

func Tracef

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

func Warn

func Warn(args ...interface{})

func Warnf

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

Types

type Level

type Level int8

Level defines log levels.

const (
	// DebugLevel defines debug log level.
	DebugLevel Level = iota
	// InfoLevel defines info log level.
	InfoLevel
	// WarnLevel defines warn log level.
	WarnLevel
	// ErrorLevel defines error log level.
	ErrorLevel
	// FatalLevel defines fatal log level.
	FatalLevel
	// PanicLevel defines panic log level.
	PanicLevel
	// NoLevel defines an absent log level.
	NoLevel
	// Disabled disables the logger.
	Disabled

	// TraceLevel defines trace log level.
	TraceLevel Level = -1
)

func (Level) String

func (l Level) String() string

type Logger

type Logger interface {
	// Init initialises options
	Init(options ...Option) error
	// The Logger options
	Options() Options
	// Fields set fields to always be logged
	Fields(fields map[string]interface{}) Logger
	// Log writes a log entry
	Log(level Level, v ...interface{})
	// Logf writes a formatted log entry
	Logf(level Level, format string, v ...interface{})
	// String returns the name of logger
	String() string
}

Logger is a generic logging interface

var DefaultLogger Logger = NewLogger()

func Fields

func Fields(fields map[string]interface{}) Logger

func NewLogger

func NewLogger(opts ...Option) Logger

NewLogger builds a new logger based on options

type Mode

type Mode uint8
const (
	Production Mode = iota
	Development
)

type Option

type Option func(*Options)

func OutputFilePath

func OutputFilePath(filename string) Option

func OutputRootPath

func OutputRootPath(rootPath string) Option

func ReportCaller

func ReportCaller() Option

func SetOption

func SetOption(k, v interface{}) Option

func SplitLogByHour

func SplitLogByHour() Option

func UseAsDefault

func UseAsDefault() Option

func WithDevelopmentMode

func WithDevelopmentMode() Option

func WithExitFunc

func WithExitFunc(exit func(int)) Option

func WithHooks

func WithHooks(hooks []zerolog.Hook) Option

func WithProductionMode

func WithProductionMode() Option

func WithTimeFormat

func WithTimeFormat(timeFormat string) Option

type Options

type Options struct {
	// It's common to set this to a file, or leave it default which is `os.Stderr`
	Out io.Writer
	// The logging level the logger should log at. default is `InfoLevel`
	Level Level
	// fields to always be logged
	Fields  map[string]interface{}
	Context context.Context

	// Flag for whether to log caller info (off by default)
	ReportCaller bool
	// Use this logger as system wide default logger  (off by default)
	UseAsDefault bool
	// zerolog hooks
	Hooks []zerolog.Hook
	// TimeFormat is one of time.RFC3339, time.RFC3339Nano, time.*
	TimeFormat string
	// Runtime mode. (Production by default)
	Mode Mode
	// Exit Function to call when FatalLevel log
	ExitFunc func(int)
	// Output file name
	// If set, output to a file
	OutputFilePath string
	// Output root path
	OutputRootPath string
	// Split the log by the hour
	SplitLogByHour bool
}

Jump to

Keyboard shortcuts

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