parse

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2021 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultVariables = []string{
	"$TS",
	"$RAW", "$MSG",
	"$LVL", "$UNKNOWN", "$TRACE", "$DEBUG", "$INFO", "$WARN", "$ERROR", "$PANIC", "$DPANIC", "$FATAL",
}

Functions

func CompileJQ

func CompileJQ(p string) (*gojq.Code, error)

CompileJQ compiles the provided jq program.

func DefaultTimeParser

func DefaultTimeParser(in interface{}) (time.Time, error)

DefaultTimeParser treats numbers as seconds since the Unix epoch and strings as RFC3339 timestamps.

func StrictUnixTimeParser

func StrictUnixTimeParser(in interface{}) (time.Time, error)

StrictUnixTimeParser always treats the incoming data as a float64 number of seconds since the Unix epoch.

Types

type DefaultOutputFormatter

type DefaultOutputFormatter struct {
	Aurora aurora.Aurora // Controls the use of color.

	// If true, print ↑ for fields that have an identical value as the previous line.
	ElideDuplicateFields bool

	// The time.Format string to show times in, like time.RFC3339.  If empty, show relative
	// times since the time the program started.  (A minus sign indicates the past; positive
	// values are in the future, good for when you are following a log file.)
	AbsoluteTimeFormat string

	// If non-empty, print only the fractional seconds for log lines that occurred on the same
	// second as the previous line.  For example, if SecondsOnlyFormat is set to ".000":
	//
	// INFO 2020-01-02T03:04:05.123Z first event
	// INFO                    .456  next event
	// INFO                    .789  another event
	// INFO 2020-01-02T03:04:05.000Z a brand new second
	//
	// Decimals are only aligned by careful selection of AbsoluteTimeFormat and
	// SecondsOnlyFormat strings.  The algorithm does nothing smart.
	SubSecondsOnlyFormat string

	Zone            *time.Location      // Zone is the time zone to display the output in.
	HighlightFields map[string]struct{} // HighlightFields visually distinguishes the named fields.
}

func (*DefaultOutputFormatter) FormatField

func (f *DefaultOutputFormatter) FormatField(s *State, k string, v interface{}, w *bytes.Buffer)

func (*DefaultOutputFormatter) FormatLevel

func (f *DefaultOutputFormatter) FormatLevel(s *State, level Level, w *bytes.Buffer)

func (*DefaultOutputFormatter) FormatMessage

func (f *DefaultOutputFormatter) FormatMessage(s *State, msg string, highlight bool, w *bytes.Buffer)

func (*DefaultOutputFormatter) FormatTime

func (f *DefaultOutputFormatter) FormatTime(s *State, t time.Time, w *bytes.Buffer)

type InputSchema

type InputSchema struct {
	TimeKey     string      // The name of the key that holds the timestamp.
	TimeFormat  TimeParser  // How to turn the value of the time key into a time.Time.
	LevelKey    string      // The name of the key that holds the log level.
	LevelFormat LevelParser // How to turn the value of the level key into a Level.
	MessageKey  string      // The name of the key that holds the main log message.

	// If true, print an error when non-JSON lines appear in the input.  If false, treat them
	// as normal messages with as much information extracted as possible.
	Strict bool

	// DeleteKeys contains a list of keys to delete; used when the log lines contain version
	// information that is used for guessing the schema.
	DeleteKeys []string
}

InputSchema controls the interpretation of incoming log lines.

func (*InputSchema) ReadLine

func (s *InputSchema) ReadLine(l *line) error

ReadLine parses a log line into the provided line object.

type Level

type Level uint8

Level is a log level. This exists so that you can write jq expressions like "select($LVL<$WARN)". Whatever logger you're using probably has totally different levels because nobody can agree on them. Feel free to add them here in the right place.

const (
	LevelUnknown Level = iota
	LevelTrace
	LevelDebug
	LevelInfo
	LevelWarn
	LevelError
	LevelPanic
	LevelDPanic
	LevelFatal
)

func BunyanV0LevelParser added in v0.0.2

func BunyanV0LevelParser(in interface{}) (Level, error)

BunyanV0LLevelParser maps bunyan's float64 levels to log levels.

func DefaultLevelParser

func DefaultLevelParser(in interface{}) (Level, error)

DefaultLevelParser uses common strings to determine the log level. Case does not matter; info is the same log level as INFO.

func LagerLevelParser

func LagerLevelParser(in interface{}) (Level, error)

LagerLevelParser maps lager's float64 levels to log levels.

type LevelParser

type LevelParser func(interface{}) (Level, error)

LevelParser is a function that parses log levels in log messages.

type OutputFormatter

type OutputFormatter interface {
	// FormatTime is a function that formats a time.Time and outputs it to an io.Writer.
	FormatTime(s *State, t time.Time, w *bytes.Buffer)

	// FormatLevel is a function that formats a log level and outputs it to an io.Writer.
	FormatLevel(s *State, lvl Level, w *bytes.Buffer)

	// FormatMessage is a function that formats a log message and outputs it to an io.Writer.
	FormatMessage(s *State, msg string, highlight bool, w *bytes.Buffer)

	// FormatField is a function that formats a (key, value) pair and outputs it to an io.Writer.
	FormatField(s *State, k string, v interface{}, w *bytes.Buffer)
}

OutputFormatter describes an object that actually does the output formatting. Methods take a bytes.Buffer so they can output incrementally as with an io.Writer, but without worrying about write errors or short writes.

type OutputSchema

type OutputSchema struct {
	PriorityFields []string         // PriorityFields controls which fields are printed first.
	Formatter      OutputFormatter  // Actually does the formatting.
	EmitErrorFn    func(msg string) // A function that sees all errors.
	// contains filtered or unexported fields
}

OutputSchema controls how output lines are formatted.

func (*OutputSchema) Emit

func (s *OutputSchema) Emit(l *line, w *bytes.Buffer)

Emit emits a formatted line to the provided buffer. The provided line object may not be used again until reinitialized.

func (*OutputSchema) EmitError

func (s *OutputSchema) EmitError(msg string)

EmitError prints any internal errors, so that log lines are not silently ignored if they are unparseable.

type State

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

State keeps state between log lines.

type Summary

type Summary struct {
	Lines    int
	Errors   int
	Filtered int
}

func ReadLog

func ReadLog(r io.Reader, w io.Writer, ins *InputSchema, outs *OutputSchema, jq *gojq.Code) (Summary, error)

ReadLog reads a stream of JSON-formatted log lines from the provided reader according to the input schema, reformatting it and writing to the provided writer according to the output schema. Parse errors are handled according to the input schema. Any other errors, not including io.EOF on the reader, are returned.

type TimeParser

type TimeParser func(interface{}) (time.Time, error)

TimeParser is a function that parses timestamps in log messages.

Jump to

Keyboard shortcuts

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