log

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package log 日志打印输出

Example Usage

log.MustSetup(c) // 使用配置初始化日志
log.Info("hello %s", nickname) // Info 日志
log.Error("error %s", err.String()) // Error 日志
log.WithContext(ctx) // 设置 trace span 等附加信息

Index

Constants

View Source
const (
	DefaultLevel      = INFO
	DefaultService    = "Unknown"
	DefaultTimeFormat = "15:04:05.000"
)
View Source
const (
	ModuleName = "Default"
)

Variables

View Source
var (
	// ErrInvalidConfig 不支持的日志设置
	ErrInvalidConfig = errors.New("invalid log config")
)

Functions

func Debug

func Debug(message string, param ...interface{})

Debug 打印 Debug 信息

func Error

func Error(message string, param ...interface{})

Error 打印 Error 信息

func Info

func Info(message string, param ...interface{})

Info 打印 Info 信息

func MustSetup

func MustSetup(c *Config)

MustSetup 通过配置初始化日志,失败会使当前线程崩溃

在大多数场景下,推荐使用这个函数初始化/改动日志设置

func RegisterLoggerType

func RegisterLoggerType(name string, f NewLoggerFunc)

RegisterLoggerType 注册一个新的日志类型

通过这个函数,可以轻松扩展这个库

func SetLevel

func SetLevel(level Level)

SetLevel 设置日志等级

func SetLogger

func SetLogger(logger Logger)

SetLogger 设置日志实例

我们更推荐使用 MustSetup

func Setup

func Setup(c *Config) error

Setup 通过配置初始化日志,失败会返回 error

func Warning

func Warning(message string, param ...interface{})

Warning 打印 Warning 信息

func WithContext

func WithContext(ctx context.Context)

WithContext 打印 WithContext 信息

Types

type Config

type Config struct {
	Type        string `json:",default=default,options=default|zero"`
	Level       string `json:",default=info,options=info|debug|warn|error"`
	ServiceName string `json:",optional"`
	TimeFormat  string `json:",optional"`
}

Config 日志配置

我们推荐用配置文件的方式设置日志

type Level

type Level uint8

Level 日志等级

const (
	DEBUG Level = iota
	INFO
	WARNING
	ERROR
)

type Logger

type Logger interface {
	// Debug 打印 Debug 信息
	Debug(string, ...interface{})
	// Info 打印 Info 信息
	Info(string, ...interface{})
	// Warning 打印 Warning 信息
	Warning(string, ...interface{})
	// Error 打印 Error 信息
	Error(string, ...interface{})
	// SetLevel 设置日志等级
	SetLevel(Level)
	// WithContext 设置日志上下文(通常是用作设置 trace span 信息,可以用作扩展功能的信息传递)
	WithContext(ctx context.Context)
}

Logger 日志处理抽象

type NewLoggerFunc

type NewLoggerFunc func(service string, timeFormat string) Logger

NewLoggerFunc 创建日志实例的函数

Jump to

Keyboard shortcuts

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