Documentation
¶
Overview ¶
Package plog provides pretty-printing for JSON lines logs with colorized output.
Index ¶
- Constants
- func DefaultLevelMap() map[string]int
- func DefaultLogger(opts ...Option) *slog.Logger
- func NewConsoleLogger(opts ...Option) *slog.Logger
- func NewLogger(w io.Writer, opts ...Option) *slog.Logger
- func PrettyPrint(r io.Reader, opts ...Option) error
- func SetDefault(opts ...Option)
- type Attr
- type Config
- type Formatter
- type Handler
- type OmitMatcher
- type Option
- func WithAddSource(addSource bool) Option
- func WithHideTimestamp(hide bool) Option
- func WithLevel(level int) Option
- func WithLevelKeys(keys ...string) Option
- func WithLevelMap(m map[string]int) Option
- func WithMessageKeys(keys ...string) Option
- func WithNoColor(noColor bool) Option
- func WithOmitKeys(patterns ...string) Option
- func WithOutput(w io.Writer) Option
- func WithSourceKeys(keys ...string) Option
- func WithTheme(theme Theme) Option
- func WithTimestampKeys(keys ...string) Option
- func WithWorkingDir(dir string) Option
- type Record
- type Source
- type Theme
Constants ¶
const ( LevelTrace = iota LevelDebug LevelInfo LevelWarn LevelError LevelFatal )
Level constants for log severity.
Variables ¶
This section is empty.
Functions ¶
func DefaultLevelMap ¶
DefaultLevelMap returns the default mapping from level strings to level values.
func DefaultLogger ¶
DefaultLogger creates a new slog.Logger with the pretty handler.
func NewConsoleLogger ¶
NewConsoleLogger creates a logger that writes to stdout.
func PrettyPrint ¶
PrettyPrint reads JSON lines from r and writes formatted output using the given options.
func SetDefault ¶
func SetDefault(opts ...Option)
SetDefault sets the default slog logger to use the pretty handler.
Types ¶
type Config ¶
type Config struct {
LevelKeys []string
LevelMap map[string]int
MessageKeys []string
TimestampKeys []string
SourceKeys []string
OmitKeys []string
WorkingDir string
Output io.Writer
NoColor bool
HideTimestamp bool
AddSource bool
Level int
Theme Theme
// contains filtered or unexported fields
}
Config holds the configuration for pretty-printing logs.
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a Config with sensible defaults.
func (*Config) ShouldOmit ¶
ShouldOmit returns true if the field name should be omitted from output.
type Formatter ¶
type Formatter struct {
// contains filtered or unexported fields
}
Formatter formats log records for output.
func NewFormatter ¶
NewFormatter creates a new Formatter with the given config.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler implements slog.Handler for pretty-printed output.
func NewHandler ¶
NewHandler creates a new slog Handler with the given options.
type OmitMatcher ¶
type OmitMatcher struct {
// contains filtered or unexported fields
}
OmitMatcher matches field names against glob patterns with caching.
func NewOmitMatcher ¶
func NewOmitMatcher(patterns []string) *OmitMatcher
NewOmitMatcher creates a new OmitMatcher with the given patterns.
func (*OmitMatcher) Match ¶
func (m *OmitMatcher) Match(field string) bool
Match returns true if the field name matches any of the omit patterns.
type Option ¶
type Option func(*Config)
Option is a functional option for configuring the pretty printer.
func WithAddSource ¶
WithAddSource enables adding source location to log output.
func WithHideTimestamp ¶
WithHideTimestamp disables timestamp rendering.
func WithLevel ¶
WithLevel sets the minimum level to display. Logs below this level will be suppressed.
func WithLevelKeys ¶
WithLevelKeys sets the keys to look for the log level.
func WithLevelMap ¶
WithLevelMap sets the mapping from level strings to level values.
func WithMessageKeys ¶
WithMessageKeys sets the keys to look for the log message.
func WithOmitKeys ¶
WithOmitKeys sets field name patterns to omit from output. Supports glob patterns (e.g., "request.*", "*_id", "secret*").
func WithSourceKeys ¶
WithSourceKeys sets the keys to look for the source location.
func WithTheme ¶
WithTheme sets the color theme. Any zero-value fields in the provided theme are filled from the default theme.
func WithTimestampKeys ¶
WithTimestampKeys sets the keys to look for the timestamp.
func WithWorkingDir ¶
WithWorkingDir sets the working directory for relative source paths.
type Record ¶
type Record struct {
Level int
LevelRaw string
Message string
Time time.Time
Source *Source
Attrs []Attr
}
Record represents a parsed log record.
type Source ¶
Source represents a source code location.
func (*Source) RelativePath ¶
RelativePath returns the source file path relative to the working directory. For paths in the Go module cache, it formats them as "module@version:path/in/module".
type Theme ¶
type Theme struct {
Timestamp lipgloss.Style
LevelTrace lipgloss.Style
LevelDebug lipgloss.Style
LevelInfo lipgloss.Style
LevelWarn lipgloss.Style
LevelError lipgloss.Style
LevelFatal lipgloss.Style
Message lipgloss.Style
Source lipgloss.Style
AttrKey lipgloss.Style
AttrValue lipgloss.Style
AttrBracket lipgloss.Style
}
Theme defines the color styles for log output.