Documentation
¶
Index ¶
- Variables
- func Close() error
- func FromContext(ctx context.Context) *zap.Logger
- func GetWriteSyncer2(filename string) io.Writer
- func Init() error
- func RequestIDFromContext(ctx context.Context) string
- func SetLevel(level string) error
- func Sync() error
- func WithComponent(component string) *zap.SugaredLogger
- func WithRequestID(ctx context.Context, requestID string) context.Context
- type Zap
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // Logger 是脚手架全局结构化 Logger;Init 前使用无输出实现避免 nil panic。 Logger = zap.NewNop() // SugaredLogger 是兼容现有 printf 风格调用的全局 Logger。 SugaredLogger = Logger.Sugar() )
View Source
var CONFIG = Zap{ Level: "debug", Format: "console", Prefix: "[go-blackbox]", Director: ".", LinkName: "latest_log", ShowLine: true, EncodeLevel: "LowercaseColorLevelEncoder", StacktraceKey: "stacktrace", LogInConsole: true, MaxAge: 7 * 24, WithRotationTime: 24, }
CONFIG 保存兼容旧调用方的全局日志配置。 应用应在启动阶段单线程修改配置,并在 Init 成功后停止写入该变量。
Functions ¶
func Close ¶
func Close() error
Close 刷新日志缓冲区并关闭全部轮转日志文件,释放底层文件句柄。 该函数幂等;关闭后再次调用 Init 会创建新的写入器。多个关闭错误会聚合返回。
func FromContext ¶
FromContext 返回附加 request_id 字段的结构化 Logger。 context 中不存在 request_id 时返回全局 Logger,保证日志调用方无需判空。
func GetWriteSyncer2 ¶
GetWriteSyncer2 保留旧版直接获取 io.Writer 的公开入口。 Deprecated: 新代码应由 Init 统一创建日志核心并处理初始化错误。
func Init ¶
func Init() error
Init 根据 CONFIG 创建完整日志目录和严格单级文件 Logger。 初始化失败时保留现有 Logger,不会注册半初始化日志实例。
func RequestIDFromContext ¶
RequestIDFromContext 从 context 读取 request_id;不存在时返回空字符串。
func Sync ¶
func Sync() error
Sync 刷新全局 Logger 的缓冲区。 macOS 和终端标准输出不支持 fsync 时产生的 EINVAL、ENOTTY 会被安全忽略。
func WithComponent ¶
func WithComponent(component string) *zap.SugaredLogger
WithComponent 返回携带 component 字段的 SugaredLogger。 component 为空时使用 unknown,确保日志始终可以按功能模块筛选。
Types ¶
type Zap ¶
type Zap struct {
// Level 是允许输出的最低日志级别。
Level string `mapstructure:"level" json:"level" yaml:"level"`
// Format 支持 console 和 json。
Format string `mapstructure:"format" json:"format" yaml:"format"`
// Prefix 是兼容旧配置的应用标识,初始化后会规范化为结构化 service 字段。
Prefix string `mapstructure:"prefix" json:"prefix" yaml:"prefix"`
// Director 是日志根目录,等级文件写入其 zap 子目录。
Director string `mapstructure:"director" json:"director" yaml:"director"`
// LinkName 为旧版配置字段保留;新版每个等级使用独立软链接名称。
LinkName string `mapstructure:"link-name" json:"link-name" yaml:"link-name"`
// ShowLine 控制是否记录调用文件和行号。
ShowLine bool `mapstructure:"show-line" json:"show-line" yaml:"show-line"`
// EncodeLevel 为旧版编码器名称保留。
EncodeLevel string `mapstructure:"encode-level" json:"encode-level" yaml:"encode-level"`
// StacktraceKey 是堆栈字段名称。
StacktraceKey string `mapstructure:"stacktrace-key" json:"stacktrace-key" yaml:"stacktrace-key"`
// LogInConsole 控制是否同时输出到标准输出。
LogInConsole bool `mapstructure:"log-in-console" json:"log-in-console" yaml:"log-in-console"`
// MaxAge 是轮转日志最大保留小时数。
MaxAge int `mapstructure:"max-age" json:"max-age" yaml:"max-age"`
// WithRotationTime 是日志文件轮转间隔小时数。
WithRotationTime int `mapstructure:"rotation-time" json:"rotation-time" yaml:"rotation-time"`
}
Zap 定义日志编码、输出目录和文件轮转策略。
Click to show internal directories.
Click to hide internal directories.