2016-11-23 10:49:35 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-07-12 04:18:02 +00:00
|
|
|
"log/syslog"
|
2016-11-23 10:49:35 +00:00
|
|
|
"os"
|
2017-07-12 04:18:02 +00:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
logrus_syslog "github.com/Sirupsen/logrus/hooks/syslog"
|
2016-11-23 10:49:35 +00:00
|
|
|
)
|
|
|
|
|
2017-07-12 04:18:02 +00:00
|
|
|
// 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
|
2016-11-23 10:49:35 +00:00
|
|
|
type ShowuserlogHook struct {
|
2017-07-12 04:18:02 +00:00
|
|
|
Level logrus.Level
|
|
|
|
syslogHook *logrus_syslog.SyslogHook
|
|
|
|
storedEntries []*logrus.Entry
|
2016-11-23 10:49:35 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 04:18:02 +00:00
|
|
|
// NewShowuserlogHook creates a new hook for use
|
2016-11-23 10:49:35 +00:00
|
|
|
func NewShowuserlogHook(l logrus.Level) (*ShowuserlogHook, error) {
|
2017-07-12 04:18:02 +00:00
|
|
|
return &ShowuserlogHook{l, nil, []*logrus.Entry{}}, nil
|
2016-11-23 10:49:35 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 04:18:02 +00:00
|
|
|
// Fire is called by logrus when the Hook is active
|
2016-11-23 10:49:35 +00:00
|
|
|
func (hook *ShowuserlogHook) Fire(entry *logrus.Entry) error {
|
|
|
|
line, err := entry.String()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Unable to read entry, %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if entry.Level <= hook.Level {
|
2017-07-12 04:18:02 +00:00
|
|
|
fmt.Printf("SVEN %s", line)
|
2016-11-23 10:49:35 +00:00
|
|
|
}
|
2017-07-12 04:18:02 +00:00
|
|
|
|
|
|
|
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 write %s, %v", line, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 10:49:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-12 04:18:02 +00:00
|
|
|
// Levels returns all log levels, so we can process them ourselves
|
2016-11-23 10:49:35 +00:00
|
|
|
func (hook *ShowuserlogHook) Levels() []logrus.Level {
|
|
|
|
return []logrus.Level{
|
|
|
|
logrus.DebugLevel,
|
|
|
|
logrus.InfoLevel,
|
|
|
|
logrus.WarnLevel,
|
|
|
|
logrus.ErrorLevel,
|
|
|
|
logrus.FatalLevel,
|
|
|
|
logrus.PanicLevel,
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 04:18:02 +00:00
|
|
|
|
|
|
|
// Set up Syslog Hook, and replay any stored entries.
|
|
|
|
func (hook *ShowuserlogHook) LogSystemReady() error {
|
|
|
|
if hook.syslogHook == nil {
|
|
|
|
h, err := logrus_syslog.NewSyslogHook("", "", syslog.LOG_INFO, "")
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("error creating SyslogHook: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hook.syslogHook = h
|
|
|
|
|
|
|
|
for _, entry := range hook.storedEntries {
|
2017-07-12 05:17:03 +00:00
|
|
|
line, _ := entry.String()
|
|
|
|
fmt.Printf("---- CATCHUP %s\n", line)
|
2017-07-12 04:18:02 +00:00
|
|
|
hook.syslogHook.Fire(entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|