layout

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultSpringBootPattern = "%d{yyyy-MM-dd HH:mm:ss.SSS}  %5level %pid - " +
	"[%15.15thread] %-40.40logger{1.2*} : %msg%attrs%n"

DefaultSpringBootPattern 是默认控制台输出格式,风格对齐 Spring Boot。

Variables

This section is empty.

Functions

func AppendJSONEvent

func AppendJSONEvent(
	buf *bytes.Buffer,
	when time.Time,
	level slog.Level,
	logger string,
	message string,
	attrs []slog.Attr,
)

AppendJSONEvent 用零额外状态编码 JSON 单行事件。

func AppendJSONFixedEvent

func AppendJSONFixedEvent(
	buf *bytes.Buffer,
	when time.Time,
	level slog.Level,
	logger string,
	message string,
	attrs [3]slog.Attr,
	count int,
)

AppendJSONFixedEvent 编码最多三个属性的固定数组事件,避免热路径切片分配。

func RequiresSynchronizedFormatting

func RequiresSynchronizedFormatting(layout Layout) bool

RequiresSynchronizedFormatting 判断布局格式化是否必须与写入在同一临界区内完成。

func WriteFooter

func WriteFooter(writer io.Writer, layout Layout) (int, error)

WriteFooter 在支持生命周期的布局上写入流页脚。

func WriteHeader

func WriteHeader(writer io.Writer, layout Layout) (int, error)

WriteHeader 在支持生命周期的布局上写入流页眉。

Types

type CSVLayout

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

CSVLayout 输出单行 CSV,字段顺序固定。

func NewCSVLayout

func NewCSVLayout(options LayoutOptions) CSVLayout

NewCSVLayout 创建可配置 CSV 布局。

func (CSVLayout) AppendFooter

func (l CSVLayout) AppendFooter(buf *bytes.Buffer) error

func (CSVLayout) AppendHeader

func (l CSVLayout) AppendHeader(buf *bytes.Buffer) error

func (CSVLayout) Format

func (l CSVLayout) Format(buf *bytes.Buffer, event Event) error

Format 把事件编码为 CSV。

type Event

type Event = logevent.Event

Event 是布局看到的事件快照。

type GELFLayout

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

GELFLayout 输出 Graylog Extended Log Format 单行 JSON。

func NewGELFLayout

func NewGELFLayout(options LayoutOptions) GELFLayout

NewGELFLayout 创建可配置 GELF 布局。

func (GELFLayout) AppendFooter

func (l GELFLayout) AppendFooter(buf *bytes.Buffer) error

func (GELFLayout) AppendHeader

func (l GELFLayout) AppendHeader(buf *bytes.Buffer) error

func (GELFLayout) Format

func (l GELFLayout) Format(buf *bytes.Buffer, event Event) error

Format 把事件编码为 GELF JSON。

type HTMLLayout

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

HTMLLayout 输出 HTML 表格行,适合文件或控制台片段组合。

func NewHTMLLayout

func NewHTMLLayout(options LayoutOptions) HTMLLayout

NewHTMLLayout 创建可配置 HTML 布局。

func (HTMLLayout) AppendFooter

func (l HTMLLayout) AppendFooter(buf *bytes.Buffer) error

func (HTMLLayout) AppendHeader

func (l HTMLLayout) AppendHeader(buf *bytes.Buffer) error

func (HTMLLayout) Format

func (l HTMLLayout) Format(buf *bytes.Buffer, event Event) error

Format 把事件编码为 HTML 表格行。

type JSONLayout

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

JSONLayout 输出 JSON 事件。

func NewJSONLayout

func NewJSONLayout(options LayoutOptions) JSONLayout

NewJSONLayout 创建可配置 JSON 布局。

func (JSONLayout) AppendFooter

func (l JSONLayout) AppendFooter(buf *bytes.Buffer) error

func (JSONLayout) AppendHeader

func (l JSONLayout) AppendHeader(buf *bytes.Buffer) error

