json

package
v1.0.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2022 License: MIT Imports: 3 Imported by: 0

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

func JSON(v interface{}, msg string) (string, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL