Documentation ¶
Overview ¶
Package JSON formats log outputs into JSON using the structure { "context": {}, "message": "message" }
Note that the encoding/json package used underneath does not work with embbeded structures until the go issue https://github.com/golang/go/issues/6213 is solved.
A temporary solution is to define the MarshalJSON() ([]byte, error) method on the context type.
Example:
func (c Context) MarshalJSON() ([]byte, error) { return libjson.Marshal(struct { Time libtime.Time `json:"time"` Level level.Level `json:"level"` }{ Time: c.WithTime.Time(), Level: c.WithLevel.Level(), }) }
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JSON ¶
FormatJSON format into JSON using the structure { "context": v, "message": msg }
Example ¶
buf := bytes.Buffer{} var context = struct { Date time.Time `json:"date"` Level string `json:"level"` }{ Date: time.Date(2022, 2, 1, 12, 30, 0, 0, time.UTC), Level: "info", } logger := genelog.New(&buf). WithContext(context). WithFormatter(JSON) logger.Println("mylog") logger.Println("mylog2") fmt.Println(&buf)
Output: {"context":{"date":"2022-02-01T12:30:00Z","level":"info"},"message":"mylog"} {"context":{"date":"2022-02-01T12:30:00Z","level":"info"},"message":"mylog2"}
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.