github.com/resin-io/docker@v1.13.1/pkg/jsonlog/jsonlog.go (about)

     1  package jsonlog
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"time"
     7  )
     8  
     9  // JSONLog represents a log message, typically a single entry from a given log stream.
    10  // JSONLogs can be easily serialized to and from JSON and support custom formatting.
    11  type JSONLog struct {
    12  	// Log is the log message
    13  	Log string `json:"log,omitempty"`
    14  	// Stream is the log source
    15  	Stream string `json:"stream,omitempty"`
    16  	// Created is the created timestamp of log
    17  	Created time.Time `json:"time"`
    18  	// Attrs is the list of extra attributes provided by the user
    19  	Attrs map[string]string `json:"attrs,omitempty"`
    20  }
    21  
    22  // Format returns the log formatted according to format
    23  // If format is nil, returns the log message
    24  // If format is json, returns the log marshaled in json format
    25  // By default, returns the log with the log time formatted according to format.
    26  func (jl *JSONLog) Format(format string) (string, error) {
    27  	if format == "" {
    28  		return jl.Log, nil
    29  	}
    30  	if format == "json" {
    31  		m, err := json.Marshal(jl)
    32  		return string(m), err
    33  	}
    34  	return fmt.Sprintf("%s %s", jl.Created.Format(format), jl.Log), nil
    35  }
    36  
    37  // Reset resets the log to nil.
    38  func (jl *JSONLog) Reset() {
    39  	jl.Log = ""
    40  	jl.Stream = ""
    41  	jl.Created = time.Time{}
    42  }