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 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"}
Click to show internal directories.
Click to hide internal directories.