formatter

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2020 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (

	// FieldKeyMsg is Default message field key
	FieldKeyMsg = "message"
	// FieldKeyLevel is Default level field key
	FieldKeyLevel = "level"
	// FieldKeyTime is Default time field key
	FieldKeyTime = "time"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type FieldMap

type FieldMap map[string]string

FieldMap is Default field key map

type Fields

type Fields map[string]interface{}

Fields is custom field map

type JSONFormatter

type JSONFormatter struct {
	FieldMap FieldMap
}

JSONFormatter is custom json formatter for logrus

func (*JSONFormatter) Format

func (j *JSONFormatter) Format(entry *logrus.Entry) ([]byte, error)

Format is custom json format

Setup:

gwlog.GetLogger().SetFormatter(&formatter.JSONFormatter{})

Options:

// Change default field name.
gwlog.GetLogger().SetFormatter(&formatter.JSONFormatter{
	FieldMap: FieldMap{
		FieldKeyMsg:   "@message",
		FieldKeyLevel: "@level",
	}
})

Notice:

If FieldName of FieldMap and FieldName of WithFields are the same, WithFields takes precedence.
Example
// singleton instance
gwlog.GetLogger().SetOutput(os.Stdout)
gwlog.GetLogger().SetFormatter(&JSONFormatter{})
gwlog.GetLogger().WithFields(map[string]interface{}{
	FieldKeyTime: "2000-01-01T00:00:00+09:00", // fixed time
}).Info("aaa")
Output:

{"level":"INFO","message":"aaa","time":"2000-01-01T00:00:00+09:00"}
Example (ChangeDefaultField)
logger := gwlog.GetLogger()
logger.SetOutput(os.Stdout)
logger.SetFormatter(&JSONFormatter{
	FieldMap: FieldMap{
		FieldKeyMsg:   "@message",
		FieldKeyLevel: "@level",
	},
})
logger.WithFields(map[string]interface{}{
	FieldKeyTime: "2000-01-01T00:00:00+09:00", // fixed time
}).Info("aaa")
Output:

{"@level":"INFO","@message":"aaa","time":"2000-01-01T00:00:00+09:00"}
Example (WithFields)
logger := gwlog.GetLogger()
logger.SetOutput(os.Stdout)
logger.SetFormatter(&JSONFormatter{})
logger.WithFields(map[string]interface{}{
	FieldKeyTime: "2000-01-01T00:00:00+09:00", // fixed time
	"hoge":       "hoge",
}).Info("aaa")
Output:

{"hoge":"hoge","level":"INFO","message":"aaa","time":"2000-01-01T00:00:00+09:00"}

Jump to

Keyboard shortcuts

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