/
log.go
149 lines (119 loc) · 3.92 KB
/
log.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//Package log implements github.com/sirupsen/logrus to provide logging functionality.
package log
import (
"io/ioutil"
"strings"
"github.com/wcsanders1/MockApiHub/config"
"github.com/sirupsen/logrus"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
)
const (
defaultFilename = "default.log"
defaultMaxFileSize = 20
defaultMaxFileBackups = 20
defaultMaxFileDaysAge = 20
pkgField = "pkg"
// FuncField is the name of the log field denoting the name of the function doing the logging.
FuncField = "func"
// PortField is the name of the log field denoting a server port number.
PortField = "port"
// MethodField is the name of the log field denoting the HTTP method name.
MethodField = "method"
// PathField is the name of the log field denoting an HTTP path.
PathField = "path"
// UseTLSField is the name of the log field denoting whether a server is configured to use TLS.
UseTLSField = "useTLS"
// CertFileField is the name of the log field denoting the configured path to a server's TLS certificate.
CertFileField = "certFile"
// KeyFileField is the name of the log field denoting the configured path to a server's TLS key.
KeyFileField = "keyFile"
// DefaultCertFileField is the name of the log field denoting the default cert file.
DefaultCertFileField = "defaultCertFile"
// DefaultKeyFileField is the name of the log field denoting the default key file.
DefaultKeyFileField = "defaultKeyFile"
// BaseURLField is the name of the log field denoting the base URL of an API.
BaseURLField = "baseURL"
// RouteField is the name of the log field denoting a registered route in an API.
RouteField = "route"
// FileField is the name of the log field denoting a file that a mock API route will serve.
FileField = "file"
// APIDirField is the name of the log field denoting the directory a mock API is in.
APIDirField = "apiDir"
// APINameField is the name of the log field denoting the name of a mock API.
APINameField = "mockAPIName"
// EndpointNameField is the name of the log field denoting the name of an endpoint.
EndpointNameField = "endpointName"
// ResponseHeadersField is the name of the log field denoting the response headers.
ResponseHeadersField = "responseHeaders"
)
// NewLogger returns a new instance of a logger.
func NewLogger(config *config.Log, pkgName string) *logrus.Entry {
log := logrus.New()
if !config.LoggingEnabled {
log.Out = ioutil.Discard
return log.WithField("", "")
}
if config.FormatAsJSON {
log.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: "2006-01-02 15:04:05",
PrettyPrint: config.PrettyJSON,
})
} else {
log.SetFormatter(&logrus.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
})
}
rotate := &lumberjack.Logger{
Filename: getLogFilename(config.Filename),
MaxSize: getMaxFileSize(config.MaxFileSize),
MaxBackups: getMaxFileBackups(config.MaxFileBackups),
MaxAge: getMaxFileDaysAge(config.MaxFileDaysAge),
}
log.SetOutput(rotate)
log.SetLevel(getLogLevel(config.Level))
return log.WithField(pkgField, pkgName)
}
func getLogFilename(filename string) string {
if len(filename) == 0 {
return defaultFilename
}
return filename
}
func getMaxFileSize(maxSize int) int {
if maxSize < 1 {
return defaultMaxFileSize
}
return maxSize
}
func getMaxFileBackups(maxFileBackups int) int {
if maxFileBackups < 1 {
return defaultMaxFileBackups
}
return maxFileBackups
}
func getMaxFileDaysAge(maxFileDaysAge int) int {
if maxFileDaysAge < 1 {
return defaultMaxFileDaysAge
}
return maxFileDaysAge
}
func getLogLevel(level string) logrus.Level {
lowerCaseLevel := strings.ToLower(level)
switch lowerCaseLevel {
case "debug":
return logrus.DebugLevel
case "info":
return logrus.InfoLevel
case "warn":
return logrus.WarnLevel
case "error":
return logrus.ErrorLevel
case "fatal":
return logrus.FatalLevel
case "panic":
return logrus.PanicLevel
default:
return logrus.DebugLevel
}
}