mirror of
https://github.com/rancher/os.git
synced 2025-09-02 07:15:41 +00:00
make ros log to dmesg
Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>
This commit is contained in:
143
log/log.go
Normal file
143
log/log.go
Normal file
@@ -0,0 +1,143 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Default to using the logrus standard logger until log.InitLogger(logLevel) is called
|
||||
var appLog = logrus.StandardLogger()
|
||||
var userHook *ShowuserlogHook
|
||||
|
||||
type Fields logrus.Fields
|
||||
type Level logrus.Level
|
||||
type Logger logrus.Logger
|
||||
|
||||
const (
|
||||
// PanicLevel level, highest level of severity. Logs and then calls panic with the
|
||||
// message passed to Debug, Info, ...
|
||||
PanicLevel Level = iota
|
||||
// FatalLevel level. Logs and then calls `os.Exit(1)`. It will exit even if the
|
||||
// logging level is set to Panic.
|
||||
FatalLevel
|
||||
// ErrorLevel level. Logs. Used for errors that should definitely be noted.
|
||||
// Commonly used for hooks to send errors to an error tracking service.
|
||||
ErrorLevel
|
||||
// WarnLevel level. Non-critical entries that deserve eyes.
|
||||
WarnLevel
|
||||
// InfoLevel level. General operational entries about what's going on inside the
|
||||
// application.
|
||||
InfoLevel
|
||||
// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
|
||||
DebugLevel
|
||||
)
|
||||
|
||||
func SetOutput(out io.Writer) {
|
||||
appLog.Out = out
|
||||
}
|
||||
func SetLevel(level Level) {
|
||||
if userHook != nil {
|
||||
userHook.Level = logrus.Level(level)
|
||||
} else {
|
||||
appLog.Level = logrus.Level(level)
|
||||
logrus.SetLevel(logrus.Level(level))
|
||||
}
|
||||
}
|
||||
|
||||
func Debugf(format string, args ...interface{}) {
|
||||
appLog.Debugf(format, args...)
|
||||
}
|
||||
func Infof(format string, args ...interface{}) {
|
||||
appLog.Infof(format, args...)
|
||||
}
|
||||
func Printf(format string, args ...interface{}) {
|
||||
appLog.Printf(format, args...)
|
||||
}
|
||||
func Warnf(format string, args ...interface{}) {
|
||||
appLog.Warnf(format, args...)
|
||||
}
|
||||
func Warningf(format string, args ...interface{}) {
|
||||
appLog.Warningf(format, args...)
|
||||
}
|
||||
func Errorf(format string, args ...interface{}) {
|
||||
appLog.Errorf(format, args...)
|
||||
}
|
||||
func Fatalf(format string, args ...interface{}) {
|
||||
appLog.Fatalf(format, args...)
|
||||
}
|
||||
func Panicf(format string, args ...interface{}) {
|
||||
appLog.Panicf(format, args...)
|
||||
}
|
||||
|
||||
func Debug(args ...interface{}) {
|
||||
appLog.Debug(args...)
|
||||
}
|
||||
func Info(args ...interface{}) {
|
||||
appLog.Info(args...)
|
||||
}
|
||||
func Print(args ...interface{}) {
|
||||
appLog.Print(args...)
|
||||
}
|
||||
func Warn(args ...interface{}) {
|
||||
appLog.Warn(args...)
|
||||
}
|
||||
func Warning(args ...interface{}) {
|
||||
appLog.Warning(args...)
|
||||
}
|
||||
func Error(args ...interface{}) {
|
||||
appLog.Error(args...)
|
||||
}
|
||||
func Fatal(args ...interface{}) {
|
||||
appLog.Fatal(args...)
|
||||
}
|
||||
func Panic(args ...interface{}) {
|
||||
appLog.Panic(args...)
|
||||
}
|
||||
|
||||
func WithField(key string, value interface{}) *logrus.Entry {
|
||||
return appLog.WithField(key, value)
|
||||
}
|
||||
func WithFields(fields Fields) *logrus.Entry {
|
||||
return appLog.WithFields(logrus.Fields(fields))
|
||||
}
|
||||
|
||||
func InitLogger() {
|
||||
if userHook != nil {
|
||||
return // we've already initialised it
|
||||
}
|
||||
thisLog := logrus.New()
|
||||
|
||||
filename := "/dev/kmsg"
|
||||
f, err := os.OpenFile(filename, os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
logrus.Debugf("error opening /dev/kmsg %s", err)
|
||||
}
|
||||
|
||||
// Filter what the user sees (info level, unless they set --debug)
|
||||
stdLogger := logrus.StandardLogger()
|
||||
showuserHook, err := NewShowuserlogHook(stdLogger.Level)
|
||||
if err != nil {
|
||||
f.Close()
|
||||
logrus.Errorf("hook failure %s", err)
|
||||
}
|
||||
|
||||
// We're all set up, now we can make it global
|
||||
appLog = thisLog
|
||||
userHook = showuserHook
|
||||
|
||||
thisLog.Hooks.Add(showuserHook)
|
||||
logrus.StandardLogger().Hooks.Add(showuserHook)
|
||||
thisLog.Out = f
|
||||
logrus.SetOutput(f)
|
||||
thisLog.Level = logrus.DebugLevel
|
||||
|
||||
pwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
thisLog.Error(err)
|
||||
}
|
||||
|
||||
thisLog.Debugf("START: %v in %s", os.Args, pwd)
|
||||
|
||||
}
|
40
log/showuserlog.go
Normal file
40
log/showuserlog.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Sirupsen/logrus"
|
||||
"os"
|
||||
)
|
||||
|
||||
// ShowuserlogHook writes all levels of logrus entries to a file for later analysis
|
||||
type ShowuserlogHook struct {
|
||||
Level logrus.Level
|
||||
}
|
||||
|
||||
func NewShowuserlogHook(l logrus.Level) (*ShowuserlogHook, error) {
|
||||
return &ShowuserlogHook{l}, nil
|
||||
}
|
||||
|
||||
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 {
|
||||
fmt.Printf("> %s", line)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (hook *ShowuserlogHook) Levels() []logrus.Level {
|
||||
return []logrus.Level{
|
||||
logrus.DebugLevel,
|
||||
logrus.InfoLevel,
|
||||
logrus.WarnLevel,
|
||||
logrus.ErrorLevel,
|
||||
logrus.FatalLevel,
|
||||
logrus.PanicLevel,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user