-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
125 lines (105 loc) · 2.67 KB
/
main.go
File metadata and controls
125 lines (105 loc) · 2.67 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package dlog
import (
"github.com/sirupsen/logrus"
"os"
)
type hook struct{}
func (h *hook) Levels() []logrus.Level {
return logrus.AllLevels
}
func (h *hook) Fire(e *logrus.Entry) error {
for k, v := range e.Data {
if s, ok := v.(*string); ok {
if *s == "" {
delete(e.Data, k)
continue
}
}
}
return nil
}
func init() {
f := &logrus.JSONFormatter{
FieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "timestamp",
logrus.FieldKeyLevel: "level",
logrus.FieldKeyMsg: "message",
},
}
logrus.SetFormatter(f)
logrus.SetOutput(os.Stdout)
logrus.AddHook(&hook{})
}
const (
// CorrelationID used to correlate logs
CorrelationID = "correlationid"
// AppName visible as a service
AppName = "appname"
// Parent segment trace id
Parent = "parent"
// Trace current trace id
Trace = "trace"
// Span id
Span = "span"
// Version of the application
Version = "version"
// Commit Short SHA
Commit = "commit"
// Build info
Build = "build"
// Func is the name of the function
Func = "func"
// File is the file name with line number
File = "file"
)
// Config parameters for creating a logger
type Config struct {
CorrelationID string // correlation id
AppName string // name of the application, e.g. diatom
Parent string // parent id
Trace string // trace id
Span string // span id
Version string // version of the application, e.g. 0.1.0
Commit string // short SHA
Build string // build number, e.g. 123
Level string // debug, info, warn, error, fatal, panic
ReportCaller bool // default is false
Formatter string // json or text, default is json
}
// NewLogger creates logger based on the config
func NewLogger(c *Config) *logrus.Entry {
if c == nil {
return logrus.NewEntry(logrus.StandardLogger())
}
logrus.SetReportCaller(c.ReportCaller)
if c.Level != "" {
parseLevel, err := logrus.ParseLevel(c.Level)
if err == nil {
logrus.SetLevel(parseLevel)
}
}
if c.Formatter == "text" {
// this should be used for local development
logrus.SetFormatter(&logrus.TextFormatter{
DisableColors: false,
DisableLevelTruncation: true,
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05.000Z",
PadLevelText: true,
ForceColors: true,
})
logrus.SetOutput(os.Stdout)
return logrus.NewEntry(logrus.StandardLogger())
}
fields := logrus.WithFields(logrus.Fields{
CorrelationID: &c.CorrelationID,
AppName: &c.AppName,
Parent: &c.Parent,
Trace: &c.Trace,
Span: &c.Span,
Version: &c.Version,
Commit: &c.Commit,
Build: &c.Build,
})
return fields
}