This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
log.go
92 lines (84 loc) · 2.5 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
package ds
import (
"fmt"
"log"
"time"
logger "github.com/LF-Engineering/insights-datasource-shared/ingestjob"
)
var (
gLogger *logger.Logger
gLoggerConnector string
gLoggerStatus string
gLoggerConfiguration []map[string]string
gSync bool
gConsoleAfterES bool
gLogLoggerError bool
)
// AddLogger - adds logger
func AddLogger(logger *logger.Logger, connector, status string, configuration []map[string]string) {
if logger != nil {
gLogger = logger
gLoggerConnector = connector
gLoggerConfiguration = configuration
gLoggerStatus = status
}
}
// SetSyncMode - sets sync/async ES loging mode
// sync -> gSyncMode: true - wait for log message to be sent to ES before exiting (sync mode)
// sync -> gSyncMode: false - default, send log message to ES in goroutine and return immediately
// consoleAfterES -> gConsoleAfterES - will log on console after logged to ES
func SetSyncMode(sync, consoleAfterES bool) {
gSync = sync
gConsoleAfterES = consoleAfterES
}
// SetLogLoggerError - if logging to ES/console fails - try to log error
func SetLogLoggerError(logLoggerError bool) {
gLogLoggerError = logLoggerError
}
// Printf is a wrapper around Printf(...) that supports logging and removes redacted data.
func Printf(format string, args ...interface{}) {
// Actual logging to stdout & DB
now := time.Now()
msg := FilterRedacted(fmt.Sprintf("%s: "+format, append([]interface{}{ToYMDHMSDate(now)}, args...)...))
logConsole := func() {
_, err := fmt.Printf("%s", msg)
if err != nil && gLogLoggerError {
log.Printf("Error (log to console): %s", err.Error())
}
}
if !gConsoleAfterES || gLogger == nil {
logConsole()
}
if gLogger != nil {
logES := func() {
err := gLogger.Write(&logger.Log{
Connector: gLoggerConnector,
Configuration: gLoggerConfiguration,
Status: gLoggerStatus,
CreatedAt: time.Now(),
Message: msg,
})
if err != nil && gLogLoggerError {
log.Printf("Error (log to ES): %s", err.Error())
}
if gConsoleAfterES {
logConsole()
}
}
if gSync {
logES()
return
}
go logES()
}
}
// PrintfNoRedacted is a wrapper around Printf(...) that supports logging and don't removes redacted data
func PrintfNoRedacted(format string, args ...interface{}) {
// Actual logging to stdout & DB
now := time.Now()
msg := fmt.Sprintf("%s: "+format, append([]interface{}{ToYMDHMSDate(now)}, args...)...)
_, err := fmt.Printf("%s", msg)
if err != nil {
log.Printf("Err: %s", err.Error())
}
}