nlog

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2020 License: MIT Imports: 12 Imported by: 0

README

nlog

快速使用
opts := nlog.DefaultOptions()
logger := nlog.Init(opts)

logger.Info("Hello!")

默认按照日志文件体积(RotateBySize)方式切割。

默认配置:

RotateBy: RotateBySize // 日志文件按体积切割
MaxAge:10 // 日志保存时间
LogLevel: "debug" //日志等级
WriteBy: WriteSingle // 单日志文件
MaxSize: 100 // 日志最大体积(100mb),仅 RotateBySize 方式生效
MaxBackups: 10 // 最多日志保存份数,仅 RotateBySize 方式生效
Compress: true // 日志压缩,仅 RotateBySize 方式生效
InConsole: true // 输出在控制台
自定义日志滚动方式
  • 按照体积滚动
opts := []nlog.LogOption{
    nlog.SetLogPath("app.log"),
    nlog.SetRotateBy(RotateBySize),
    nlog.SetMaxSize(100), // 100m
    nlog.SetMaxAge(7), // 7天
    nlog.SetMaxBackups(10), // 10份
}
logger := nlog.Init(opts)

logger.Info("Hello!")
  • 按照日期滚动
opts := []nlog.LogOption{
    nlog.SetLogPath("app.log"),
    nlog.SetRotateBy(RotateByDate),
    nlog.SetRotateInterval(RotateIntervalDay), // 一天滚动一次
}
logger := nlog.Init(opts)

logger.Info("Hello!")
自定义等级日志输出方式

等级日志输出和日志滚动不冲突。

  • 单文件
opts := []nlog.LogOption{
    nlog.SetLogPath("app.log"),
    nlog.SetRotateBy(RotateBySize),
    nlog.SetMaxSize(100), // 100m
    nlog.SetMaxAge(7), // 7天
    nlog.SetMaxBackups(10), // 10份
    nlog.SetWriteBy(WriteSingle), // 全部日志等级都输出在一个日志文件
}
logger := nlog.Init(opts)

logger.Info("Hello!")
  • 正常信息与警告、错误信息区分

debug、info 输出一个文件,warn、error 输出一个文件;

warn、error 的日志文件以 .error 结尾。

opts := []nlog.LogOption{
    nlog.SetLogPath("app.log"),
    nlog.SetRotateBy(RotateBySize),
    nlog.SetMaxSize(100), // 100m
    nlog.SetMaxAge(7), // 7天
    nlog.SetMaxBackups(10), // 10份
    nlog.SetWriteBy(WriteLevel), // debug、info 输出一个文件,warn、error 输出一个文件。
}
logger := nlog.Init(opts)

logger.Info("Hello!")

Documentation

Index

Constants

View Source
const (
	RotateByDate RotateType = 1
	RotateBySize RotateType = 2

	WriteLevel  WriteType = 1
	WriteSingle WriteType = 2

	EncodeJson    LogEncodeType = "json"
	EncodeConsole LogEncodeType = "console"

	LevelCapital LevelEncoderType = 1
	LevelLower   LevelEncoderType = 2

	NoCaller    = 1
	ShortCaller = 2
	FullCaller  = 3
)
View Source
const (
	RotateIntervalMinute = "minute"
	RotateIntervalHour   = "hour"
	RotateIntervalDay    = "day"
	RotateIntervalMonth  = "month"
	RotateIntervalYear   = "year"
)
View Source
const (
	BYTE = 1 << (10 * iota)
	KILOBYTE
	MEGABYTE
	GIGABYTE
	TERABYTE
)

Variables

View Source
var (
	ErrorInvalidByteQuantity = fmt.Errorf(
		"byte quantity must be a positive integer with a unit of measurement like M, MB, MiB, G, GiB, or GB")

	ErrorInvalidSize = fmt.Errorf("value is greater than 1024")
)

Functions

func GetFileSuffix

func GetFileSuffix(filepath string, suffix string) string

func SizeToBytes

func SizeToBytes(s string) (int, error)

Types

type CallerType

type CallerType int

type KeyOptions

type KeyOptions struct {
	MessageKey    string `json:"message_key" yaml:"message_key" toml:"message_key"`
	LevelKey      string `json:"level_key" yaml:"level_key" toml:"level_key"`
	TimeKey       string `json:"time_key" yaml:"time_key" toml:"time_key"`
	NameKey       string `json:"name_key" yaml:"name_key" toml:"name_key"`
	CallerKey     string `json:"caller_key" yaml:"caller_key" toml:"caller_key"`
	StacktraceKey string `json:"stack_trace_key" yaml:"stack_trace_key" toml:"stack_trace_key"`
}

type LevelEncoderType

type LevelEncoderType int

type LogEncodeType

type LogEncodeType string

type LogOption

type LogOption func(*LogOptions)

func CallerKey

func CallerKey(key string) LogOption

func DefaultOptions

func DefaultOptions(logPath string) []LogOption

func LevelKey

func LevelKey(key string) LogOption

func NameKey

func NameKey(key string) LogOption

func SetCaller

func SetCaller(caller CallerType) LogOption

