store arbitrary json blobs in postgres over http
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
740B

  1. package main
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. )
  6. type LogMessage struct {
  7. Namespace string `json:"namespace"`
  8. Level int `json:"level"`
  9. Payload interface{} `json:"payload"`
  10. }
  11. func (l *LogMessage) json() ([]byte, error) {
  12. return json.Marshal(l.Payload)
  13. }
  14. func (l *LogMessage) save() (sql.Result, error) {
  15. j, err := l.json()
  16. if err != nil {
  17. return nil, err
  18. }
  19. params := map[string]interface{}{
  20. "namespace": l.Namespace,
  21. "level": l.Level,
  22. "payload": j,
  23. }
  24. // TODO: actually make this return something useful as a first param, might
  25. // need RETURNING log_id?
  26. return db.NamedExec(`
  27. INSERT INTO log
  28. (namespace, level, payload)
  29. VALUES
  30. (:namespace, :level, :payload)`,
  31. params,
  32. )
  33. }