tlog

package module
v0.0.0-...-422129e Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

TLOG

对go.uber.org/zap日志库的包装

基本使用

在默认情况下,tlog仅会在console输出debug等级以上的日志

tlog.Info("this is an info level message")

tlog.WithField("name", "tlog").Info("this is an info level message with a field named name")

tlog.WithError(io.EOF).Error("this is an error level message with error io.EOF")

通过配置使用

用户可以通过名为config.yaml的配置文件来达到一定程度的日志自定义输出,以下是一个配置文件示例

log:
  - type: console
    level: debug
  - type: file
    level: debug
    prefix: tlog
  - type: file
    level: error
    prefix: tlog.error
    max-age: 24h
    rotation-time: 1h
    rotation-size: 1

配置支持的参数:

配置名称 类型 默认值 解释
type string / 支持console或file
console类型的日志即使配置了多个也只有第一个生效
level string info 日志等级:debug、info、warn、error、fatal
prefix string default 文件名前缀,必须全局唯一
若prefix为tmp,则文件名为tmp.log
(仅在type为file时生效)
max-age string 168h 文件最大保存时间,使用time.ParseDuration函数进行计算
(仅在type为file时生效)
rotation-time string 24h 文件切割时间间隔,使用time.ParseDuration函数进行计算
(仅在type为file时生效)
rotation-size Int / 文件最大大小,单位MB,不填写则不限制文件大小
(仅在type为file时生效)

Documentation

Overview

Package tlog zap日志库的包装

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string)

Debug 打印Debug等级的日志

func DebugContext

func DebugContext(ctx context.Context, msg string)

DebugContext 打印Debug等级的日志,优先使用ctx中的logger

func DebugContextf

func DebugContextf(ctx context.Context, format string, args ...interface{})

DebugContextf 使用fmt.Sprintf打印Debug等级的日志,优先使用ctx中的logger

func Debugf

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

Debugf 使用fmt.Sprintf打印Debug等级的日志

func Error

func Error(msg string)

Error 打印Error等级的日志

func ErrorContext

func ErrorContext(ctx context.Context, msg string)

ErrorContext 打印Error等级的日志,优先使用ctx中的logger

func ErrorContextf

func ErrorContextf(ctx context.Context, format string, args ...interface{})

ErrorContextf 使用fmt.Sprintf打印Error等级的日志,优先使用ctx中的logger

func Errorf

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

Errorf 使用fmt.Sprintf打印Error等级的日志

func Fatal

func Fatal(msg string)

Fatal 打印Fatal等级的日志

func FatalContext

func FatalContext(ctx context.Context, msg string)

FatalContext 打印Fatal等级的日志,优先使用ctx中的logger

func FatalContextf

func FatalContextf(ctx context.Context, format string, args ...interface{})

FatalContextf 使用fmt.Sprintf打印Fatal等级的日志,优先使用ctx中的logger

func Fatalf

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

Fatalf 使用fmt.Sprintf打印Fatal等级的日志

func Info

func Info(msg string)

Info 打印Info等级的日志

func InfoContext

func InfoContext(ctx context.Context, msg string)

InfoContext 打印Info等级的日志,优先使用ctx中的logger

func InfoContextf

func InfoContextf(ctx context.Context, format string, args ...interface{})

InfoContextf 使用fmt.Sprintf打印Info等级的日志,优先使用ctx中的logger

func Infof

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

Infof 使用fmt.Sprintf打印Info等级的日志

func Warn

func Warn(msg string)

Warn 打印Warn等级的日志

func WarnContext

func WarnContext(ctx context.Context, msg string)

WarnContext 打印Warn等级的日志,优先使用ctx中的logger

func WarnContextf

func WarnContextf(ctx context.Context, format string, args ...interface{})

WarnContextf 使用fmt.Sprintf打印Warn等级的日志,优先使用ctx中的logger

func Warnf

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

Warnf 使用fmt.Sprintf打印Warn等级的日志

Types

type Config

type Config struct {
	Type  string `yaml:"type"`  // 日志类型:console/file
	Level string `yaml:"level"` // 日志等级:debug/info/warn/error/fatal

	// 以下配置仅在类型为file时生效
	Prefix       string `yaml:"prefix"`        // 文件名前缀,例如prefix为tmp,则文件名为tmp.log
	MaxAge       string `yaml:"max-age"`       // 文件最大保存时间,使用time.ParseDuration函数进行计算
	RotationTime string `yaml:"rotation-time"` // 文件切割时间间隔,使用time.ParseDuration函数进行计算
	RotationSize int64  `yaml:"rotation-size"` // 文件最大大小,单位MB
}

Config 日志配置

type Fields

type Fields map[string]interface{}

Fields 用户定义的字段

type Logger

type Logger interface {
	// Debug 打印Debug等级的日志
	Debug(msg string)
	// Info 打印Info等级的日志
	Info(msg string)
	// Warn 打印Warn等级的日志
	Warn(msg string)
	// Error 打印Error等级的日志
	Error(msg string)
	// Fatal 打印Fatal等级的日志
	Fatal(msg string)

	// Debugf 使用fmt.Sprintf打印Debug等级的日志
	Debugf(template string, args ...interface{})
	// Infof 使用fmt.Sprintf打印Info等级的日志
	Infof(template string, args ...interface{})
	// Warnf 使用fmt.Sprintf打印Warn等级的日志
	Warnf(template string, args ...interface{})
	// Errorf 使用fmt.Sprintf打印Error等级的日志
	Errorf(template string, args ...interface{})
	// Fatalf 使用fmt.Sprintf打印Fatal等级的日志
	Fatalf(template string, args ...interface{})

	// WithField 向日志增加一个自定义字段
	WithField(key string, value interface{}) Logger
	// WithFields 向日志增加多个自定义字段
	WithFields(fields Fields) Logger
	// WithError 向日志增加error错误类型字段
	WithError(err error) Logger
	// Named 向日志器增加标题
	Named(name string) Logger
}

Logger 日志接口

func FromContext

func FromContext(ctx context.Context) (context.Context, Logger)

FromContext 从ctx中获取日志器,如果没有则创建新的日志器并返回新的context

func Named

func Named(name string) Logger

Named 向日志器增加标题

func New

func New(configs ...*Config) (Logger, error)

New 创建新的日志器

func NewWithCallerSkip

func NewWithCallerSkip(skip int, configs ...*Config) (Logger, error)

NewWithCallerSkip 创建新的日志器,额外加上skip调用者的个数

func ToContext

func ToContext(ctx context.Context, logger Logger) (context.Context, Logger)

ToContext 向ctx中新增日志器,如果logger为nil则按照默认选项创建新的logger并返回

func WithContextField

func WithContextField(ctx context.Context, key string, value interface{}) Logger

WithContextField 向ctx中的日志器中增加一个自定义字段,不会修改ctx中的日志器

func WithContextFields

func WithContextFields(ctx context.Context, fields Fields) Logger

WithContextFields 向ctx中的日志器中增加多个自定义字段,不会修改ctx中的日志器

func WithError

func WithError(err error) Logger

WithError 向日志增加error错误类型字段

func WithField

func WithField(key string, value interface{}) Logger

WithField 向日志增加一个自定义字段

func WithFields

func WithFields(fields Fields) Logger

WithFields 向日志增加多个自定义字段

Jump to

Keyboard shortcuts

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