forked from Z-Bolt/OctoScreen
/
logger.go
85 lines (69 loc) · 1.6 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
package ui
import (
"os"
"path"
"runtime"
"strings"
"time"
"github.com/sirupsen/logrus"
)
type ContextHook struct{}
func (hook ContextHook) Levels() []logrus.Level {
return logrus.AllLevels
}
func (hook ContextHook) Fire(entry *logrus.Entry) error {
pc := make([]uintptr, 3, 3)
cnt := runtime.Callers(6, pc)
for i := 0; i < cnt; i++ {
fu := runtime.FuncForPC(pc[i] - 1)
name := fu.Name()
if !strings.Contains(name, "github.com/sirupsen/logrus") {
file, line := fu.FileLine(pc[i] - 1)
entry.Data["file"] = path.Base(file)
entry.Data["func"] = path.Base(name)
entry.Data["line"] = line
break
}
}
return nil
}
type NotificationsHook struct {
n *Notifications
}
func NewNotificationsHook(n *Notifications) *NotificationsHook {
return &NotificationsHook{n: n}
}
func (h NotificationsHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
}
}
func (h NotificationsHook) Fire(entry *logrus.Entry) error {
d := 10 * time.Second
if entry.Level == logrus.WarnLevel {
d = time.Second
}
h.n.Show(entry.Level.String(), entry.Message, d)
return nil
}
var Logger *logrus.Entry
func init() {
var LogFile = os.Getenv("OCTOSCREEN_LOG_FILE")
var log = logrus.New()
log.AddHook(ContextHook{})
log.SetLevel(logrus.DebugLevel)
if LogFile == "" {
log.Out = os.Stdout
} else {
file, err := os.OpenFile(LogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err == nil {
log.Out = file
} else {
log.Info("Failed to log to file, using default stderr")
}
}
Logger = log.WithFields(logrus.Fields{})
}