func (JSONLayout) CloneLayout

func (l JSONLayout) CloneLayout() Layout

CloneLayout 为每个 appender 隔离 Complete 模式的事件计数。

func (JSONLayout) Format

func (l JSONLayout) Format(buf *bytes.Buffer, event Event) error

func (JSONLayout) Options

func (l JSONLayout) Options() LayoutOptions

Options 返回布局输出参数快照。

func (JSONLayout) RequiresSynchronizedFormatting

func (l JSONLayout) RequiresSynchronizedFormatting() bool

RequiresSynchronizedFormatting 说明 Complete JSON 流需要按实际写入顺序生成分隔符。

type JSONTemplateLayout

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

JSONTemplateLayout 按 JSON 事件模板输出日志事件。

func NewJSONTemplateLayout

func NewJSONTemplateLayout(
	template string,
	options ...JSONTemplateLayoutOption,
) (*JSONTemplateLayout, error)

func NewJSONTemplateLayoutFromFile

func NewJSONTemplateLayoutFromFile(
	path string,
	options ...JSONTemplateLayoutOption,
) (*JSONTemplateLayout, error)

NewJSONTemplateLayoutFromFile 从本地文件编译 JSON 事件模板。

func (*JSONTemplateLayout) AppendFooter

func (l *JSONTemplateLayout) AppendFooter(buf *bytes.Buffer) error

func (*JSONTemplateLayout) AppendHeader

func (l *JSONTemplateLayout) AppendHeader(buf *bytes.Buffer) error

func (*JSONTemplateLayout) CloneLayout

func (l *JSONTemplateLayout) CloneLayout() Layout

CloneLayout 为每个 appender 隔离 Complete 模式的事件计数。

func (*JSONTemplateLayout) Format

func (l *JSONTemplateLayout) Format(buf *bytes.Buffer, event Event) error

func (*JSONTemplateLayout) RequiresSynchronizedFormatting

func (l *JSONTemplateLayout) RequiresSynchronizedFormatting() bool

RequiresSynchronizedFormatting 说明 Complete JSON 模板流需要按实际写入顺序生成分隔符。

type JSONTemplateLayoutOption

type JSONTemplateLayoutOption func(*jsonTemplateLayoutOptions)

JSONTemplateLayoutOption 调整 JSONTemplateLayout 编译行为。

func WithJSONTemplateLayoutOptions

func WithJSONTemplateLayoutOptions(layoutOptions LayoutOptions) JSONTemplateLayoutOption

WithJSONTemplateLayoutOptions 设置 JSON Template 布局的通用输出参数。

func WithJSONTemplateResolverLookup

func WithJSONTemplateResolverLookup(lookup JSONTemplateResolverLookup) JSONTemplateLayoutOption

WithJSONTemplateResolverLookup 设置用于解析自定义 resolver 的查找函数。

type JSONTemplateResolver

type JSONTemplateResolver interface {
	AppendJSON(buf *bytes.Buffer, event Event)
}

JSONTemplateResolver 是 JSON Template 字段值编码器。

type JSONTemplateResolverBuildConfig

type JSONTemplateResolverBuildConfig struct {
	Name    string
	Options map[string]sonic.NoCopyRawMessage
}

JSONTemplateResolverBuildConfig 是 JSON Template resolver 插件的构建输入。

type JSONTemplateResolverFactory

type JSONTemplateResolverFactory func(
	config JSONTemplateResolverBuildConfig,
) (JSONTemplateResolver, error)

JSONTemplateResolverFactory 从配置构建 JSON Template resolver。

type JSONTemplateResolverLookup

type JSONTemplateResolverLookup func(kind string) (JSONTemplateResolverFactory, bool)

JSONTemplateResolverLookup 按规范化前的名称查找自定义 resolver 工厂。

type Layout

type Layout interface {
	Format(buf *bytes.Buffer, event Event) error
}

Layout 把日志事件编码为字节。

func CloneLayout

