logging.go 7.3 KB

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