Documentation
¶
Overview ¶
Package logutil provides log-related utilities used by multiple log processing tasks, such as parsers for different log string formats and log string converters.
Index ¶
- Constants
- Variables
- func ConvertSpecialSequences(original string, converter ...SpecialSequenceConverter) string
- type ANSIEscapeSequenceStripper
- type ColumnBoundary
- type FallbackRawTextLogParser
- type JsonlTextParser
- type KLogTextParser
- type LogfmtTextParser
- type MultiTextLogParser
- type ParseStructuredLogResult
- type RegexSequenceConverter
- type SequenceConverter
- type SpecialSequenceConverter
- type StructuredLogParser
- type TabulateLineType
- type TabulateParseResult
- type TabulateReader
- type UnicodeUnquoteConverter
Constants ¶
const KLogHeaderDateFieldKey = "@date"
Special header field keys stored in fields.
const KLogHeaderSourceLocationFieldKey = "@source"
const KLogHeaderThreadIDFieldKey = "@threadid"
const KLogHeaderTimeFieldKey = "@time"
const MainMessageStructuredFieldKey = "@msg"
MainMessageStructuredFieldKey is the key used to store the main log message in a structured log result.
const OriginalMessageFieldKey = "@original"
const SeverityStructuredFieldKey = "@severity"
SeverityStructuredFieldKey is the key used to store the log severity in a structured log result.
Variables ¶
var ANSIBeginSequences []string = []string{
"\\x1b[",
"\\033[",
"\\u001B[",
}
ANSIBeginSequences is a list of prefixes of ANSI escape sequences.
Functions ¶
func ConvertSpecialSequences ¶
func ConvertSpecialSequences(original string, converter ...SpecialSequenceConverter) string
ConvertSpecialSequences returns the stripped string with applying provided strippers to the original string.
Types ¶
type ANSIEscapeSequenceStripper ¶
type ANSIEscapeSequenceStripper struct {
}
ANSIEscapeSequenceStripper removes ANSI escape sequences.
func (*ANSIEscapeSequenceStripper) Convert ¶
func (a *ANSIEscapeSequenceStripper) Convert(s string) string
Convert implements SpecialSequenceStripper.
type ColumnBoundary ¶ added in v0.52.8
ColumnBoundary defines the character indices bounding a single column.
type FallbackRawTextLogParser ¶
type FallbackRawTextLogParser struct{}
FallbackRawTextLogParser uses the given message directly as the result of main message of parsing structured message.
func (*FallbackRawTextLogParser) TryParse ¶
func (f *FallbackRawTextLogParser) TryParse(message string) *ParseStructuredLogResult
TryParse implements StructuredLogParser.
type JsonlTextParser ¶ added in v0.52.8
type JsonlTextParser struct{}
JsonlTextParser parses given JSONL formatted string.
func NewJsonlTextParser ¶ added in v0.52.8
func NewJsonlTextParser() *JsonlTextParser
NewJsonlTextParser creates a new JsonlTextParser.
func (*JsonlTextParser) TryParse ¶ added in v0.52.8
func (j *JsonlTextParser) TryParse(originalMessage string) *ParseStructuredLogResult
TryParse implements StructuredLogParser.
type KLogTextParser ¶
type KLogTextParser struct {
// contains filtered or unexported fields
}
KLogTextParser parses given klog formatted string. Example klog: I0929 08:20:24.205299 1949 kubelet_getters.go:219] "Pod status updated" pod="kube-system/kube-proxy-gke-p0-gke-basic-1-default-6400229f-0hgr" status="Running"
func NewKLogTextParser ¶
func NewKLogTextParser(hasHeader bool) *KLogTextParser
func (*KLogTextParser) TryParse ¶
func (k *KLogTextParser) TryParse(message string) *ParseStructuredLogResult
TryParse implements StructuredLogParser.
type LogfmtTextParser ¶
type LogfmtTextParser struct {
// contains filtered or unexported fields
}
LogfmtTextParser parses given logfmt formatted string. Reference: https://github.com/hynek/structlog/issues/511#issuecomment-1916426273
func NewLogfmtTextParser ¶
func NewLogfmtTextParser() *LogfmtTextParser
func (*LogfmtTextParser) TryParse ¶
func (l *LogfmtTextParser) TryParse(message string) *ParseStructuredLogResult
TryParse implements StructuredLogParser.
type MultiTextLogParser ¶
type MultiTextLogParser struct {
// contains filtered or unexported fields
}
func NewMultiTextLogParser ¶
func NewMultiTextLogParser(parsers ...StructuredLogParser) *MultiTextLogParser
func (*MultiTextLogParser) TryParse ¶
func (m *MultiTextLogParser) TryParse(message string) *ParseStructuredLogResult
TryParse implements StructuredLogParser.
type ParseStructuredLogResult ¶
ParseStructuredLogResult represents the result of parsing a structured log message.
func (*ParseStructuredLogResult) MainMessage ¶
func (p *ParseStructuredLogResult) MainMessage() (string, error)
MainMessage returns the main message of the structured log, or an error if not found or not a string.
func (*ParseStructuredLogResult) Raw ¶
func (p *ParseStructuredLogResult) Raw() string
func (*ParseStructuredLogResult) Severity ¶
func (p *ParseStructuredLogResult) Severity() (enum.Severity, error)
Severity returns the severity of the structured log, or an error if not found or not of type enum.Severity.
func (*ParseStructuredLogResult) StringField ¶
func (p *ParseStructuredLogResult) StringField(field string) (string, error)
StringField returns the string value of a specific field, or an error if not found or not a string.
type RegexSequenceConverter ¶
type RegexSequenceConverter struct {
// Regex is the regular expression to match.
Regex *regexp.Regexp
// Repl is the replacement string.
Repl string
}
func MustNewRegexSequenceConverter ¶
func MustNewRegexSequenceConverter(regex string, repl string) *RegexSequenceConverter
func NewRegexSequenceConverter ¶
func NewRegexSequenceConverter(regex string, repl string) (*RegexSequenceConverter, error)
NewRegexSequenceConverter instanciate RegexSequenceConverter from given string regex and replace target.
func (*RegexSequenceConverter) Convert ¶
func (r *RegexSequenceConverter) Convert(s string) string
Convert implements SpecialSequenceConverter.
type SequenceConverter ¶
type SequenceConverter struct {
// To is the replace result of any items in the From field.
To string
// From is the list of string to be replaced with the To field.
From []string
}
SequenceConverter replaces specific sequences to a string.
func (*SequenceConverter) Convert ¶
func (c *SequenceConverter) Convert(s string) string
type SpecialSequenceConverter ¶
type SpecialSequenceConverter interface {
// Convert receives the original string and returns the converted string.
Convert(s string) string
}
SpecialSequenceConverter converts specific sequences from string to the other or removes them. (e.g ASCII escape characters, ANSI color characters)
type StructuredLogParser ¶
type StructuredLogParser interface {
// TryParse attempts to parse the given message into *ParseStructuredLogResult.
// It returns nil when the given message is not in the format.
TryParse(message string) *ParseStructuredLogResult
}
StructuredLogParser is the interface to parse string represented structured logs like klog.
type TabulateLineType ¶ added in v0.52.8
type TabulateLineType int
TabulateLineType indicates the classified type of a line in a tabulate format.
const ( // TabulateLineTypeUnknown represents an unrecognized or out-of-table line. TabulateLineTypeUnknown TabulateLineType = iota // TabulateLineTypeHeaderCandidate represents a line that looks like a table header. TabulateLineTypeHeaderCandidate // TabulateLineTypeSeparator represents a separator line (e.g., "--- ---"). TabulateLineTypeSeparator // TabulateLineTypeBody represents a data row within a table. TabulateLineTypeBody )
type TabulateParseResult ¶ added in v0.52.8
type TabulateParseResult struct {
// Type classification of the processed line.
Type TabulateLineType
// Columns is populated for HeaderCandidate and Body types, representing the column names.
Columns []string
// Values contains the parsed key-value pairs for a Body line.
Values map[string]string
}
TabulateParseResult holds the parsing result of a single line.
type TabulateReader ¶ added in v0.52.8
type TabulateReader struct {
ColumnBoundaries []ColumnBoundary
Headers []string
}
TabulateReader is a stateful reader for space-padded tabulate tables. It tracks headers and vertical column boundaries dynamically.
func NewTabulateReader ¶ added in v0.52.8
func NewTabulateReader() *TabulateReader
NewTabulateReader creates a new TabulateReader instance.
func (*TabulateReader) ParseLine ¶ added in v0.52.8
func (r *TabulateReader) ParseLine(line string) (*TabulateParseResult, error)
ParseLine processes a single line, returning the parsed TabulateParseResult. It sequentially checks if the line is a separator, a body row (if inside a table), or a header candidate. Returns an error if a format violation is found (which resets internal state).
func (*TabulateReader) Reset ¶ added in v0.52.8
func (r *TabulateReader) Reset()
Reset clears the internal state when a table ends or a format error occurs.
type UnicodeUnquoteConverter ¶
type UnicodeUnquoteConverter struct{}
UnicodeUnquoteConverter replaces escaped unicode characters like `\\xe2\\x80\\xa6` into the corresponded unicode string.
func (*UnicodeUnquoteConverter) Convert ¶
func (u *UnicodeUnquoteConverter) Convert(s string) string
Convert implements SpecialSequenceConverter.