logging

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package logging 提供 CLI、MCP、SDK 共用的 slog 层:ctx 中的调用信息、属性键常量和 可配置的 text/JSON handler。

WithCall 由执行边界写入 operation 与 call_id,之后同一 ctx 上的日志都会附带这些 字段;WithOperation 只登记 SDK 操作名。

包的组成:New 创建带调用上下文与脱敏行为的 handler,From 从 ctx 取 logger, AttrMethod 等常量是各方共用的字段名,WithCall 与 WithOperation 负责绑定。

origin.go 保存 SDK 错误的创建位置,供执行边界读取而无需扩展 SDK 公开方法集。

transport.go 从标准库 typed cause 提取安全的传输诊断,绝不读取错误文本或 URL。

Index

Constants

View Source
const (
	// FormatText 输出带来源位置与人读错误链的文本日志。
	FormatText = "text"
	// FormatJSON 输出单行 JSON 日志对象。
	FormatJSON = "json"
)
View Source
const (
	AttrOrigin       = "origin"
	AttrMethod       = "method"
	AttrHost         = "host"
	AttrPath         = "path"
	AttrStatus       = "status"
	AttrDuration     = "duration"
	AttrAttempt      = "attempt"
	AttrWait         = "wait"
	AttrSDKOperation = "sdk_operation"
	AttrError        = "error"
	AttrCount        = "count"
	AttrStage        = "stage"
	AttrOperation    = "operation"
	AttrCallID       = "call_id"
	AttrReason       = "reason"
)

AttrMethod 等常量是日志属性键,SDK 传输层、执行边界和 JSON 输出共用这些字段名。

View Source
const (
	AttrNetworkOperation = "op"
	AttrNetwork          = "net"
	AttrAddress          = "address"
	AttrErrno            = "errno"
)

AttrNetworkOperation 等键标记 typed 传输原因的安全字段。

Variables

This section is empty.

Functions

func Discard added in v1.3.0

func Discard() *slog.Logger

Discard 返回不输出任何记录的 logger。

func From added in v1.3.0

func From(ctx context.Context) *slog.Logger

From 返回 ctx 中的 logger;没有时返回丢弃所有输出的 logger。

func HasLogger added in v1.3.0

func HasLogger(ctx context.Context) bool

HasLogger 报告 ctx 是否携带执行边界放入的 logger。

func New added in v1.3.0

func New(w io.Writer, level, format string) (*slog.Logger, error)

New 按配置创建 logger;level 接受 debug、info、warn、error。

func OperationAttr added in v1.3.0

func OperationAttr(ctx context.Context) slog.Attr

OperationAttr 返回 SDK 操作名属性;没有登记时为空字符串。

func OriginAttr added in v1.3.0

func OriginAttr(err error) []slog.Attr

OriginAttr 从错误链取得 SDK 的私有位置;不呈现任何底层错误文本。

func TransportAttrs added in v1.3.0

func TransportAttrs(err error) []slog.Attr

TransportAttrs 提取操作、网络、远端地址与已知 errno 名。 地址只接受标准库 IP 地址类型;未知 errno 不使用 Error 文本代替名称。

func WithCall added in v1.3.0

func WithCall(ctx context.Context, logger *slog.Logger, operation, callID string) context.Context

WithCall 把调用信息和 logger 放进 ctx;同一次调用的所有日志都会附带这些信息。

func WithOperation added in v1.3.0

func WithOperation(ctx context.Context, operation string) context.Context

WithOperation 把 SDK 操作名放进 ctx;只作为日志属性,不影响请求。

Types

type ErrorOrigin added in v1.3.0

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

ErrorOrigin 以未导出方法跨包传递位置;SDK 通过私有别名嵌入,JSON 无可序列化字段。

func CaptureErrorOrigin added in v1.3.0

func CaptureErrorOrigin() ErrorOrigin

CaptureErrorOrigin 跳过 SDK 构造函数及产品 errors.go 中的分类包装。

type State added in v1.3.0

type State struct {
	Logger       *slog.Logger
	Ctx          context.Context
	FailureLevel slog.Level
}

State 保存一次执行的 logger、调用上下文与失败记录的级别。

func Start added in v1.3.0

func Start(ctx context.Context, out io.Writer, commandPath, level, format string) (State, error)

Start 为一次命令执行创建 stderr logger 与带调用 ID 的上下文;JSON 格式把 失败记录提升到 ERROR,文本格式使用 DEBUG。

Jump to

Keyboard shortcuts

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