func SetCompress

func SetCompress(compress bool) LogOption

func SetHiddenDisplay

func SetHiddenDisplay(hiddenDisplay bool) LogOption

func SetLevelEncoder

func SetLevelEncoder(encoder LevelEncoderType) LogOption

func SetLogEncoder

func SetLogEncoder(encoder LogEncodeType) LogOption

func SetLogLevel

func SetLogLevel(logLevel string) LogOption

func SetLogPath

func SetLogPath(path string) LogOption

func SetMaxAge

func SetMaxAge(maxAge int) LogOption

func SetMaxBackups

func SetMaxBackups(maxBackups int) LogOption

func SetMaxSize

func SetMaxSize(maxSize int) LogOption

func SetMessageKey

func SetMessageKey(key string) LogOption

func SetRotateBy

func SetRotateBy(rotateType RotateType) LogOption

func SetRotateInterval

func SetRotateInterval(rotateInterval RotateInterval) LogOption

func SetStackTrace

func SetStackTrace(stackTrace bool) LogOption

func SetTimeEncoderLayout

func SetTimeEncoderLayout(layout string) LogOption

func SetWriteBy

func SetWriteBy(writeType WriteType) LogOption

func StacktraceKey

func StacktraceKey(key string) LogOption

func TimeKey

func TimeKey(key string) LogOption

type LogOptions

type LogOptions struct {
	LogPath           string           `json:"log_path" yaml:"log_path" toml:"log_path"`
	LogLevel          zapcore.Level    `json:"log_level" yaml:"log_level" toml:"log_level"`
	WriteBy           WriteType        `json:"write_by" yaml:"write_by" toml:"write_by"`
	MaxAge            int              `json:"max_age" yaml:"max_age" toml:"max_age"`
	HiddenDisplay     bool             `json:"in_console" yaml:"in_console" toml:"in_console"`
	RotateBy          RotateType       `json:"rotate_by" yaml:"rotate_by" toml:"rotate_by"`
	MaxSize           int              `json:"max_size" yaml:"max_size" toml:"max_size"`
	MaxBackups        int              `json:"max_backups" yaml:"max_backups" toml:"max_backups"`
	Compress          bool             `json:"compress" yaml:"compress" toml:"compress"`
	RotateInterval    RotateInterval   `json:"rotate_interval" yaml:"rotate_interval" toml:"rotate_interval"`
	Caller            CallerType       `json:"caller_type" yaml:"caller_type" toml:"caller_type"`
	StackTrace        bool             `json:"stack_trace" yaml:"stack_trace" toml:"stack_trace"`
	LogEncoder        LogEncodeType    `json:"log_encoder" yaml:"log_encoder" toml:"log_encoder"`
	LevelEncoder      LevelEncoderType `json:"level_encoder" yaml:"level_encoder" toml:"level_encoder"`
	TimeEncoderLayout string           `json:"time_encoder_layout" yaml:"time_encoder_layout" toml:"time_encoder_layout"`
	Keys              KeyOptions       `json:"keys" yaml:"keys" toml:"keys"`
}

type Logger added in v0.1.3

type Logger struct {
	// contains filtered or unexported fields
}

func Init

func Init(opts ...LogOption) *Logger

func InitByOptions

func InitByOptions(l *LogOptions) *Logger

开发者可以从配置文件读取配置生成 LogOptions 然后初始化。

func (*Logger) Debug added in v0.1.3

func (logger *Logger) Debug(msg string, args ...zap.Field)

func (*Logger) Debugf added in v0.1.3

func (logger *Logger) Debugf(format string, args ...interface{})

func (*Logger) Error added in v0.1.3

func (logger *Logger) Error(msg string, args ...zap.Field)

func (*Logger) Errorf added in v0.1.3

func (logger *Logger) Errorf(format string, args ...interface{})

func (*Logger) Fatal added in v0.1.3

func (logger *Logger) Fatal(msg string, args ...zap.Field)

func (*Logger) Fatalf added in v0.1.3

func (logger *Logger) Fatalf(format string, args ...interface{})

func (*Logger) Info added in v0.1.3

func (logger *Logger) Info(msg string, args ...zap.Field)

func (*Logger) Infof added in v0.1.3

func (logger *Logger) Infof(format string, args ...interface{})

func (*Logger) Sync added in v0.1.3

func (logger *Logger) Sync() error

func (*Logger) Warn added in v0.1.3

func (logger *Logger) Warn(msg string, args ...zap.Field)

func (*Logger) Warnf added in v0.1.3

func (logger *Logger) Warnf(format string, args ...interface{})

func (*Logger) With added in v0.1.3

func (logger *Logger) With(k string, v interface{}) zap.Field

func (*Logger) WithError added in v0.1.3

func (logger *Logger) WithError(err error) zap.Field

type RotateInterval

type RotateInterval string

func (RotateInterval) Format

func (t RotateInterval) Format() string

func (RotateInterval) RotationGap

func (t RotateInterval) RotationGap() time.Duration

type RotateType

type RotateType int

type WriteType

type WriteType int

Jump to

Keyboard shortcuts

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