This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 657
/
showuserlog.go
80 lines (69 loc) · 2.08 KB
/
showuserlog.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
package log
import (
"fmt"
"log/syslog"
"os"
"github.com/Sirupsen/logrus"
logrus_syslog "github.com/Sirupsen/logrus/hooks/syslog"
)
// ShowuserlogHook stores all levels of logrus entries in memory until its told the RancherOS logging system is ready
// then it replays them to be logged
type ShowuserlogHook struct {
Level logrus.Level
syslogHook *logrus_syslog.SyslogHook
storedEntries []*logrus.Entry
appName string
}
// NewShowuserlogHook creates a new hook for use
func NewShowuserlogHook(l logrus.Level, app string) (*ShowuserlogHook, error) {
return &ShowuserlogHook{l, nil, []*logrus.Entry{}, app}, nil
}
// Fire is called by logrus when the Hook is active
func (hook *ShowuserlogHook) Fire(entry *logrus.Entry) error {
if entry.Level <= hook.Level {
//if f, err := os.OpenFile("/dev/kmsg", os.O_WRONLY, 0644); err != nil {
// fmt.Fprintf(f, "%s:%s: %s\n", hook.appName, entry.Level, entry.Message)
// f.Close()
//} else {
fmt.Printf("[ ] %s:%s: %s\n", hook.appName, entry.Level, entry.Message)
//}
}
if hook.syslogHook == nil {
hook.storedEntries = append(hook.storedEntries, entry)
} else {
err := hook.syslogHook.Fire(entry)
if err != nil {
fmt.Fprintf(os.Stderr, "LOGERR: Unable to syslog.Fire %v, %v", entry, err)
return err
}
}
return nil
}
// Levels returns all log levels, so we can process them ourselves
func (hook *ShowuserlogHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.DebugLevel,
logrus.InfoLevel,
logrus.WarnLevel,
logrus.ErrorLevel,
logrus.FatalLevel,
logrus.PanicLevel,
}
}
// NotUsedYetLogSystemReady Set up Syslog Hook, and replay any stored entries.
func (hook *ShowuserlogHook) NotUsedYetLogSystemReady() error {
if hook.syslogHook == nil {
h, err := logrus_syslog.NewSyslogHook("", "", syslog.LOG_INFO, "")
if err != nil {
logrus.Debugf("error creating SyslogHook: %v", err)
return err
}
hook.syslogHook = h
for _, entry := range hook.storedEntries {
line, _ := entry.String()
fmt.Printf("---- CATCHUP %s\n", line)
hook.syslogHook.Fire(entry)
}
}
return nil
}