-
Notifications
You must be signed in to change notification settings - Fork 24
/
logger.go
96 lines (79 loc) · 2.28 KB
/
logger.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
/*
Copyright (c) 2016, Percona LLC and/or its affiliates. All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package pct
import (
"fmt"
"time"
"github.com/percona/pmm/proto"
)
type Logger struct {
logChan chan proto.LogEntry
service string
cmd *proto.Cmd
}
func NewLogger(logChan chan proto.LogEntry, service string) *Logger {
l := &Logger{
logChan: logChan,
service: service,
}
return l
}
func (l *Logger) Service() string {
return l.service
}
func (l *Logger) LogChan() chan proto.LogEntry {
return l.logChan
}
func (l *Logger) Debug(entry ...interface{}) {
l.log(false, proto.LOG_DEBUG, entry)
}
func (l *Logger) DebugOffline(entry ...interface{}) {
// Log entry is not sent to log API, only log file if enabled.
l.log(true, proto.LOG_DEBUG, entry)
}
func (l *Logger) Info(entry ...interface{}) {
l.log(false, proto.LOG_INFO, entry)
}
func (l *Logger) Warn(entry ...interface{}) {
l.log(false, proto.LOG_WARNING, entry)
}
func (l *Logger) Error(entry ...interface{}) {
l.log(false, proto.LOG_ERROR, entry)
}
func (l *Logger) Fatal(entry ...interface{}) {
l.log(false, proto.LOG_CRITICAL, entry)
}
func (l *Logger) log(offline bool, level byte, entry []interface{}) {
fullMsg := ""
for i, str := range entry {
if i > 0 {
fullMsg += " "
}
fullMsg += fmt.Sprintf("%v", str)
}
logEntry := proto.LogEntry{
Ts: time.Now().UTC(),
Level: level,
Service: l.service,
Msg: fullMsg,
Offline: offline,
}
select {
case l.logChan <- logEntry:
default:
// todo: lot.Println()?
// This happens when LogRelay.LogChan() is full, which means the log relay
// is receiving log entries faster than it can buffer and send them.
}
}