go_logger

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: MIT Imports: 13 Imported by: 0

README

go-logger 基于 zap 结合 lumberjack rotate-logs

Quick Start

package main

import (
	glog "github.com/jianlu8023/go-logger"
)

func main() {
	logger := NewLogger(
		&Config{
			LogLevel:    "debug",
			DevelopMode: true,
			StackLevel:  "",
			ModuleName:  "[SDK]",
			Caller:      true,
		},
		WithRotateLog(&RotateLogConfig{
			FileName:  "./logs/rotatelog-logger.log",
			LocalTime: true,
		}),
		WithRotateLog(RotateLogDefaultConfig()),
		WithLumberjack(&LumberjackConfig{
			FileName: "./logs/lumberjack-logger.log",
		}),
		WithLumberjack(LumberjackDefaultConfig()),
		WithFileConfig(zapcore.EncoderConfig{
			MessageKey:     "msg",
			LevelKey:       "level",
			TimeKey:        "time",
			NameKey:        "logger",
			CallerKey:      "caller",
			StacktraceKey:  "stacktrace",
			FunctionKey:    "func",
			LineEnding:     zapcore.DefaultLineEnding,
			EncodeLevel:    glog.CustomColorCapitalLevelEncoder,
			EncodeTime:     glog.CustomTimeEncoder,
			EncodeDuration: zapcore.SecondsDurationEncoder,
			EncodeCaller:   zapcore.ShortCallerEncoder,
		}),
		WithConsoleConfig(zapcore.EncoderConfig{
			MessageKey:    "msg",
			LevelKey:      "level",
			TimeKey:       "time",
			NameKey:       "logger",
			CallerKey:     "caller",
			StacktraceKey: "stacktrace",
			FunctionKey:   "func",
			LineEnding:    zapcore.DefaultLineEnding,
			EncodeLevel:   CustomColorCapitalLevelEncoder,
			EncodeTime: func(date time.Time, encoder zapcore.PrimitiveArrayEncoder) {
				encoder.AppendString(date.Format("2006-01-02 15:04:05.000"))
			},
			EncodeDuration: zapcore.SecondsDurationEncoder,
			EncodeCaller:   zapcore.ShortCallerEncoder,
		}),
		WithConsoleFormat(),
	)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CustomCapitalLevelEncoder

func CustomCapitalLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder)

func CustomColorCapitalLevelEncoder

func CustomColorCapitalLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder)

func CustomTimeEncoder

func CustomTimeEncoder(time time.Time, encoder zapcore.PrimitiveArrayEncoder)

func NewLogger

func NewLogger(config *Config, options ...Option) *zap.Logger

func NewLumberjackUrl

func NewLumberjackUrl(config *LumberjackConfig) string

func NewRotateLogURL

func NewRotateLogURL(config *RotateLogConfig) string

func NewSugaredLogger

func NewSugaredLogger(config *Config, options ...Option) *zap.SugaredLogger

Types

type Config

type Config struct {
	LogLevel    string `json:"logLevel,omitempty"`
	DevelopMode bool   `json:"developMode,omitempty"`
	StackLevel  string `json:"stackLevel,omitempty"`
	ModuleName  string `json:"moduleName,omitempty"`
	Caller      bool   `json:"caller,omitempty"`
}

type LumberjackConfig

type LumberjackConfig struct {
	// 文件名
	FileName string `json:"fileName"`
	// MaxSize is the maximum size in megabytes of the log file before it gets
	// rotated. It defaults to 100 megabytes.
	MaxSize int `json:"maxSize"`
	// MaxBackups is the maximum number of old log files to retain.  The default
	// is to retain all old log files (though MaxAge may still cause them to get
	// deleted.)
	MaxBackups int `json:"maxBackups"`
	// MaxAge is the maximum number of days to retain old log files based on the
	// timestamp encoded in their filename.  Note that a day is defined as 24
	// hours and may not exactly correspond to calendar days due to daylight
	// savings, leap seconds, etc. The default is not to remove old log files
	// based on age.
	MaxAge int `json:"maxAge"`
	// Compress determines if the rotated log files should be compressed
	// using gzip. The default is not to perform compression.
	Compress bool `json:"compress"`
	// LocalTime determines if the time used for formatting the timestamps in
	// backup files is the computer's local time.  The default is to use UTC
	// time.
	Localtime bool `json:"localtime"`
}

func LumberjackDefaultConfig

func LumberjackDefaultConfig() *LumberjackConfig

func (*LumberjackConfig) String

func (l *LumberjackConfig) String() string

type Option

type Option interface {
	Name() string
	Value() interface{}
}

func WithConsoleConfig

func WithConsoleConfig(config zapcore.EncoderConfig) Option

func WithConsoleFormat

func WithConsoleFormat() Option

func WithConsoleOutPut

func WithConsoleOutPut() Option

func WithFileConfig

func WithFileConfig(config zapcore.EncoderConfig) Option

func WithFileOutPut

func WithFileOutPut() Option

func WithJSONFormat

func WithJSONFormat() Option

func WithLumberjack

func WithLumberjack(config *LumberjackConfig) Option

func WithRotateLog

func WithRotateLog(config *RotateLogConfig) Option

func WithZaplogfmtFormat

func WithZaplogfmtFormat() Option

type RotateLogConfig

type RotateLogConfig struct {
	FileName     string `json:"fileName,omitempty"`
	MaxAge       string `json:"maxAge,omitempty"`
	LocalTime    bool   `json:"localTime,omitempty"`
	RotationTime string `json:"rotationTime,omitempty"`
}

func RotateLogDefaultConfig

func RotateLogDefaultConfig() *RotateLogConfig

func (*RotateLogConfig) String

func (r *RotateLogConfig) String() string

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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