summaryrefslogtreecommitdiffhomepage
path: root/internal/logging/logging.go
blob: 0ef5f0f281dbe3e118b22f9607debff499b9682c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package logging

import (
	"bytes"
	"context"
	"database/sql"
	"encoding/json"
	"fmt"
	"io"
	"log/slog"
	"os"
	"runtime/debug"
	"strings"
	"sync"
	"time"

	"github.com/go-logfmt/logfmt"
	"github.com/google/uuid"
	"github.com/opencode-ai/opencode/internal/db"
	"github.com/opencode-ai/opencode/internal/pubsub"
)

type Log struct {
	ID         string
	SessionID  string
	Timestamp  int64
	Level      string
	Message    string
	Attributes map[string]string
	CreatedAt  int64
}

const (
	EventLogCreated pubsub.EventType = "log_created"
)

type Service interface {
	pubsub.Subscriber[Log]

	Create(ctx context.Context, log Log) error
	ListBySession(ctx context.Context, sessionID string) ([]Log, error)
	ListAll(ctx context.Context, limit int) ([]Log, error)
}

type service struct {
	db     *db.Queries
	broker *pubsub.Broker[Log]
	mu     sync.RWMutex
}

var globalLoggingService *service

func InitService(dbConn *sql.DB) error {
	if globalLoggingService != nil {
		return fmt.Errorf("logging service already initialized")
	}
	queries := db.New(dbConn)
	broker := pubsub.NewBroker[Log]()

	globalLoggingService = &service{
		db:     queries,
		broker: broker,
	}
	return nil
}

func GetService() Service {
	if globalLoggingService == nil {
		panic("logging service not initialized. Call logging.InitService() first.")
	}
	return globalLoggingService
}

func (s *service) Create(ctx context.Context, log Log) error {
	s.mu.Lock()
	defer s.mu.Unlock()

	if log.ID == "" {
		log.ID = uuid.New().String()
	}
	if log.Timestamp == 0 {
		log.Timestamp = time.Now().UnixMilli()
	}
	if log.CreatedAt == 0 {
		log.CreatedAt = time.Now().UnixMilli()
	}
	if log.Level == "" {
		log.Level = "info"
	}

	var attributesJSON sql.NullString
	if len(log.Attributes) > 0 {
		attributesBytes, err := json.Marshal(log.Attributes)
		if err != nil {
			return fmt.Errorf("failed to marshal log attributes: %w", err)
		}
		attributesJSON = sql.NullString{String: string(attributesBytes), Valid: true}
	}

	err := s.db.CreateLog(ctx, db.CreateLogParams{
		ID:         log.ID,
		SessionID:  sql.NullString{String: log.SessionID, Valid: log.SessionID != ""},
		Timestamp:  log.Timestamp / 1000,
		Level:      log.Level,
		Message:    log.Message,
		Attributes: attributesJSON,
		CreatedAt:  log.CreatedAt / 1000,
	})
	if err != nil {
		return fmt.Errorf("db.CreateLog: %w", err)
	}

	s.broker.Publish(EventLogCreated, log)
	return nil
}

func (s *service) ListBySession(ctx context.Context, sessionID string) ([]Log, error) {
	s.mu.RLock()
	defer s.mu.RUnlock()

	dbLogs, err := s.db.ListLogsBySession(ctx, sql.NullString{String: sessionID, Valid: true})
	if err != nil {
		return nil, fmt.Errorf("db.ListLogsBySession: %w", err)
	}
	return s.fromDBItems(dbLogs)
}

func (s *service) ListAll(ctx context.Context, limit int) ([]Log, error) {
	s.mu.RLock()
	defer s.mu.RUnlock()

	dbLogs, err := s.db.ListAllLogs(ctx, int64(limit))
	if err != nil {
		return nil, fmt.Errorf("db.ListAllLogs: %w", err)
	}
	return s.fromDBItems(dbLogs)
}

func (s *service) Subscribe(ctx context.Context) <-chan pubsub.Event[Log] {
	return s.broker.Subscribe(ctx)
}

func (s *service) fromDBItems(items []db.Log) ([]Log, error) {
	logs := make([]Log, len(items))
	for i, item := range items {
		log := Log{
			ID:        item.ID,
			SessionID: item.SessionID.String,
			Timestamp: item.Timestamp * 1000,
			Level:     item.Level,
			Message:   item.Message,
			CreatedAt: item.CreatedAt * 1000,
		}
		if item.Attributes.Valid && item.Attributes.String != "" {
			if err := json.Unmarshal([]byte(item.Attributes.String), &log.Attributes); err != nil {
				slog.Error("Failed to unmarshal log attributes", "log_id", item.ID, "error", err)
				log.Attributes = make(map[string]string)
			}
		} else {
			log.Attributes = make(map[string]string)
		}
		logs[i] = log
	}
	return logs, nil
}

