Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Formatter ¶
type Formatter struct {
// FieldsOrder - default: fields sorted alphabetically
FieldsOrder []string
// TimestampFormat - default: time.StampMilli = "Jan _2 15:04:05.000"
TimestampFormat string
// HideKeys - show [fieldValue] instead of [fieldKey:fieldValue]
HideKeys bool
// NoColors - disable colors
NoColors bool
// NoFieldsColors - apply colors only to the level, default is level + fields
NoFieldsColors bool
// NoFieldsSpace - no space between fields
NoFieldsSpace bool
// ShowFullLevel - show a full level [WARNING] instead of [WARN]
ShowFullLevel bool
// NoUppercaseLevel - no upper case for level value
NoUppercaseLevel bool
// TrimMessages - trim whitespaces on messages
TrimMessages bool
// CallerFirst - print caller info first
CallerFirst bool
// CustomCallerFormatter - set custom formatter for caller info
CustomCallerFormatter func(*runtime.Frame) string
}
Formatter - logrus formatter, implements logrus.Formatter
func (*Formatter) Format ¶
Format an log entry
Example (Default) ¶
package main
import (
"os"
"github.com/andoma-go/logrus"
nested "github.com/andoma-go/logrus-formatter-nested"
)
func main() {
l := logrus.New()
l.SetOutput(os.Stdout)
l.SetLevel(logrus.DebugLevel)
l.SetFormatter(&nested.Formatter{
NoColors: true,
TimestampFormat: "-",
})
l.Debug("test1")
l.Info("test2")
l.Warn("test3")
l.Error("test4")
}
Output: - [DEBU] test1 - [INFO] test2 - [WARN] test3 - [ERRO] test4
Example (Field_order) ¶
package main
import (
"os"
"github.com/andoma-go/logrus"
nested "github.com/andoma-go/logrus-formatter-nested"
)
func main() {
l := logrus.New()
l.SetOutput(os.Stdout)
l.SetLevel(logrus.DebugLevel)
l.SetFormatter(&nested.Formatter{
NoColors: true,
TimestampFormat: "-",
FieldsOrder: []string{"component", "category"},
HideKeys: false,
})
ll := l.WithField("component", "main")
lll := ll.WithField("category", "rest")
l.Info("test1")
ll.Info("test2")
lll.Info("test3")
}
Output: - [INFO] test1 - [INFO] [component:main] test2 - [INFO] [component:main] [category:rest] test3
Example (Full_level) ¶
package main
import (
"os"
"github.com/andoma-go/logrus"
nested "github.com/andoma-go/logrus-formatter-nested"
)
func main() {
l := logrus.New()
l.SetOutput(os.Stdout)
l.SetLevel(logrus.DebugLevel)
l.SetFormatter(&nested.Formatter{
NoColors: true,
TimestampFormat: "-",
ShowFullLevel: true,
})
l.Debug("test1")
l.Info("test2")
l.Warn("test3")
l.Error(" test4")
}
Output: - [DEBUG] test1 - [INFO] test2 - [WARNING] test3 - [ERROR] test4
Example (Hide_keys) ¶
package main
import (
"os"
"github.com/andoma-go/logrus"
nested "github.com/andoma-go/logrus-formatter-nested"
)
func main() {
l := logrus.New()
l.SetOutput(os.Stdout)
l.SetLevel(logrus.DebugLevel)
l.SetFormatter(&nested.Formatter{
NoColors: true,
TimestampFormat: "-",
HideKeys: true,
})
ll := l.WithField("category", "rest")
l.Info("test1")
ll.Info("test2")
}
Output: - [INFO] test1 - [INFO] [rest] test2
Example (No_fields_space) ¶
package main
import (
"os"
"github.com/andoma-go/logrus"
nested "github.com/andoma-go/logrus-formatter-nested"
)
func main() {
l := logrus.New()
l.SetOutput(os.Stdout)
l.SetLevel(logrus.DebugLevel)
l.SetFormatter(&nested.Formatter{
NoColors: true,
TimestampFormat: "-",
FieldsOrder: []string{"component", "category"},
HideKeys: false,
NoFieldsSpace: true,
})
ll := l.WithField("component", "main")
lll := ll.WithField("category", "rest")
l.Info("test1")
ll.Info("test2")
lll.Info("test3")
}
Output: - [INFO] test1 - [INFO][component:main] test2 - [INFO][component:main][category:rest] test3
Example (No_uppercase_level) ¶
package main
import (
"os"
"github.com/andoma-go/logrus"
nested "github.com/andoma-go/logrus-formatter-nested"
)
func main() {
l := logrus.New()
l.SetOutput(os.Stdout)
l.SetLevel(logrus.DebugLevel)
l.SetFormatter(&nested.Formatter{
NoColors: true,
TimestampFormat: "-",
FieldsOrder: []string{"component", "category"},
NoUppercaseLevel: true,
})
ll := l.WithField("component", "main")
lll := ll.WithField("category", "rest")
llll := ll.WithField("category", "other")
l.Debug("test1")
ll.Info("test2")
lll.Warn("test3")
llll.Error("test4")
}
Output: - [debu] test1 - [info] [component:main] test2 - [warn] [component:main] [category:rest] test3 - [erro] [component:main] [category:other] test4
Example (Show_keys) ¶
package main
import (
"os"
"github.com/andoma-go/logrus"
nested "github.com/andoma-go/logrus-formatter-nested"
)
func main() {
l := logrus.New()
l.SetOutput(os.Stdout)
l.SetLevel(logrus.DebugLevel)
l.SetFormatter(&nested.Formatter{
NoColors: true,
TimestampFormat: "-",
HideKeys: false,
})
ll := l.WithField("category", "rest")
l.Info("test1")
ll.Info("test2")
}
Output: - [INFO] test1 - [INFO] [category:rest] test2
Example (Sort_order) ¶
package main
import (
"os"
"github.com/andoma-go/logrus"
nested "github.com/andoma-go/logrus-formatter-nested"
)
func main() {
l := logrus.New()
l.SetOutput(os.Stdout)
l.SetLevel(logrus.DebugLevel)
l.SetFormatter(&nested.Formatter{
NoColors: true,
TimestampFormat: "-",
HideKeys: false,
})
ll := l.WithField("component", "main")
lll := ll.WithField("category", "rest")
l.Info("test1")
ll.Info("test2")
lll.Info("test3")
}
Output: - [INFO] test1 - [INFO] [component:main] test2 - [INFO] [category:rest] [component:main] test3
Example (Trim_message) ¶
package main
import (
"os"
"github.com/andoma-go/logrus"
nested "github.com/andoma-go/logrus-formatter-nested"
)
func main() {
l := logrus.New()
l.SetOutput(os.Stdout)
l.SetLevel(logrus.DebugLevel)
l.SetFormatter(&nested.Formatter{
TrimMessages: true,
NoColors: true,
TimestampFormat: "-",
})
l.Debug(" test1 ")
l.Info("test2 ")
l.Warn(" test3")
l.Error(" test4 ")
}
Output: - [DEBU] test1 - [INFO] test2 - [WARN] test3 - [ERRO] test4
Click to show internal directories.
Click to hide internal directories.
