Documentation
¶
Index ¶
- func CloseHandler(h slog.Handler) error
- func CompressOldSessions(baseDir string, olderThan time.Duration) ([]string, error)
- func CountLogs(sessionPath string) (int64, error)
- func ExportSession(sessionPath string, w io.Writer) error
- func FlushHandler(h slog.Handler) error
- func ParseFileIndex(filename string) (int, error)
- type Closer
- type FileHandler
- func (h *FileHandler) Close() error
- func (h *FileHandler) Enabled(ctx context.Context, level slog.Level) bool
- func (h *FileHandler) Flush() error
- func (h *FileHandler) Handle(ctx context.Context, r slog.Record) error
- func (h *FileHandler) SessionDir() string
- func (h *FileHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *FileHandler) WithGroup(name string) slog.Handler
- type FileHandlerOpt
- type FileStats
- type FilterHandler
- func (h *FilterHandler) Enabled(ctx context.Context, level slog.Level) bool
- func (h *FilterHandler) GetConfig() (defaultLevel slog.Level, rules map[string]slog.Level, disabled bool, ...)
- func (h *FilterHandler) GetFilterTraceId() string
- func (h *FilterHandler) Handle(ctx context.Context, r slog.Record) error
- func (h *FilterHandler) HttpHandler() http.Handler
- func (h *FilterHandler) RemoveLogLevel(path string)
- func (h *FilterHandler) SetDefaultLevel(level slog.Level)
- func (h *FilterHandler) SetEnabled(enabled bool)
- func (h *FilterHandler) SetExclusiveFiltering(exclusive bool)
- func (h *FilterHandler) SetFilterTraceId(traceId string)
- func (h *FilterHandler) SetLogLevel(path string, level slog.Level)
- func (h *FilterHandler) SetTraceIdKey(key any)
- func (h *FilterHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *FilterHandler) WithGroup(name string) slog.Handler
- type FilterHandlerOpt
- type Flusher
- type HttpHandler
- type LogEntry
- type LogIterator
- type LogPage
- type LogReader
- type LogSession
- type MultiHandler
- func (h *MultiHandler) Close() error
- func (h *MultiHandler) Enabled(ctx context.Context, level slog.Level) bool
- func (h *MultiHandler) Flush() error
- func (h *MultiHandler) Handle(ctx context.Context, r slog.Record) error
- func (h *MultiHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *MultiHandler) WithGroup(name string) slog.Handler
- type SearchCriteria
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloseHandler ¶
CloseHandler closes a handler if it implements Closer.
func CompressOldSessions ¶
CompressOldSessions compresses sessions older than the given duration. Returns the paths of compressed sessions.
func CountLogs ¶
CountLogs counts the total number of log entries in a session. This scans all files and may be slow for large sessions.
func ExportSession ¶
ExportSession exports a session to a single file or writer.
func FlushHandler ¶
FlushHandler flushes a handler if it implements Flusher.
func ParseFileIndex ¶
ParseFileIndex extracts the numeric index from a log filename.
Types ¶
type Closer ¶
type Closer interface {
Close() error
}
Closer interface for handlers that need cleanup.
type FileHandler ¶
type FileHandler struct {
// contains filtered or unexported fields
}
FileHandler is a slog.Handler that writes logs to files with rotation support.
func NewFileHandler ¶
func NewFileHandler(baseDir string, opts ...FileHandlerOpt) (*FileHandler, error)
NewFileHandler creates a new FileHandler that writes logs to files. The directory structure will be: baseDir/<timestamp>/00001.log where timestamp is the current date and time when the handler is created.
func (*FileHandler) Close ¶
func (h *FileHandler) Close() error
Close flushes and closes the current log file.
func (*FileHandler) Flush ¶
func (h *FileHandler) Flush() error
Flush forces a flush of the current buffer to disk.
func (*FileHandler) SessionDir ¶
func (h *FileHandler) SessionDir() string
SessionDir returns the current session directory path.
type FileHandlerOpt ¶
type FileHandlerOpt func(*FileHandler)
FileHandlerOpt is an option for configuring the FileHandler.
func WithMaxBytes ¶
func WithMaxBytes(maxBytes int64) FileHandlerOpt
WithMaxBytes sets the maximum size in bytes before rotating to a new file.
func WithMaxRecords ¶
func WithMaxRecords(maxRecords int64) FileHandlerOpt
WithMaxRecords sets the maximum number of log records before rotating to a new file.
type FileStats ¶
type FileStats struct {
FileName string `json:"file_name"`
FilePath string `json:"file_path"`
Size int64 `json:"size"`
LineCount int64 `json:"line_count"`
ModTime time.Time `json:"mod_time"`
}
FileStats returns statistics about log files in a session.
func GetSessionStats ¶
GetSessionStats returns statistics about all log files in a session.
type FilterHandler ¶
type FilterHandler struct {
// contains filtered or unexported fields
}
func NewFilterHandler ¶
func NewFilterHandler(inner slog.Handler, opts ...FilterHandlerOpt) *FilterHandler
func (*FilterHandler) GetConfig ¶
func (h *FilterHandler) GetConfig() (defaultLevel slog.Level, rules map[string]slog.Level, disabled bool, exclusiveFiltering bool, traceIdKey any, filterTraceId string)
GetConfig returns the current configuration (for debugging/inspection)
func (*FilterHandler) GetFilterTraceId ¶
func (h *FilterHandler) GetFilterTraceId() string
GetFilterTraceId returns the current trace ID filter
func (*FilterHandler) HttpHandler ¶
func (h *FilterHandler) HttpHandler() http.Handler
func (*FilterHandler) RemoveLogLevel ¶
func (h *FilterHandler) RemoveLogLevel(path string)
RemoveLogLevel removes a path-specific log level, falling back to default
func (*FilterHandler) SetDefaultLevel ¶
func (h *FilterHandler) SetDefaultLevel(level slog.Level)
SetDefaultLevel changes the default log level at runtime
func (*FilterHandler) SetEnabled ¶
func (h *FilterHandler) SetEnabled(enabled bool)
SetEnabled enables or disables all logging
func (*FilterHandler) SetExclusiveFiltering ¶
func (h *FilterHandler) SetExclusiveFiltering(exclusive bool)
SetExclusiveFiltering enables or disables exclusive filtering mode at runtime
func (*FilterHandler) SetFilterTraceId ¶
func (h *FilterHandler) SetFilterTraceId(traceId string)
SetFilterTraceId filters logs to only show those with the specified trace ID If set to empty string, disables trace ID filtering
func (*FilterHandler) SetLogLevel ¶
func (h *FilterHandler) SetLogLevel(path string, level slog.Level)
SetLogLevel sets or updates the log level for a specific path at runtime
func (*FilterHandler) SetTraceIdKey ¶
func (h *FilterHandler) SetTraceIdKey(key any)
SetTraceIdKey configures the key used to extract trace ID from context
type FilterHandlerOpt ¶
type FilterHandlerOpt func(*FilterHandler)
func WithDefaultLevel ¶
func WithDefaultLevel(level slog.Level) FilterHandlerOpt
WithDefaultLevel sets the default log level for all packages
func WithExclusiveFiltering ¶
func WithExclusiveFiltering() FilterHandlerOpt
WithExclusiveFiltering enables exclusive filtering mode. When enabled, only logs from paths that have explicit rules (set via WithLogLevel) will be shown. All other logs will be filtered out regardless of their level.
func WithLogLevel ¶
func WithLogLevel(path string, level slog.Level) FilterHandlerOpt
WithLogLevel sets the log level for a specific package path
type Flusher ¶
type Flusher interface {
Flush() error
}
Flusher interface for handlers that need flushing.
type HttpHandler ¶
type HttpHandler struct {
// contains filtered or unexported fields
}
func NewHttpHandler ¶
func NewHttpHandler(log *FilterHandler) *HttpHandler
func (*HttpHandler) ServeHTTP ¶
func (h *HttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type LogEntry ¶
type LogEntry struct {
Time time.Time `json:"time"`
Level string `json:"level"`
Message string `json:"msg"`
Attrs map[string]interface{} `json:"-"` // all other attributes
Raw json.RawMessage `json:"-"` // the raw JSON for custom parsing
}
LogEntry represents a single log entry read from a file.
func SearchSession ¶
func SearchSession(sessionPath string, criteria SearchCriteria, limit int) ([]LogEntry, error)
SearchSession searches a session for log entries matching the criteria.
type LogIterator ¶
type LogIterator struct {
// contains filtered or unexported fields
}
LogIterator provides an iterator interface for reading logs.
func NewLogIterator ¶
func NewLogIterator(sessionPath string) (*LogIterator, error)
NewLogIterator creates a new iterator for reading logs sequentially.
func (*LogIterator) All ¶
func (it *LogIterator) All() func(yield func(LogEntry) bool)
All returns an iterator function compatible with range loops. Usage: for entry := range iterator.All() { ... }
func (*LogIterator) Close ¶
func (it *LogIterator) Close() error
Close closes the iterator and releases resources.
func (*LogIterator) Error ¶
func (it *LogIterator) Error() error
Error returns any error that occurred during iteration.
func (*LogIterator) Next ¶
func (it *LogIterator) Next() (LogEntry, bool)
Next returns the next log entry and a boolean indicating if there are more entries.
type LogPage ¶
type LogPage struct {
Entries []LogEntry `json:"entries"`
TotalCount int `json:"total_count"` // total entries across all files (estimate)
PageSize int `json:"page_size"`
PageNumber int `json:"page_number"`
HasMore bool `json:"has_more"`
FileIndex int `json:"file_index"` // current file being read
LineOffset int `json:"line_offset"` // line offset within the file
}
LogPage represents a page of log entries with pagination info.
type LogReader ¶
type LogReader struct {
// contains filtered or unexported fields
}
LogReader provides paginated access to log files in a session.
func NewLogReader ¶
NewLogReader creates a new LogReader for the given session directory.
func ReadLatestSession ¶
ReadLatestSession reads logs from the most recent session.
func ReadSessionByName ¶
ReadSessionByName reads logs from a session by its name (timestamp string).
func (*LogReader) ReadAll ¶
ReadAll reads all log entries from the session. Use with caution for large log files - prefer ReadPage for pagination.
type LogSession ¶
type LogSession struct {
Name string // directory name (timestamp)
Path string // full path to the session directory
Timestamp time.Time // parsed timestamp
FileCount int // number of log files in the session
}
LogSession represents a logging session (a timestamp-based directory).
func ListSessions ¶
func ListSessions(baseDir string) ([]LogSession, error)
ListSessions returns all logging sessions in the base directory. Sessions are sorted by timestamp, newest first.
type MultiHandler ¶
type MultiHandler struct {
// contains filtered or unexported fields
}
MultiHandler combines multiple slog.Handlers, writing to all of them.
func NewMultiHandler ¶
func NewMultiHandler(handlers ...slog.Handler) *MultiHandler
NewMultiHandler creates a handler that writes to multiple handlers. This is useful for writing logs to both console and file simultaneously.
func (*MultiHandler) Close ¶
func (h *MultiHandler) Close() error
Close closes all handlers in a MultiHandler that implement Closer.
func (*MultiHandler) Flush ¶
func (h *MultiHandler) Flush() error
Flush flushes all handlers in a MultiHandler that implement Flusher.
type SearchCriteria ¶
type SearchCriteria struct {
Level string // filter by log level (e.g., "ERROR", "WARN")
Message string // substring match in message
StartTime *time.Time // filter entries after this time
EndTime *time.Time // filter entries before this time
AttrKey string // filter by attribute key presence
AttrValue string // filter by attribute value (requires AttrKey)
}
SearchLogs searches for log entries matching the given criteria.