123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package logutil
- import (
- "fmt"
- "strings"
- "devt.de/krotik/common/testutil"
- "devt.de/krotik/common/timeutil"
- )
- type Formatter interface {
-
- Format(level Level, scope string, msg ...interface{}) string
- }
- func ConsoleFormatter() Formatter {
- return &consoleFormatter{}
- }
- type consoleFormatter struct {
- }
- func (sf *consoleFormatter) Format(level Level, scope string, msg ...interface{}) string {
- return fmt.Sprintln(fmt.Sprintf("%v:", level), fmt.Sprint(msg...))
- }
- func SimpleFormatter() Formatter {
- return &simpleFormatter{timeutil.MakeTimestamp}
- }
- type simpleFormatter struct {
- tsFunc func() string // Timestamp function
- }
- func (sf *simpleFormatter) Format(level Level, scope string, msg ...interface{}) string {
- if scope == "" {
- return fmt.Sprintln(sf.tsFunc(), level, fmt.Sprint(msg...))
- }
- return fmt.Sprintln(sf.tsFunc(), level, scope, fmt.Sprint(msg...))
- }
- func TemplateFormatter(template string) Formatter {
- return &templateFormatter{template, timeutil.MakeTimestamp}
- }
- type templateFormatter struct {
- template string
- tsFunc func() string // Timestamp function
- }
- func (sf *templateFormatter) Format(level Level, scope string, msg ...interface{}) string {
- name, loc := testutil.GetCaller(2)
- out := sf.template
- out = strings.Replace(out, "%s", scope, -1)
- out = strings.Replace(out, "%l", fmt.Sprint(level), -1)
- out = strings.Replace(out, "%t", sf.tsFunc(), -1)
- out = strings.Replace(out, "%f", name, -1)
- out = strings.Replace(out, "%c", loc, -1)
- out = strings.Replace(out, "%m", fmt.Sprint(msg...), -1)
- return fmt.Sprintln(out)
- }
|