logging

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 13 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 的 slog 层:ctx 中的调用信息、属性键常量和 可配置的 text/JSON handler。

WithCall 由执行边界写入 operation 与 call_id,之后同一 ctx 上的日志都会附带这些 字段;SDK 使用默认 slog logger,属性键由各 SDK 自持。

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

Index

Constants

View Source
const (
	// FormatText 输出带来源位置与人读错误链的文本日志。
	FormatText = "text"
	// FormatJSON 输出单行 JSON 日志对象。
	FormatJSON = "json"
)
View Source
const (
	AttrDuration  = "duration"
	AttrError     = "error"
	AttrCount     = "count"
	AttrStage     = "stage"
	AttrOperation = "operation"
	AttrCallID    = "call_id"
	AttrReason    = "reason"
)

AttrOperation 等常量是执行边界与 JSON 输出使用的日志属性键。

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 WithCall added in v1.3.0

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

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

Types

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