logger

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 11 Imported by: 0

README

Logger

logger 包定义了基础的日志级别类型。

内容

LogLevel

定义了日志级别字符串类型:

  • Info:信息(info
  • Error:错误(error
  • Success:成功(success
使用示例
import "github.com/fireflycore/go-micro/logger"

func Log(level logger.LogLevel, msg string) {
	_ = level
	_ = msg
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConsoleCore added in v1.0.3

func NewConsoleCore(level zapcore.LevelEnabler) zapcore.Core

NewConsoleCore 构造一个输出到 stdout 的 console encoder core。

设计要点: - 采用 ProductionEncoderConfig,字段与 zap 默认生产格式保持一致 - 通过自定义 EncodeTime/EncodeLevel/EncodeCaller,把输出变成更利于人读的形式 - 返回的 Core 可与 JSON Core 通过 zapcore.NewTee 合并

func NewRemoteCore added in v1.0.3

func NewRemoteCore(level zapcore.LevelEnabler, handle func(b []byte)) zapcore.Core

NewRemoteCore 构造一个远端输出 core。

该 core 的目标是减少额外编解码:直接在 core.Write 中组装目标 JSON,并调用 handle。

func NewZapLogger added in v1.0.3

func NewZapLogger(conf *Conf, handle func(b []byte)) *zap.Logger

New 构造一个 zap.Logger

- Console=true 时输出到 stdout(面向人读) - Remote=true 且提供 handle 时输出 JSON 到 handle(面向机器解析) - 两者都未启用时返回 Nop logger,避免 nil 引用

Types

type AccessLogger added in v0.8.3

type AccessLogger struct {
	Method   uint32 `json:"method"`
	Path     string `json:"path"`
	Request  string `json:"request"`
	Response string `json:"response"`
	Duration uint64 `json:"duration"`
	Status   uint32 `json:"status"`

	ClientIp        string `json:"client_ip"`
	SourceIp        string `json:"source_ip"`
	SourceIpAddress string `json:"source_ip_address"`

	ClientType    uint32 `json:"client_type"`
	ClientName    string `json:"client_name"`
	ClientVersion string `json:"client_version"`

	SystemType    uint32 `json:"system_type"`
	SystemName    string `json:"system_name"`
	SystemVersion string `json:"system_version"`
	AppVersion    string `json:"app_version"`

	InvokeServiceAppId    string `json:"invoke_service_app_id"`
	InvokeServiceEndpoint string `json:"invoke_service_endpoint"`
	TargetServiceAppId    string `json:"target_service_app_id"`
	TargetServiceEndpoint string `json:"target_service_endpoint"`

	TraceId  string `json:"trace_id"`
	ParentId string `json:"parent_id"`
	SpanId   string `json:"span_id"`

	UserId   string `json:"user_id"`
	AppId    string `json:"app_id"`
	TenantId string `json:"tenant_id"`
}

AccessLogger 表示访问日志。

type AsyncLogger added in v1.0.4

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

AsyncLogger 用于把日志写入异步队列,然后由后台 goroutine 调用 handle 消费。

该类型同时实现 io.Writer,可直接作为 zap 的写入目标。

func NewAsyncLogger added in v1.0.4

func NewAsyncLogger(size int, handle func(b []byte)) *AsyncLogger

NewAsyncLogger 创建一个异步写入器。

size 为队列长度;当队列已满时,新日志会被丢弃(不阻塞调用方)。

func (*AsyncLogger) Close added in v1.0.4

func (l *AsyncLogger) Close()

func (*AsyncLogger) Logger added in v1.0.4

func (l *AsyncLogger) Logger(b []byte)

func (*AsyncLogger) Sync added in v1.0.4

func (l *AsyncLogger) Sync() error

func (*AsyncLogger) Write added in v1.0.4

func (l *AsyncLogger) Write(p []byte) (n int, err error)

Write 实现 io.Writer。

这里会复制入参切片,避免上层复用/修改同一底层数组导致数据竞争或内容错乱。

type Conf added in v1.0.3

type Conf struct {
	Console bool `json:"console"`
	Remote  bool `json:"remote"`
	// contains filtered or unexported fields
}

Conf 是 logger 的配置项 - Console:是否启用控制台输出 - Remote:是否启用远端输出(需要同时提供 handle 才会生效)

func (*Conf) WithHandle added in v1.0.3

func (c *Conf) WithHandle(handle func(b []byte))

WithHandle 设置远端输出回调

type Core added in v1.0.3

type Core struct {
	*zap.Logger
}

func NewLogger added in v1.0.3

func NewLogger(logger *zap.Logger) *Core

func (*Core) WithError added in v1.0.3

func (l *Core) WithError(ctx context.Context, msg string, fields ...zap.Field)

func (*Core) WithInfo added in v1.0.3

func (l *Core) WithInfo(ctx context.Context, msg string, fields ...zap.Field)

func (*Core) WithWarn added in v1.0.3

func (l *Core) WithWarn(ctx context.Context, msg string, fields ...zap.Field)

type LogLevel

type LogLevel uint32

LogLevel 定义日志级别枚举。

const (
	Info  LogLevel = 1 // Info 普通级别。
	Warn  LogLevel = 2 // Warn 警告级别。
	Error LogLevel = 3 // Error 错误级别。
)

type OperationLogger added in v0.9.1

type OperationLogger struct {
	Database  string `json:"database"`
	Statement string `json:"statement"`
	Result    string `json:"result"`
	Path      string `json:"path"`

	Duration uint64 `json:"duration"`

	Level uint32 `json:"level"`
	Type  uint32 `json:"type"`

	TraceId  string `json:"trace_id"`
	ParentId string `json:"parent_id"`

	TargetAppId string `json:"target_app_id"`
	InvokeAppId string `json:"invoke_app_id"`

	UserId   string `json:"user_id"`
	AppId    string `json:"app_id"`
	TenantId string `json:"tenant_id"`
}

OperationLogger 表示操作日志。

type ServerLogger added in v0.9.1

type ServerLogger struct {
	Path    string `json:"path"`
	Level   uint32 `json:"level"`
	Content string `json:"content"`

	TraceId  string `json:"trace_id"`
	ParentId string `json:"parent_id"`

	UserId   string `json:"user_id"`
	AppId    string `json:"app_id"`
	TenantId string `json:"tenant_id"`
}

ServerLogger 表示服务端日志。

Jump to

Keyboard shortcuts

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