logging.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package logging
  2. import (
  3. "bytes"
  4. "context"
  5. "database/sql"
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "log/slog"
  10. "os"
  11. "runtime/debug"
  12. "strings"
  13. "sync"
  14. "time"
  15. "github.com/go-logfmt/logfmt"
  16. "github.com/google/uuid"
  17. "github.com/opencode-ai/opencode/internal/db"
  18. "github.com/opencode-ai/opencode/internal/pubsub"
  19. )
  20. type Log struct {
  21. ID string
  22. SessionID string
  23. Timestamp int64
  24. Level string
  25. Message string
  26. Attributes map[string]string
  27. CreatedAt int64
  28. }
  29. const (
  30. EventLogCreated pubsub.EventType = "log_created"
  31. )
  32. type Service interface {
  33. pubsub.Subscriber[Log]
  34. Create(ctx context.Context, log Log) error
  35. ListBySession(ctx context.Context, sessionID string) ([]Log, error)
  36. ListAll(ctx context.Context, limit int) ([]Log, error)
  37. }
  38. type service struct {
  39. db *db.Queries
  40. broker *pubsub.Broker[Log]
  41. mu sync.RWMutex
  42. }
  43. var globalLoggingService *service
  44. func InitService(dbConn *sql.DB) error {
  45. if globalLoggingService != nil {
  46. return fmt.Errorf("logging service already initialized")
  47. }
  48. queries := db.New(dbConn)
  49. broker := pubsub.NewBroker[Log]()
  50. globalLoggingService = &service{
  51. db: queries,
  52. broker: broker,
  53. }
  54. return nil
  55. }
  56. func GetService() Service {
  57. if globalLoggingService == nil {
  58. panic("logging service not initialized. Call logging.InitService() first.")
  59. }
  60. return globalLoggingService
  61. }
  62. func (s *service) Create(ctx context.Context, log Log) error {
  63. s.mu.Lock()
  64. defer s.mu.Unlock()
  65. if log.ID == "" {
  66. log.ID = uuid.New().String()
  67. }
  68. if log.Timestamp == 0 {
  69. log.Timestamp = time.Now().UnixMilli()
  70. }
  71. if log.CreatedAt == 0 {
  72. log.CreatedAt = time.Now().UnixMilli()
  73. }
  74. if log.Level == "" {
  75. log.Level = "info"
  76. }
  77. var attributesJSON sql.NullString
  78. if len(log.Attributes) > 0 {
  79. attributesBytes, err := json.Marshal(log.Attributes)
  80. if err != nil {
  81. return fmt.Errorf("failed to marshal log attributes: %w", err)
  82. }
  83. attributesJSON = sql.NullString{String: string(attributesBytes), Valid: true}
  84. }
  85. err := s.db.CreateLog(ctx, db.CreateLogParams{
  86. ID: log.ID,
  87. SessionID: sql.NullString{String: log.SessionID, Valid: log.SessionID != ""},
  88. Timestamp: log.Timestamp / 1000,
  89. Level: log.Level,
  90. Message: log.Message,
  91. Attributes: attributesJSON,
  92. CreatedAt: log.CreatedAt / 1000,
  93. })
  94. if err != nil {
  95. return fmt.Errorf("db.CreateLog: %w", err)
  96. }
  97. s.broker.Publish(EventLogCreated, log)
  98. return nil
  99. }
  100. func (s *service) ListBySession(ctx context.Context, sessionID string) ([]Log, error) {
  101. s.mu.RLock()
  102. defer s.mu.RUnlock()
  103. dbLogs, err := s.db.ListLogsBySession(ctx, sql.NullString{String: sessionID, Valid: true})
  104. if err != nil {
  105. return nil, fmt.Errorf("db.ListLogsBySession: %w", err)
  106. }
  107. return s.fromDBItems(dbLogs)
  108. }
  109. func (s *service) ListAll(ctx context.Context, limit int) ([]Log, error) {
  110. s.mu.RLock()
  111. defer s.mu.RUnlock()
  112. dbLogs, err := s.db.ListAllLogs(ctx, int64(limit))
  113. if err != nil {
  114. return nil, fmt.Errorf("db.ListAllLogs: %w", err)
  115. }
  116. return s.fromDBItems(dbLogs)
  117. }
  118. func (s *service) Subscribe(ctx context.Context) <-chan pubsub.Event[Log] {
  119. return s.broker.Subscribe(ctx)
  120. }
  121. func (s *service) fromDBItems(items []db.Log) ([]Log, error) {
  122. logs := make([]Log, len(items))
  123. for i, item := range items {
  124. log := Log{
  125. ID: item.ID,
  126. SessionID: item.SessionID.String,
  127. Timestamp: item.Timestamp * 1000,
  128. Level: item.Level,
  129. Message: item.Message,
  130. CreatedAt: item.CreatedAt * 1000,
  131. }
  132. if item.Attributes.Valid && item.Attributes.String != "" {
  133. if err := json.Unmarshal([]byte(item.Attributes.String), &log.Attributes); err != nil {
  134. slog.Error("Failed to unmarshal log attributes", "log_id", item.ID, "error", err)
  135. log.Attributes = make(map[string]string)
  136. }
  137. } else {
  138. log.Attributes = make(map[string]string)
  139. }
  140. logs[i] = log
  141. }
  142. return logs, nil
  143. }
  144. func Create(ctx context.Context, log Log) error {
  145. return GetService().Create(ctx, log)
  146. }
  147. func ListBySession(ctx context.Context, sessionID string) ([]Log, error) {
  148. return GetService().ListBySession(ctx, sessionID)
  149. }
  150. func ListAll(ctx context.Context, limit int) ([]Log, error) {
  151. return GetService().ListAll(ctx, limit)
  152. }
  153. func SubscribeToEvents(ctx context.Context) <-chan pubsub.Event[Log] {
  154. return GetService().Subscribe(ctx)
  155. }
  156. type slogWriter struct{}
  157. func (sw *slogWriter) Write(p []byte) (n int, err error) {
  158. // Example: time=2024-05-09T12:34:56.789-05:00 level=INFO msg="User request" session=xyz foo=bar
  159. d := logfmt.NewDecoder(bytes.NewReader(p))
  160. for d.ScanRecord() {
  161. logEntry := Log{
  162. Attributes: make(map[string]string),
  163. }
  164. hasTimestamp := false
  165. for d.ScanKeyval() {
  166. key := string(d.Key())
  167. value := string(d.Value())
  168. switch key {
  169. case "time":
  170. parsedTime, timeErr := time.Parse(time.RFC3339Nano, value)
  171. if timeErr != nil {
  172. parsedTime, timeErr = time.Parse(time.RFC3339, value)
  173. if timeErr != nil {
  174. slog.Error("Failed to parse time in slog writer", "value", value, "error", timeErr)
  175. logEntry.Timestamp = time.Now().UnixMilli()
  176. hasTimestamp = true
  177. continue
  178. }
  179. }
  180. logEntry.Timestamp = parsedTime.UnixMilli()
  181. hasTimestamp = true
  182. case "level":
  183. logEntry.Level = strings.ToLower(value)
  184. case "msg", "message":
  185. logEntry.Message = value
  186. case "session_id", "session", "sid":
  187. logEntry.SessionID = value
  188. default:
  189. logEntry.Attributes[key] = value
  190. }
  191. }
  192. if d.Err() != nil {
  193. return len(p), fmt.Errorf("logfmt.ScanRecord: %w", d.Err())
  194. }
  195. if !hasTimestamp {
  196. logEntry.Timestamp = time.Now().UnixMilli()
  197. }
  198. // Create log entry via the service (non-blocking or handle error appropriately)
  199. // Using context.Background() as this is a low-level logging write.
  200. go func(le Log) { // Run in a goroutine to avoid blocking slog
  201. if globalLoggingService == nil {
  202. // If the logging service is not initialized, log the message to stderr
  203. // fmt.Fprintf(os.Stderr, "ERROR [logging.slogWriter]: logging service not initialized\n")
  204. return
  205. }
  206. if err := Create(context.Background(), le); err != nil {
  207. // Log internal error using a more primitive logger to avoid loops
  208. fmt.Fprintf(os.Stderr, "ERROR [logging.slogWriter]: failed to persist log: %v\n", err)
  209. }
  210. }(logEntry)
  211. }
  212. if d.Err() != nil {
  213. return len(p), fmt.Errorf("logfmt.ScanRecord final: %w", d.Err())
  214. }
  215. return len(p), nil
  216. }
  217. func NewSlogWriter() io.Writer {
  218. return &slogWriter{}
  219. }
  220. // RecoverPanic is a common function to handle panics gracefully.
  221. // It logs the error, creates a panic log file with stack trace,
  222. // and executes an optional cleanup function.
  223. func RecoverPanic(name string, cleanup func()) {
  224. if r := recover(); r != nil {
  225. errorMsg := fmt.Sprintf("Panic in %s: %v", name, r)
  226. // Use slog directly here, as our service might be the one panicking.
  227. slog.Error(errorMsg)
  228. // status.Error(errorMsg)
  229. timestamp := time.Now().Format("20060102-150405")
  230. filename := fmt.Sprintf("opencode-panic-%s-%s.log", name, timestamp)
  231. file, err := os.Create(filename)
  232. if err != nil {
  233. errMsg := fmt.Sprintf("Failed to create panic log file '%s': %v", filename, err)
  234. slog.Error(errMsg)
  235. // status.Error(errMsg)
  236. } else {
  237. defer file.Close()
  238. fmt.Fprintf(file, "Panic in %s: %v\n\n", name, r)
  239. fmt.Fprintf(file, "Time: %s\n\n", time.Now().Format(time.RFC3339))
  240. fmt.Fprintf(file, "Stack Trace:\n%s\n", string(debug.Stack())) // Capture stack trace
  241. infoMsg := fmt.Sprintf("Panic details written to %s", filename)
  242. slog.Info(infoMsg)
  243. // status.Info(infoMsg)
  244. }
  245. if cleanup != nil {
  246. cleanup()
  247. }
  248. }
  249. }