func CloneLayout(layout Layout) Layout

CloneLayout 为 appender 绑定创建隔离布局,避免生命周期状态跨输出流共享。

func NewCharsetLayout added in v0.0.3

func NewCharsetLayout(delegate Layout, name string) (Layout, error)

NewCharsetLayout 使用指定字符集编码布局结果,UTF-8 不增加转换层。

func NewDefaultLayout

func NewDefaultLayout() Layout

NewDefaultLayout 创建默认 Spring Boot 风格布局。

type LayoutOptions

type LayoutOptions struct {
	// Compact 禁用默认事件换行,适合由上层协议自行分隔事件的场景。
	Compact bool
	// EventEOL 在 Compact 模式下仍然为每个事件追加换行。
	EventEOL bool
	// Complete 启用布局页眉和页脚输出,由 appender 在流生命周期内写一次。
	Complete bool
	// IncludeStacktrace 在支持异常结构的布局中输出完整异常结构。
	IncludeStacktrace bool
	// StacktraceAsString 将异常栈输出为字符串,便于兼容文本型采集器。
	StacktraceAsString bool
	// PropertiesAsList 将上下文属性输出为键值列表。
	PropertiesAsList bool
	// IncludeNullDelimiter 在事件结束后追加 NUL 字节,用于 GELF 等协议分隔。
	IncludeNullDelimiter bool
	// DisableANSI 禁用 PatternLayout 中 highlight/style 转换器的 ANSI SGR 输出。
	DisableANSI bool
	// Header 是 Complete 模式下流打开时写入的页眉。
	Header string
	// Footer 是 Complete 模式下流关闭时写入的页脚。
	Footer string
}

LayoutOptions 描述通用结构化布局参数。

type PatternLayout

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

PatternLayout 支持常用日志 pattern 占位符。

func NewPatternLayout

func NewPatternLayout(pattern string) (*PatternLayout, error)

NewPatternLayout 编译 pattern,避免热路径反复解析。

func NewPatternLayoutWithOptions

func NewPatternLayoutWithOptions(pattern string, options LayoutOptions) (*PatternLayout, error)

NewPatternLayoutWithOptions 使用指定布局参数编译 pattern。

func (*PatternLayout) Format

func (l *PatternLayout) Format(buf *bytes.Buffer, event Event) error

func (*PatternLayout) Options

func (l *PatternLayout) Options() LayoutOptions

Options 返回布局输出参数快照。

type RFC5424Layout

type RFC5424Layout struct {
	Facility  int
	AppName   string
	MessageID string
}

RFC5424Layout 输出 RFC 5424 syslog 单行事件。

func (RFC5424Layout) Format

func (l RFC5424Layout) Format(buf *bytes.Buffer, event Event) error

Format 把事件编码为 RFC 5424 syslog。

type StructuredECSOptions added in v0.0.3

type StructuredECSOptions struct {
	ServiceEnvironment string
	ServiceName        string
	ServiceNodeName    string
	ServiceVersion     string
}

StructuredECSOptions 描述 ECS 服务元数据。

type StructuredFormat added in v0.0.3

type StructuredFormat string

StructuredFormat 标识受支持的结构化日志协议。

const (
	StructuredFormatECS      StructuredFormat = "ecs"
	StructuredFormatGELF     StructuredFormat = "gelf"
	StructuredFormatLogstash StructuredFormat = "logstash"
)

type StructuredGELFOptions added in v0.0.3

type StructuredGELFOptions struct {
	Host           string
	ServiceName    string
	ServiceVersion string
}

StructuredGELFOptions 描述 GELF 主机与服务元数据。

type StructuredJSONCustomizer added in v0.0.3

type StructuredJSONCustomizer interface {
	Customize(event Event, fields StructuredJSONFieldAppender)
}

StructuredJSONCustomizer 以显式 Go API 追加结构化成员。

type StructuredJSONCustomizerFunc added in v0.0.3

