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
- Variables
- func AppendToSessionLogFile(sessionId string, filename string, content string) string
- func AppendToStreamSessionLog(sessionId string, requestSeqId int, chunk string) string
- func AppendToStreamSessionLogJson(sessionId string, requestSeqId int, jsonableChunk any) string
- func Debug(msg string, args ...any)
- func DebugPersist(msg string, args ...any)
- func Error(msg string, args ...any)
- func ErrorPersist(msg string, args ...any)
- func GetMessageDir() string
- func GetSessionPrefix(sessionId string) string
- func Info(msg string, args ...any)
- func InfoPersist(msg string, args ...any)
- func NewWriter() *writer
- func RecoverPanic(name string, cleanup func())
- func SetMessageDir(dir string)
- func Subscribe() (<-chan pubsub.Event[LogMessage], func())
- func SubscribeWithContext(ctx context.Context) <-chan pubsub.Event[LogMessage]
- func Warn(msg string, args ...any)
- func WarnPersist(msg string, args ...any)
- func WriteChatResponseJson(sessionId string, requestSeqId int, response any) string
- func WriteRequestMessage(sessionId string, requestSeqId int, message string) string
- func WriteRequestMessageJson(sessionId string, requestSeqId int, message any) string
- func WriteToolResultsJson(sessionId string, requestSeqId int, toolResults any) string
- type Attr
- type LogData
- type LogMessage
Constants ¶
const (
PersistTimeArg = "$_persist_time"
)
Variables ¶
var (
MessageDir string
)
MessageDir is the directory where session messages are stored.
Functions ¶
func AppendToSessionLogFile ¶
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 ¶
AppendToStreamSessionLog appends a chunk to a session stream log file.
func AppendToStreamSessionLogJson ¶
AppendToStreamSessionLogJson marshals the chunk to JSON and appends it to a session stream log.
func DebugPersist ¶
DebugPersist logs a message at DEBUG level that should be persisted to the status bar.
func ErrorPersist ¶
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 ¶
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 InfoPersist ¶
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 WarnPersist ¶
WarnPersist logs a message at WARN level that should be persisted to the status bar.
func WriteChatResponseJson ¶
WriteChatResponseJson marshals the response to JSON and writes it to a session log file.
func WriteRequestMessage ¶
WriteRequestMessage writes a request message to a session log file.
func WriteRequestMessageJson ¶
WriteRequestMessageJson marshals the message to JSON and writes it to a session log file.
Types ¶
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.