jsonlog.go 545 B

123456789101112131415161718192021222324252627282930
  1. package jsonlog
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. )
  7. type JSONLog struct {
  8. Log string `json:"log,omitempty"`
  9. Stream string `json:"stream,omitempty"`
  10. Created time.Time `json:"time"`
  11. }
  12. func (jl *JSONLog) Format(format string) (string, error) {
  13. if format == "" {
  14. return jl.Log, nil
  15. }
  16. if format == "json" {
  17. m, err := json.Marshal(jl)
  18. return string(m), err
  19. }
  20. return fmt.Sprintf("%s %s", jl.Created.Format(format), jl.Log), nil
  21. }
  22. func (jl *JSONLog) Reset() {
  23. jl.Log = ""
  24. jl.Stream = ""
  25. jl.Created = time.Time{}
  26. }