1
0

logging.go 1022 B

123456789101112131415161718192021222324252627282930313233343536
  1. package logging
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "gogs.dmsc.dev/arp/auth"
  7. )
  8. // LogOperation logs a CRUD operation to the console
  9. // Format: <datetime> <user> <operation> <entity_info>
  10. func LogOperation(ctx context.Context, operation string, entityInfo string) {
  11. // Get user email from context if available
  12. userEmail := "anonymous"
  13. user, err := auth.CurrentUser(ctx)
  14. if err == nil && user != nil {
  15. userEmail = user.Email
  16. }
  17. timestamp := time.Now().Format("2006-01-02 15:04:05")
  18. fmt.Printf("%s %s %s %s\n", timestamp, userEmail, operation, entityInfo)
  19. }
  20. // LogMutation logs a mutation operation (create/update/delete)
  21. func LogMutation(ctx context.Context, action string, entityType string, identifier string) {
  22. operation := fmt.Sprintf("%s_%s", action, entityType)
  23. LogOperation(ctx, operation, identifier)
  24. }
  25. // LogQuery logs a query operation
  26. func LogQuery(ctx context.Context, queryType string, details string) {
  27. operation := fmt.Sprintf("QUERY_%s", queryType)
  28. LogOperation(ctx, operation, details)
  29. }