-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
280 lines (254 loc) · 7.25 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright 2021 The CDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package log
// TODO: Panic and Fatal logging methods need to release the gdk display
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
"strings"
log "github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
"github.com/go-curses/cdk/env"
cstrings "github.com/go-curses/cdk/lib/strings"
)
const (
LevelError string = "error"
LevelWarn string = "warn"
LevelInfo string = "info"
LevelDebug string = "debug"
LevelTrace string = "trace"
)
var LogLevels = []string{
LevelError,
LevelWarn,
LevelInfo,
LevelDebug,
LevelTrace,
}
const (
FormatPretty string = "pretty"
FormatText string = "text"
FormatJson string = "json"
)
const (
OutputStderr string = "stderr"
OutputStdout string = "stdout"
OutputFile string = "file"
)
var (
logger = log.New()
logFileHandle *os.File
logFullPaths = false
logBuffer = bytes.NewBufferString("")
DefaultLogPath = os.TempDir() + string(os.PathSeparator) + "cdk.log"
)
const (
StandardTimestampFormat = "2006-01-02T15:04:05.000"
DefaultTimestampFormat = "20060102-150405.00"
)
func GetExitFunc() func(int) {
if logger == nil {
return func(int) {}
}
return logger.ExitFunc
}
func SetExitFunc(fn func(int)) {
logger.ExitFunc = fn
}
func StartRestart() error {
disableTimestamp := true
if v := env.Get("GO_CDK_LOG_TIMESTAMPS", "false"); v == "true" {
disableTimestamp = false
}
timestampFormat := DefaultTimestampFormat
if v := env.Get("GO_CDK_LOG_TIMESTAMP_FORMAT", ""); v != "" {
if v == "standard" {
timestampFormat = StandardTimestampFormat
} else if v == "default" {
timestampFormat = DefaultTimestampFormat
} else {
timestampFormat = v
}
}
switch env.Get("GO_CDK_LOG_FULL_PATHS", "false") {
case "true":
logFullPaths = true
default:
logFullPaths = false
}
switch env.Get("GO_CDK_LOG_FORMAT", "pretty") {
case FormatJson:
logger.SetFormatter(&log.JSONFormatter{
TimestampFormat: timestampFormat,
DisableTimestamp: disableTimestamp,
})
case FormatText:
logger.SetFormatter(&log.TextFormatter{
TimestampFormat: timestampFormat,
DisableTimestamp: disableTimestamp,
DisableSorting: true,
DisableColors: true,
FullTimestamp: true,
})
case FormatPretty:
fallthrough
default:
logger.SetFormatter(&prefixed.TextFormatter{
DisableTimestamp: disableTimestamp,
TimestampFormat: timestampFormat,
ForceFormatting: true,
FullTimestamp: true,
DisableSorting: true,
DisableColors: true,
})
}
switch env.Get("GO_CDK_LOG_LEVEL", LevelError) {
case LevelTrace:
logger.SetLevel(log.TraceLevel)
case LevelDebug:
logger.SetLevel(log.DebugLevel)
case LevelInfo:
logger.SetLevel(log.InfoLevel)
case LevelWarn:
logger.SetLevel(log.WarnLevel)
case LevelError:
fallthrough
default:
logger.SetLevel(log.ErrorLevel)
}
switch env.Get("GO_CDK_LOG_OUTPUT", OutputFile) {
case OutputStdout:
logger.SetOutput(os.Stdout)
case OutputStderr:
logger.SetOutput(os.Stderr)
case OutputFile:
fallthrough
default:
_ = Stop()
if logfile := env.Get("GO_CDK_LOG_FILE", DefaultLogPath); !cstrings.IsEmpty(logfile) && logfile != "/dev/null" {
logFH, err := os.OpenFile(logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return err
}
logFileHandle = logFH
_, _ = logFileHandle.WriteString(logBuffer.String())
logBuffer.Reset()
logger.SetOutput(logFileHandle)
} else {
logger.SetOutput(ioutil.Discard)
}
}
return nil
}
func Stop() error {
if logFileHandle != nil {
logBuffer.Reset()
logger.SetOutput(logBuffer)
if err := logFileHandle.Close(); err != nil {
return err
}
logFileHandle = nil
}
return nil
}
func GetLevel() string {
switch logger.GetLevel() {
case log.TraceLevel:
return LevelTrace
case log.DebugLevel:
return LevelDebug
case log.WarnLevel:
return LevelWarn
case log.InfoLevel:
return LevelInfo
default:
return LevelError
}
}
func getLogPrefix(depth int) string {
depth += 1
if function, file, line, ok := runtime.Caller(depth); ok {
fullName := runtime.FuncForPC(function).Name()
funcName := fullName
if i := strings.LastIndex(fullName, "."); i > -1 {
funcName = fullName[i+1:]
}
funcName = cstrings.PadLeft(funcName, " ", 12)
packName := fullName
if i := strings.Index(fullName, "."); i > -1 {
packName = fullName[:i+1]
}
filepath := file
if !logFullPaths {
if packName == "main." {
filepath = path.Base(filepath)
} else {
if i := strings.Index(filepath, packName); i > -1 {
filepath = file[i:]
}
}
}
return fmt.Sprintf("%s:%d %s", filepath, line, funcName)
}
return "(missing caller metadata)"
}
func TraceF(format string, argv ...interface{}) { TraceDF(1, format, argv...) }
func TraceDF(depth int, format string, argv ...interface{}) {
logger.Tracef(cstrings.NLSprintf("%s %s", getLogPrefix(depth+1), format), argv...)
}
func DebugF(format string, argv ...interface{}) { DebugDF(1, format, argv...) }
func DebugDF(depth int, format string, argv ...interface{}) {
logger.Debugf(cstrings.NLSprintf("%s %s", getLogPrefix(depth+1), format), argv...)
}
func InfoF(format string, argv ...interface{}) { InfoDF(1, format, argv...) }
func InfoDF(depth int, format string, argv ...interface{}) {
logger.Infof(cstrings.NLSprintf("%s %s", getLogPrefix(depth+1), format), argv...)
}
func WarnF(format string, argv ...interface{}) { WarnDF(1, format, argv...) }
func WarnDF(depth int, format string, argv ...interface{}) {
logger.Warnf(cstrings.NLSprintf("%s %s", getLogPrefix(depth+1), format), argv...)
}
func Error(err error) { ErrorDF(1, err.Error()) }
func ErrorF(format string, argv ...interface{}) { ErrorDF(1, format, argv...) }
func ErrorDF(depth int, format string, argv ...interface{}) {
logger.Errorf(cstrings.NLSprintf("%s %s", getLogPrefix(depth+1), format), argv...)
}
func Fatal(err error) { FatalDF(1, err.Error()) }
func FatalF(format string, argv ...interface{}) { FatalDF(1, format, argv...) }
func FatalDF(depth int, format string, argv ...interface{}) {
// if dm := GetDisplay(); dm != nil {
// dm.ReleaseDisplay()
// }
message := fmt.Sprintf(cstrings.NLSprintf("%s\t%s", getLogPrefix(depth+1), format), argv...)
logger.Fatalf(message)
}
func Panic(err error) { PanicDF(1, err.Error()) }
func PanicF(format string, argv ...interface{}) { PanicDF(1, format, argv...) }
func PanicDF(depth int, format string, argv ...interface{}) {
// if dm := GetDisplay(); dm != nil {
// dm.ReleaseDisplay()
// }
message := fmt.Sprintf(cstrings.NLSprintf("%s\t%s", getLogPrefix(depth+1), format), argv...)
logger.Errorf(message)
_ = Stop()
panic(message)
}
func Exit(code int) {
InfoDF(1, "exiting with code: %d", code)
logger.Exit(code)
}