added sql storage
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
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,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user