diff options
| author | Kujtim Hoxha <[email protected]> | 2025-04-09 19:07:39 +0200 |
|---|---|---|
| committer | Kujtim Hoxha <[email protected]> | 2025-04-09 19:07:39 +0200 |
| commit | d39d52d95d6aaab67fb3a17efb9ed62cc290e72f (patch) | |
| tree | 940bad6f0c847ceb213e5fc684b6d87cbf9d6996 /internal/logging | |
| parent | 0d8d324ac6e640b95f4f2f62fd189399a959319a (diff) | |
| download | opencode-d39d52d95d6aaab67fb3a17efb9ed62cc290e72f.tar.gz opencode-d39d52d95d6aaab67fb3a17efb9ed62cc290e72f.zip | |
finish logs page
Diffstat (limited to 'internal/logging')
| -rw-r--r-- | internal/logging/logger.go | 61 | ||||
| -rw-r--r-- | internal/logging/logging.go | 10 | ||||
| -rw-r--r-- | internal/logging/message.go | 16 | ||||
| -rw-r--r-- | internal/logging/writer.go | 26 |
4 files changed, 86 insertions, 27 deletions
diff --git a/internal/logging/logger.go b/internal/logging/logger.go index d38caeae2..1f0e61d00 100644 --- a/internal/logging/logger.go +++ b/internal/logging/logger.go @@ -12,6 +12,11 @@ import ( const DefaultLevel = "info" +const ( + persistKeyArg = "$persist" + PersistTimeArg = "$persist_time" +) + var levels = map[string]slog.Level{ "debug": slog.LevelDebug, DefaultLevel: slog.LevelInfo, @@ -36,16 +41,16 @@ func ValidLevels() []string { return keys } -func NewLogger(opts Options) *Logger { +func NewLogger(opts Options) Interface { logger := &Logger{} - broker := pubsub.NewBroker[Message]() + broker := pubsub.NewBroker[LogMessage]() writer := &writer{ - messages: []Message{}, + messages: []LogMessage{}, Broker: broker, } handler := slog.NewTextHandler( - io.MultiWriter(append(opts.AdditionalWriters, writer)...), + io.MultiWriter(writer), &slog.HandlerOptions{ Level: slog.Level(levels[opts.Level]), }, @@ -57,8 +62,7 @@ func NewLogger(opts Options) *Logger { } type Options struct { - Level string - AdditionalWriters []io.Writer + Level string } type Logger struct { @@ -66,6 +70,43 @@ type Logger struct { writer *writer } +func (l *Logger) SetLevel(level string) { + if _, ok := levels[level]; !ok { + level = DefaultLevel + } + handler := slog.NewTextHandler( + io.MultiWriter(l.writer), + &slog.HandlerOptions{ + Level: levels[level], + }, + ) + l.logger = slog.New(handler) +} + +// PersistDebug implements Interface. +func (l *Logger) PersistDebug(msg string, args ...any) { + args = append(args, persistKeyArg, true) + l.Debug(msg, args...) +} + +// PersistError implements Interface. +func (l *Logger) PersistError(msg string, args ...any) { + args = append(args, persistKeyArg, true) + l.Error(msg, args...) +} + +// PersistInfo implements Interface. +func (l *Logger) PersistInfo(msg string, args ...any) { + args = append(args, persistKeyArg, true) + l.Info(msg, args...) +} + +// PersistWarn implements Interface. +func (l *Logger) PersistWarn(msg string, args ...any) { + args = append(args, persistKeyArg, true) + l.Warn(msg, args...) +} + func (l *Logger) Debug(msg string, args ...any) { l.logger.Debug(msg, args...) } @@ -82,19 +123,19 @@ func (l *Logger) Error(msg string, args ...any) { l.logger.Error(msg, args...) } -func (l *Logger) List() []Message { +func (l *Logger) List() []LogMessage { return l.writer.messages } -func (l *Logger) Get(id string) (Message, error) { +func (l *Logger) Get(id string) (LogMessage, error) { for _, msg := range l.writer.messages { if msg.ID == id { return msg, nil } } - return Message{}, io.EOF + return LogMessage{}, io.EOF } -func (l *Logger) Subscribe(ctx context.Context) <-chan pubsub.Event[Message] { +func (l *Logger) Subscribe(ctx context.Context) <-chan pubsub.Event[LogMessage] { return l.writer.Subscribe(ctx) } diff --git a/internal/logging/logging.go b/internal/logging/logging.go index 14653d1bb..c23cfaff8 100644 --- a/internal/logging/logging.go +++ b/internal/logging/logging.go @@ -11,7 +11,13 @@ type Interface interface { Info(msg string, args ...any) Warn(msg string, args ...any) Error(msg string, args ...any) - Subscribe(ctx context.Context) <-chan pubsub.Event[Message] + Subscribe(ctx context.Context) <-chan pubsub.Event[LogMessage] - List() []Message + PersistDebug(msg string, args ...any) + PersistInfo(msg string, args ...any) + PersistWarn(msg string, args ...any) + PersistError(msg string, args ...any) + List() []LogMessage + + SetLevel(level string) } diff --git a/internal/logging/message.go b/internal/logging/message.go index a71788911..30ae8f379 100644 --- a/internal/logging/message.go +++ b/internal/logging/message.go @@ -4,13 +4,15 @@ import ( "time" ) -// Message is the event payload for a log message -type Message struct { - ID string - Time time.Time - Level string - Message string `json:"msg"` - Attributes []Attr +// LogMessage is the event payload for a log message +type LogMessage struct { + ID string + Time time.Time + Level string + Persist bool // used when we want to show the mesage in the status bar + PersistTime time.Duration // used when we want to show the mesage in the status bar + Message string `json:"msg"` + Attributes []Attr } type Attr struct { diff --git a/internal/logging/writer.go b/internal/logging/writer.go index b4e899b30..9e597f5c9 100644 --- a/internal/logging/writer.go +++ b/internal/logging/writer.go @@ -10,15 +10,15 @@ import ( ) type writer struct { - messages []Message - *pubsub.Broker[Message] + messages []LogMessage + *pubsub.Broker[LogMessage] } func (w *writer) Write(p []byte) (int, error) { d := logfmt.NewDecoder(bytes.NewReader(p)) for d.ScanRecord() { - msg := Message{ - ID: time.Now().Format(time.RFC3339Nano), + msg := LogMessage{ + ID: fmt.Sprintf("%d", time.Now().UnixNano()), } for d.ScanKeyval() { switch string(d.Key()) { @@ -33,10 +33,20 @@ func (w *writer) Write(p []byte) (int, error) { case "msg": msg.Message = string(d.Value()) default: - msg.Attributes = append(msg.Attributes, Attr{ - Key: string(d.Key()), - Value: string(d.Value()), - }) + if string(d.Key()) == persistKeyArg { + msg.Persist = true + } else if string(d.Key()) == PersistTimeArg { + parsed, err := time.ParseDuration(string(d.Value())) + if err != nil { + continue + } + msg.PersistTime = parsed + } else { + msg.Attributes = append(msg.Attributes, Attr{ + Key: string(d.Key()), + Value: string(d.Value()), + }) + } } } w.messages = append(w.messages, msg) |
