logging.go 7.6 KB

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