func Create(ctx context.Context, log Log) error {
	return GetService().Create(ctx, log)
}

func ListBySession(ctx context.Context, sessionID string) ([]Log, error) {
	return GetService().ListBySession(ctx, sessionID)
}

func ListAll(ctx context.Context, limit int) ([]Log, error) {
	return GetService().ListAll(ctx, limit)
}

func SubscribeToEvents(ctx context.Context) <-chan pubsub.Event[Log] {
	return GetService().Subscribe(ctx)
}

type slogWriter struct{}

func (sw *slogWriter) Write(p []byte) (n int, err error) {
	// Example: time=2024-05-09T12:34:56.789-05:00 level=INFO msg="User request" session=xyz foo=bar
	d := logfmt.NewDecoder(bytes.NewReader(p))
	for d.ScanRecord() {
		logEntry := Log{
			Attributes: make(map[string]string),
		}
		hasTimestamp := false

		for d.ScanKeyval() {
			key := string(d.Key())
			value := string(d.Value())

			switch key {
			case "time":
				parsedTime, timeErr := time.Parse(time.RFC3339Nano, value)
				if timeErr != nil {
					parsedTime, timeErr = time.Parse(time.RFC3339, value)
					if timeErr != nil {
						slog.Error("Failed to parse time in slog writer", "value", value, "error", timeErr)
						logEntry.Timestamp = time.Now().UnixMilli()
						hasTimestamp = true
						continue
					}
				}
				logEntry.Timestamp = parsedTime.UnixMilli()
				hasTimestamp = true
			case "level":
				logEntry.Level = strings.ToLower(value)
			case "msg", "message":
				logEntry.Message = value
			case "session_id", "session", "sid":
				logEntry.SessionID = value
			default:
				logEntry.Attributes[key] = value
			}
		}

		if d.Err() != nil {
			return len(p), fmt.Errorf("logfmt.ScanRecord: %w", d.Err())
		}

		if !hasTimestamp {
			logEntry.Timestamp = time.Now().UnixMilli()
		}

		// Create log entry via the service (non-blocking or handle error appropriately)
		// Using context.Background() as this is a low-level logging write.
		go func(le Log) { // Run in a goroutine to avoid blocking slog
			if globalLoggingService == nil {
				// If the logging service is not initialized, log the message to stderr
				// fmt.Fprintf(os.Stderr, "ERROR [logging.slogWriter]: logging service not initialized\n")
				return
			}
			if err := Create(context.Background(), le); err != nil {
				// Log internal error using a more primitive logger to avoid loops
				fmt.Fprintf(os.Stderr, "ERROR [logging.slogWriter]: failed to persist log: %v\n", err)
			}
		}(logEntry)
	}
	if d.Err() != nil {
		return len(p), fmt.Errorf("logfmt.ScanRecord final: %w", d.Err())
	}
	return len(p), nil
}

func NewSlogWriter() io.Writer {
	return &slogWriter{}
}

// 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.
func RecoverPanic(name string, cleanup func()) {
	if r := recover(); r != nil {
		errorMsg := fmt.Sprintf("Panic in %s: %v", name, r)
		// Use slog directly here, as our service might be the one panicking.
		slog.Error(errorMsg)
		// status.Error(errorMsg)

		timestamp := time.Now().Format("20060102-150405")
		filename := fmt.Sprintf("opencode-panic-%s-%s.log", name, timestamp)

		file, err := os.Create(filename)
		if err != nil {
			errMsg := fmt.Sprintf("Failed to create panic log file '%s': %v", filename, err)
			slog.Error(errMsg)
			// status.Error(errMsg)
		} else {
			defer file.Close()
			fmt.Fprintf(file, "Panic in %s: %v\n\n", name, r)
			fmt.Fprintf(file, "Time: %s\n\n", time.Now().Format(time.RFC3339))
			fmt.Fprintf(file, "Stack Trace:\n%s\n", string(debug.Stack())) // Capture stack trace
			infoMsg := fmt.Sprintf("Panic details written to %s", filename)
			slog.Info(infoMsg)
			// status.Info(infoMsg)
		}

		if cleanup != nil {
			cleanup()
		}
	}
}