logging

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package logging 统一项目内 operation diagnostics 的结构化输出,不配置全局 logger。

Index

Constants

View Source
const (
	// OperationLogMessage 与字段名组成跨 CLI、MCP、SDK 与下载器共享的安全诊断协议。
	// 字段值不得含 URL、header、token、请求输入或响应 body。
	OperationLogMessage      = "pixiv operation"
	RateLimitRetryLogMessage = "pixiv app api rate limit retry"
	LogFieldComponent        = "component"
	LogFieldOperation        = "operation"
	LogFieldSource           = "source"
	LogFieldBackend          = "backend"
	LogFieldDuration         = "duration"
	LogFieldResult           = "result"
	LogFieldErrorCode        = "error_code"
	LogFieldStatus           = "status"
	LogFieldTransportKind    = "transport_kind"
	LogFieldIllustID         = "illust_id"
	LogFieldUserID           = "user_id"
	LogFieldRetryAfter       = "retry_after"
	LogFieldAttempt          = "attempt"

	ResultSuccess        = "success"
	ResultError          = "error"
	ResultRateLimitRetry = "rate_limit_retry"
	BackendLocal         = "local"
)
View Source
const DefaultTextTemplate = "{time}  {level} {pid} --- [{component}] {source} : {message}{details}\n"

DefaultTextTemplate 是本地文本日志的唯一行模板。变量名与模板分离,便于未来 调整列顺序、宽度或新增安全字段,而不让格式逻辑散落在 handler 中。

Variables

This section is empty.

Functions

func LogOperation

func LogOperation(logger *slog.Logger, event OperationEvent)

LogOperation 仅输出固定的安全字段。error result 使用 error level,其余结果为 info;错误正文由调用链返回给调用方,不复制进 operation diagnostics。

func LogRateLimitRetry

func LogRateLimitRetry(logger *slog.Logger, retryAfter time.Duration, attempt int)

LogRateLimitRetry 记录已解析的等待时长和唯一的重试序号;不记录 Retry-After 原文、 请求 URL、鉴权 header 或响应 body。

func OrDiscard

func OrDiscard(logger *slog.Logger) *slog.Logger

OrDiscard 将可选 logger 规范为明确的 discard logger;SDK 与服务边界绝不回退到 slog.Default,避免库调用污染宿主程序的日志协议。

Types

type OperationEvent

type OperationEvent struct {
	Component string
	Operation string
	Backend   string
	Duration  time.Duration
	Result    string
	ErrorCode string
	Status    int
	// TransportKind 只接受公开枚举;不能把网络错误文本放入日志。
	TransportKind string
	IllustID      int64
	UserID        int64
}

OperationEvent 是可跨边界关联的最小诊断记录。它只接受稳定元数据,避免调用方把 URL、header、refresh token 或上游响应体放入日志。

type TextHandler added in v0.7.1

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

TextHandler 把安全的结构化事件渲染为适合直接阅读的单行日志。它只服务本地 文件日志;调用方自行注入的 JSON handler 仍可取得完整、稳定的事件字段。

行首采用 Spring Boot/SLF4J 常见的列式布局:时间、级别、PID、业务组件与调用点。 source 是 LogOperation/LogRateLimitRetry 的受控调用点,operation 留在消息中。 不使用 slog 的 AddSource:它会指向该公共封装;本包自行跳过封装栈帧,记录真正 发起操作的仓库相对文件与行号。

func NewTextHandler added in v0.7.1

func NewTextHandler(writer io.Writer, options *slog.HandlerOptions) *TextHandler

NewTextHandler 返回项目本地日志使用的紧凑文本 handler。options 目前只采用 Level;日志字段由本包的安全事件协议控制,不允许 ReplaceAttr 改写或放行字段。

func (*TextHandler) Enabled added in v0.7.1

func (h *TextHandler) Enabled(_ context.Context, level slog.Level) bool

func (*TextHandler) Handle added in v0.7.1

func (h *TextHandler) Handle(_ context.Context, record slog.Record) error

func (*TextHandler) WithAttrs added in v0.7.1

func (h *TextHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*TextHandler) WithGroup added in v0.7.1

func (h *TextHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

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