2014-07-30 11:39:03 -04:00
|
|
|
package jsonlog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-08-07 23:28:22 -04:00
|
|
|
// JSONLog represents a log message, typically a single entry from a given log stream.
|
|
|
|
// JSONLogs can be easily serialized to and from JSON and support custom formatting.
|
2014-07-30 11:39:03 -04:00
|
|
|
type JSONLog struct {
|
2015-08-07 23:28:22 -04:00
|
|
|
// Log is the log message
|
|
|
|
Log string `json:"log,omitempty"`
|
|
|
|
// Stream is the log source
|
|
|
|
Stream string `json:"stream,omitempty"`
|
|
|
|
// Created is the created timestamp of log
|
2014-07-30 11:39:03 -04:00
|
|
|
Created time.Time `json:"time"`
|
|
|
|
}
|
|
|
|
|
2015-08-07 23:28:22 -04:00
|
|
|
// Format returns the log formatted according to format
|
|
|
|
// If format is nil, returns the log message
|
|
|
|
// If format is json, returns the log marshalled in json format
|
|
|
|
// By defalut, returns the log with the log time formatted according to format.
|
2014-07-30 11:39:03 -04:00
|
|
|
func (jl *JSONLog) Format(format string) (string, error) {
|
|
|
|
if format == "" {
|
|
|
|
return jl.Log, nil
|
|
|
|
}
|
|
|
|
if format == "json" {
|
|
|
|
m, err := json.Marshal(jl)
|
|
|
|
return string(m), err
|
|
|
|
}
|
2015-01-28 17:33:15 -05:00
|
|
|
return fmt.Sprintf("%s %s", jl.Created.Format(format), jl.Log), nil
|
2014-07-30 11:39:03 -04:00
|
|
|
}
|
|
|
|
|
2015-08-07 23:28:22 -04:00
|
|
|
// Reset resets the log to nil.
|
2014-09-18 11:04:18 -04:00
|
|
|
func (jl *JSONLog) Reset() {
|
|
|
|
jl.Log = ""
|
|
|
|
jl.Stream = ""
|
|
|
|
jl.Created = time.Time{}
|
|
|
|
}
|