-
Notifications
You must be signed in to change notification settings - Fork 115
/
delayed_audit_logger_unix.go
95 lines (77 loc) · 2.38 KB
/
delayed_audit_logger_unix.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
// +build !windows
package platform
import (
"fmt"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshretry "github.com/cloudfoundry/bosh-utils/retrystrategy"
"log"
"time"
)
type DelayedAuditLogger struct {
auditLoggerProvider AuditLoggerProvider
debugAuditLogger *log.Logger
errAuditLogger *log.Logger
debugLogCh, errorLogCh chan string
logger boshlog.Logger
}
const delayedAuditLoggerTag = "DelayedAuditLogger"
func NewDelayedAuditLogger(auditLoggerProvider AuditLoggerProvider, logger boshlog.Logger) *DelayedAuditLogger {
return &DelayedAuditLogger{
auditLoggerProvider: auditLoggerProvider,
debugLogCh: make(chan string, 1000),
errorLogCh: make(chan string, 1000),
logger: logger,
}
}
func (l *DelayedAuditLogger) StartLogging() {
go func() {
retryable := boshretry.NewRetryable(func() (bool, error) {
debugAuditLogger, err := l.auditLoggerProvider.ProvideDebugLogger()
if err != nil {
l.logger.Error(delayedAuditLoggerTag, err.Error())
return true, err
}
errAuditLogger, err := l.auditLoggerProvider.ProvideErrorLogger()
if err != nil {
l.logger.Error(delayedAuditLoggerTag, err.Error())
return true, err
}
l.debugAuditLogger = debugAuditLogger
l.errAuditLogger = errAuditLogger
return false, nil
})
unlimitedRetryStrategy := boshretry.NewUnlimitedRetryStrategy(100*time.Millisecond, retryable, l.logger)
err := unlimitedRetryStrategy.Try()
if err != nil {
l.logger.Error(delayedAuditLoggerTag, err.Error())
return
}
l.logger.Debug(delayedAuditLoggerTag, "Starting logging to syslog...")
go func() {
for debugLog := range l.debugLogCh {
l.debugAuditLogger.Print(debugLog)
}
}()
go func() {
for errorLog := range l.errorLogCh {
l.errAuditLogger.Print(errorLog)
}
}()
}()
}
func (l *DelayedAuditLogger) Debug(msg string) {
l.logger.Debug(delayedAuditLoggerTag, fmt.Sprintf("Logging %s to syslog", msg))
select {
case l.debugLogCh <- msg:
default:
l.logger.Debug(delayedAuditLoggerTag, fmt.Sprintf("Debug message '%s' not sent to syslog", msg))
}
}
func (l *DelayedAuditLogger) Err(msg string) {
l.logger.Debug(delayedAuditLoggerTag, fmt.Sprintf("Logging %s to syslog", msg))
select {
case l.errorLogCh <- msg:
default:
l.logger.Debug(delayedAuditLoggerTag, fmt.Sprintf("Error message '%s' not sent to syslog", msg))
}
}