type StructuredJSONCustomizerFunc func(event Event, fields StructuredJSONFieldAppender)

StructuredJSONCustomizerFunc 把函数适配为 StructuredJSONCustomizer。

func (StructuredJSONCustomizerFunc) Customize added in v0.0.3

type StructuredJSONFieldAppender added in v0.0.3

type StructuredJSONFieldAppender interface {
	Add(key string, value slog.Value)
}

StructuredJSONFieldAppender 接收自定义结构化成员。

type StructuredJSONLayout added in v0.0.3

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

StructuredJSONLayout 输出 Spring Boot 兼容的 ECS、GELF 或 Logstash JSON。

func NewStructuredJSONLayout added in v0.0.3

func NewStructuredJSONLayout(options StructuredJSONOptions) (*StructuredJSONLayout, error)

NewStructuredJSONLayout 编译结构化日志规则。

func (*StructuredJSONLayout) Format added in v0.0.3

func (l *StructuredJSONLayout) Format(buf *bytes.Buffer, event Event) error

type StructuredJSONOptions added in v0.0.3

type StructuredJSONOptions struct {
	Format         StructuredFormat
	Include        []string
	Exclude        []string
	Rename         map[string]string
	Add            map[string]string
	IncludeContext bool
	ContextPrefix  string
	Stacktrace     StructuredStacktraceOptions
	ECS            StructuredECSOptions
	GELF           StructuredGELFOptions
	Customizers    []StructuredJSONCustomizer
}

StructuredJSONOptions 描述结构化 JSON 的编译参数。

type StructuredStacktraceOptions added in v0.0.3

type StructuredStacktraceOptions struct {
	Printer             StructuredStacktracePrinter
	RootFirst           bool
	MaxLength           int
	MaxThrowableDepth   int
	IncludeCommonFrames bool
	IncludeHashes       bool
}

StructuredStacktraceOptions 描述异常链输出规则。

type StructuredStacktracePrinter added in v0.0.3

type StructuredStacktracePrinter string

StructuredStacktracePrinter 标识结构化日志的异常栈打印策略。

const (
	StructuredStacktracePrinterStandard      StructuredStacktracePrinter = "standard"
	StructuredStacktracePrinterLoggingSystem StructuredStacktracePrinter = "logging-system"
)

type SyslogLayout

type SyslogLayout = RFC5424Layout

SyslogLayout 是 RFC5424Layout 的语义别名。

type TextLayout

type TextLayout struct{}

TextLayout 输出稳定的 key=value 文本。

func (TextLayout) Format

func (TextLayout) Format(buf *bytes.Buffer, event Event) error

type Throwable

type Throwable = logevent.Throwable

Throwable 是布局输出使用的异常快照。

type XMLLayout

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

XMLLayout 输出单事件 XML 片段。

func NewXMLLayout

func NewXMLLayout(options LayoutOptions) XMLLayout

NewXMLLayout 创建可配置 XML 布局。

func (XMLLayout) AppendFooter

func (l XMLLayout) AppendFooter(buf *bytes.Buffer) error

func (XMLLayout) AppendHeader

func (l XMLLayout) AppendHeader(buf *bytes.Buffer) error

func (XMLLayout) Format

func (l XMLLayout) Format(buf *bytes.Buffer, event Event) error

Format 把事件编码为 XML。

type YAMLLayout

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

YAMLLayout 输出单事件 YAML 文档。

func NewYAMLLayout

func NewYAMLLayout(options LayoutOptions) YAMLLayout

NewYAMLLayout 创建可配置 YAML 布局。

func (YAMLLayout) AppendFooter

func (l YAMLLayout) AppendFooter(buf *bytes.Buffer) error

func (YAMLLayout) AppendHeader

func (l YAMLLayout) AppendHeader(buf *bytes.Buffer) error

func (YAMLLayout) Format

func (l YAMLLayout) Format(buf *bytes.Buffer, event Event) error

Format 把事件编码为 YAML。

Jump to

Keyboard shortcuts

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