-
Notifications
You must be signed in to change notification settings - Fork 687
/
log.go
66 lines (58 loc) · 1.53 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
package main
import (
"bytes"
"fmt"
"os"
"sort"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/terminal"
"gopkg.in/natefinch/lumberjack.v2"
"github.com/datawire/ambassador/pkg/supervisor"
)
// DaemonFormatter formats log messages for the Edge Control Daemon
type DaemonFormatter struct {
TimestampFormat string
}
// Format implement logrus.Formatter
func (f *DaemonFormatter) Format(entry *logrus.Entry) ([]byte, error) {
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}
fmt.Fprintf(b, "%s %s", entry.Time.Format(f.TimestampFormat), entry.Message)
if len(entry.Data) > 0 {
keys := make([]string, 0, len(entry.Data))
for k := range entry.Data {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := entry.Data[k]
fmt.Fprintf(b, " %s=%+v", k, v)
}
}
b.WriteByte('\n')
return b.Bytes(), nil
}
// SetUpLogging sets up standard Edge Control Daemon logging
func SetUpLogging() supervisor.Logger {
loggingToTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
logger := logrus.StandardLogger()
formatter := new(DaemonFormatter)
logger.Formatter = formatter
if loggingToTerminal {
formatter.TimestampFormat = "15:04:05"
} else {
formatter.TimestampFormat = "2006/01/02 15:04:05"
logger.SetOutput(&lumberjack.Logger{
Filename: logfile,
MaxSize: 10, // megabytes
MaxBackups: 3, // in the same directory
MaxAge: 60, // days
LocalTime: true, // rotated logfiles use local time names
})
}
return logger
}