forked from glitchdotcom/logging
-
Notifications
You must be signed in to change notification settings - Fork 4
/
formatter.go
88 lines (73 loc) · 2.79 KB
/
formatter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package logging
import (
"fmt"
"strings"
"time"
)
// LogFormat is the name of a known formatting function.
type LogFormat string
// MINIMAL describes a formatter that just prints the message, replays are not indicated.
const MINIMAL LogFormat = "minimal"
// MINIMALTAGGED describes a formatter that just prints the level, tags and message, replays are not indicated.
const MINIMALTAGGED LogFormat = "minimaltagged"
// SIMPLE describes a formatter that just prints the date, level and message, replays are not indicated.
const SIMPLE LogFormat = "simple"
// FULL formats messages with the date to ms accuracy, the level, tags and message. Replayed messages have a special field added.
const FULL LogFormat = "full"
// FormatFromString converts a string name to a LogFormat. Valid
// arguemnts include full, simple, minimaltagged and minimal. An
// unknown string will be treated like simple.
func FormatFromString(formatName string) LogFormat {
formatName = strings.ToLower(formatName)
switch formatName {
case "full":
return FULL
case "simple":
return SIMPLE
case "minimaltagged":
return MINIMALTAGGED
case "minimal":
return MINIMAL
default:
return SIMPLE
}
}
// GetFormatter returns the function associated with a named format.
func GetFormatter(formatName LogFormat) LogFormatter {
switch formatName {
case FULL:
return fullFormat
case SIMPLE:
return simpleFormat
case MINIMALTAGGED:
return minimalWithTagsFormat
case MINIMAL:
return minimalFormat
default:
return simpleFormat
}
}
// LogFormatter is a function type used to convert a log record into a string.
// Original time is provided times when the formatter has to construct a replayed message from the buffer.
type LogFormatter func(level LogLevel, tags []string, message string, t time.Time, original time.Time) string
var fullFormat = func(level LogLevel, tags []string, message string, t time.Time, original time.Time) string {
if original != t {
message = fmt.Sprintf("[replayed from %v] %v", original.Format(time.StampMilli), message)
}
if tags != nil && len(tags) > 0 {
return fmt.Sprintf("[%v] [%v] %v %v", t.Format(time.StampMilli), level, tags, message)
}
return fmt.Sprintf("[%v] [%v] %v", t.Format(time.StampMilli), level, message)
}
var simpleFormat = func(level LogLevel, tags []string, message string, t time.Time, original time.Time) string {
return fmt.Sprintf("[%v] [%v] %v", t.Format(time.Stamp), level, message)
}
var minimalFormat = func(level LogLevel, tags []string, message string, t time.Time, original time.Time) string {
return message
}
var minimalWithTagsFormat = func(level LogLevel, tags []string, message string, t time.Time, original time.Time) string {
if tags != nil && len(tags) > 0 {
return fmt.Sprintf("[%v] %v %v", level, tags, message)
}
return fmt.Sprintf("[%v] %v", level, message)
}