logger

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: MIT Imports: 7 Imported by: 0

README

Logger

logger 包提供基于 zap 的日志封装,并通过 otelzap 将日志输出到 OpenTelemetry。

功能

  • Console:输出到 stdout(面向人读)
  • Remote:输出到 OpenTelemetry(面向机器采集/检索)

使用

NewZapLogger 需要一个实现了 conf.BootstrapConf 接口的配置对象。

import (
	"context"

	"github.com/fireflycore/go-micro/logger"
	"go.uber.org/zap"
)

// MyConf 实现了 conf.BootstrapConf
type MyConf struct {
	AppName string
	Logger  *logger.Conf
}

func (c *MyConf) GetAppName() string { return c.AppName }
func (c *MyConf) GetLoggerConsole() bool { return c.Logger.GetLoggerConsole() }
func (c *MyConf) GetLoggerRemote() bool { return c.Logger.GetLoggerRemote() }
// ... 实现其他接口方法 ...

func main() {
	myConf := &MyConf{
		AppName: "your-service",
		Logger:  &logger.Conf{Console: true, Remote: true},
	}
	
	// 注意:实际项目中还需要实现 conf.BootstrapConf 的其他方法
	zl := logger.NewZapLogger(myConf)
	log := logger.NewAccessLogger(zl)

	log.WithContextInfo(context.Background(), "hello", zap.String("k", "v"))
}
Trace 关联

当启用 Remote 输出(otelzap.NewCore(...))且服务已初始化 OpenTelemetry Logs Provider 后:

  • 在日志 fields 中包含 zap.Any("ctx", ctx)otelzap 会从 ctx 提取 span context 并关联到 OTLP log record。
  • 本库的 WithContextInfo/WithContextWarn/WithContextError 已内置该字段注入。

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 NewZapLogger added in v1.0.3

func NewZapLogger(bootstrapConf conf.BootstrapConf) *zap.Logger

Types

type AccessLogger added in v0.8.3

type AccessLogger struct {
	*zap.Logger
}

func NewAccessLogger added in v1.1.8

func NewAccessLogger(logger *zap.Logger) *AccessLogger

func (*AccessLogger) WithContextError added in v1.1.8

func (l *AccessLogger) WithContextError(ctx context.Context, msg string, fields ...zap.Field)

func (*AccessLogger) WithContextInfo added in v1.1.8

func (l *AccessLogger) WithContextInfo(ctx context.Context, msg string, fields ...zap.Field)

func (*AccessLogger) WithContextWarn added in v1.1.8

func (l *AccessLogger) WithContextWarn(ctx context.Context, msg string, fields ...zap.Field)

type Conf added in v1.0.3

type Conf struct {
	Console bool `json:"console"`
	Remote  bool `json:"remote"`
}

Conf 是 logger 的配置项 - Console:是否启用控制台输出 - Remote:是否启用远端输出(OTel)

func (*Conf) GetLoggerConsole added in v1.1.8

func (c *Conf) GetLoggerConsole() bool

func (*Conf) GetLoggerRemote added in v1.1.8

func (c *Conf) GetLoggerRemote() bool

type ServerLogger added in v0.9.1

type ServerLogger struct {
	*zap.Logger
}

func NewServerLogger added in v1.1.8

func NewServerLogger(logger *zap.Logger) *ServerLogger

func (*ServerLogger) WithContextError added in v1.1.8

func (l *ServerLogger) WithContextError(ctx context.Context, msg string, fields ...zap.Field)

func (*ServerLogger) WithContextInfo added in v1.1.8

func (l *ServerLogger) WithContextInfo(ctx context.Context, msg string, fields ...zap.Field)

func (*ServerLogger) WithContextWarn added in v1.1.8

func (l *ServerLogger) WithContextWarn(ctx context.Context, msg string, fields ...zap.Field)

Jump to

Keyboard shortcuts

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