logging

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package logging provides structured logging functionality for the application. It wraps the standard slog package with additional features like session-based log persistence, panic recovery, and message directory management.

Index

Constants

View Source
const (
	PersistTimeArg = "$_persist_time"
)

Variables

View Source
var (
	MessageDir string
)

MessageDir is the directory where session messages are stored.

Functions

func AppendToSessionLogFile

func AppendToSessionLogFile(sessionId string, filename string, content string) string

AppendToSessionLogFile appends content to a session log file. It returns the absolute path to the file on success, or an empty string on failure. The function validates that the resulting path stays within the message directory to prevent path traversal attacks.

func AppendToStreamSessionLog

func AppendToStreamSessionLog(sessionId string, requestSeqId int, chunk string) string

AppendToStreamSessionLog appends a chunk to a session stream log file.

func AppendToStreamSessionLogJson

func AppendToStreamSessionLogJson(sessionId string, requestSeqId int, jsonableChunk any) string

AppendToStreamSessionLogJson marshals the chunk to JSON and appends it to a session stream log.

func Debug

func Debug(msg string, args ...any)

Debug logs a message at DEBUG level with the caller's source location.

func DebugPersist

func DebugPersist(msg string, args ...any)

DebugPersist logs a message at DEBUG level that should be persisted to the status bar.

func Error

func Error(msg string, args ...any)

Error logs a message at ERROR level with the caller's source location.

func ErrorPersist

func ErrorPersist(msg string, args ...any)

ErrorPersist logs a message at ERROR level that should be persisted to the status bar.

func GetMessageDir added in v1.5.0

func GetMessageDir() string

GetMessageDir returns the directory where session messages are stored.

func GetSessionPrefix

func GetSessionPrefix(sessionId string) string

GetSessionPrefix returns a shortened version of the session ID for use in filenames. If the session ID is shorter than 8 characters, it returns the full ID. If the session ID is empty, it returns an empty string.

func Info

func Info(msg string, args ...any)

Info logs a message at INFO level with the caller's source location.

func InfoPersist

func InfoPersist(msg string, args ...any)

InfoPersist logs a message at INFO level that should be persisted to the status bar.

func NewWriter

func NewWriter() *writer

NewWriter creates a new writer instance for parsing logfmt format.

func RecoverPanic

func RecoverPanic(name string, cleanup func())

RecoverPanic is a common function to handle panics gracefully. It logs the error, creates a panic log file with stack trace, and executes an optional cleanup function before returning.

func SetMessageDir added in v1.5.0

func SetMessageDir(dir string)

SetMessageDir sets the directory where session messages are stored.

func Subscribe

func Subscribe() (<-chan pubsub.Event[LogMessage], func())

Subscribe returns a channel of log messages for real-time log monitoring. The caller is responsible for managing the subscription lifecycle.

func SubscribeWithContext added in v1.7.0

func SubscribeWithContext(ctx context.Context) <-chan pubsub.Event[LogMessage]

SubscribeWithContext returns a channel of log messages that automatically unsubscribes when the context is cancelled.

func Warn

func Warn(msg string, args ...any)

Warn logs a message at WARN level with the caller's source location.

func WarnPersist

func WarnPersist(msg string, args ...any)

WarnPersist logs a message at WARN level that should be persisted to the status bar.

func WriteChatResponseJson

func WriteChatResponseJson(sessionId string, requestSeqId int, response any) string

WriteChatResponseJson marshals the response to JSON and writes it to a session log file.

func WriteRequestMessage

func WriteRequestMessage(sessionId string, requestSeqId int, message string) string

WriteRequestMessage writes a request message to a session log file.

func WriteRequestMessageJson

func WriteRequestMessageJson(sessionId string, requestSeqId int, message any) string

WriteRequestMessageJson marshals the message to JSON and writes it to a session log file.

func WriteToolResultsJson

func WriteToolResultsJson(sessionId string, requestSeqId int, toolResults any) string

WriteToolResultsJson marshals the tool results to JSON and writes them to a session log file.

Types

type Attr

type Attr struct {
	Key   string
	Value string
}

Attr represents a key-value pair for log message attributes.

type LogData

type LogData struct {
	*pubsub.Broker[LogMessage]
	// contains filtered or unexported fields
}

LogData holds log messages and provides pub/sub functionality for log events.

func (*LogData) Add

func (l *LogData) Add(msg LogMessage)

Add adds a log message to the LogData and publishes it to subscribers.

func (*LogData) List

func (l *LogData) List() []LogMessage

List returns all log messages in the LogData.

type LogMessage

type LogMessage struct {
	ID          string
	Time        time.Time
	Level       string
	Persist     bool          // used when we want to show the message in the status bar
	PersistTime time.Duration // used when we want to show the message in the status bar
	Message     string        `json:"msg"`
	Attributes  []Attr
}

LogMessage represents a log message with metadata for the logging system.

func List

func List() []LogMessage

List returns all log messages from the default LogData.

Jump to

Keyboard shortcuts

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