38 lines
743 B
Go
38 lines
743 B
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
)
|
|
|
|
type LogMessage struct {
|
|
Namespace string `json:"namespace"`
|
|
Level int `json:"level"`
|
|
Payload interface{} `json:"payload"`
|
|
}
|
|
|
|
func (l *LogMessage) json() ([]byte, error) {
|
|
return json.Marshal(l.Payload)
|
|
}
|
|
|
|
func (l *LogMessage) save() (sql.Result, error) {
|
|
j, err := l.json()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
params := map[string]interface{}{
|
|
"namespace": l.Namespace,
|
|
"level": l.Level,
|
|
"payload": j,
|
|
}
|
|
// TODO: actually make this return something useful as a first param, might
|
|
// need RETURNING log_id?
|
|
return db.NamedExecMap(`
|
|
INSERT INTO log
|
|
(namespace, level, payload)
|
|
VALUES
|
|
(:namespace, :level, :payload)`,
|
|
params,
|
|
)
|
|
}
|