logging

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FieldTime     = "time"
	FieldLevel    = "level"
	FieldMessage  = "message"
	FieldFile     = "file"
	FieldLine     = "line"
	FieldService  = "service"
	FieldMethod   = "method"
	FieldDuration = "duration_ms"
	FieldStatus   = "status"
	FieldError    = "error"
	FieldTraceID  = "trace_id"
	FieldSpanID   = "span_id"
)

公共字段名

Variables

This section is empty.

Functions

func GrpcStreamServerInterceptor

func GrpcStreamServerInterceptor(logger Logger) grpc.StreamServerInterceptor

GrpcStreamServerInterceptor 返回一个用于记录gRPC流式调用的拦截器

func GrpcUnaryClientInterceptor

func GrpcUnaryClientInterceptor(logger Logger) grpc.UnaryClientInterceptor

GrpcUnaryClientInterceptor 返回一个用于记录gRPC客户端一元调用的拦截器

func GrpcUnaryServerInterceptor

func GrpcUnaryServerInterceptor(logger Logger) grpc.UnaryServerInterceptor

GrpcUnaryServerInterceptor 返回一个用于记录gRPC一元调用的拦截器

func SetDefaultLogger

func SetDefaultLogger(logger Logger)

设置全局默认日志器

Types

type LogLevel

type LogLevel int

LogLevel 定义日志级别

const (
	// 调试信息
	LevelDebug LogLevel = iota
	// 普通信息
	LevelInfo
	// 警告信息
	LevelWarn
	// 错误信息
	LevelError
	// 严重错误
	LevelFatal
)

type Logger

type Logger interface {
	// 写入指定级别的日志
	Log(level LogLevel, msg string, fields map[string]interface{})

	// 调试日志
	Debug(msg string, fields map[string]interface{})
	// 普通信息
	Info(msg string, fields map[string]interface{})
	// 警告信息
	Warn(msg string, fields map[string]interface{})
	// 错误信息
	Error(msg string, fields map[string]interface{})
	// 严重错误
	Fatal(msg string, fields map[string]interface{})

	// 创建子日志
	With(fields map[string]interface{}) Logger

	// 设置日志级别
	SetLevel(level LogLevel)

	// 获取日志级别
	GetLevel() LogLevel
}

Logger 是结构化日志接口

func GetDefaultLogger

func GetDefaultLogger() Logger

获取全局默认日志器

func GetFileLogger

func GetFileLogger(filename string, level LogLevel) (Logger, error)

GetFileLogger 创建一个输出到文件的日志器

type Options

type Options struct {
	// 输出位置
	Writer io.Writer
	// 最低日志级别
	Level LogLevel
	// 是否包含调用位置
	IncludeLocation bool
	// 服务名称
	ServiceName string
	// 是否使用彩色输出
	EnableColor bool
	// 是否格式化JSON输出
	PrettyPrint bool
}

Options 定义日志选项

func DefaultOptions

func DefaultOptions() Options

DefaultOptions 返回默认日志选项

type StructuredLogger

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

StructuredLogger 实现结构化日志

func NewStructuredLogger

func NewStructuredLogger(opts Options) *StructuredLogger

NewStructuredLogger 创建新的结构化日志器

func (*StructuredLogger) Debug

func (l *StructuredLogger) Debug(msg string, fields map[string]interface{})

Debug 记录调试日志

func (*StructuredLogger) Error

func (l *StructuredLogger) Error(msg string, fields map[string]interface{})

Error 记录错误信息

func (*StructuredLogger) Fatal

func (l *StructuredLogger) Fatal(msg string, fields map[string]interface{})

Fatal 记录致命错误并退出

func (*StructuredLogger) GetLevel

func (l *StructuredLogger) GetLevel() LogLevel

GetLevel 获取日志级别

func (*StructuredLogger) Info

func (l *StructuredLogger) Info(msg string, fields map[string]interface{})

Info 记录普通信息

func (*StructuredLogger) Log

func (l *StructuredLogger) Log(level LogLevel, msg string, fields map[string]interface{})

Log 记录指定级别的日志

func (*StructuredLogger) SetLevel

func (l *StructuredLogger) SetLevel(level LogLevel)

SetLevel 设置日志级别

func (*StructuredLogger) Warn

func (l *StructuredLogger) Warn(msg string, fields map[string]interface{})

Warn 记录警告信息

func (*StructuredLogger) With

func (l *StructuredLogger) With(fields map[string]interface{}) Logger

With 创建带有额外字段的子日志器

Jump to

Keyboard shortcuts

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