|
| 1 | +// Package logging manages setup of common logging interfaces and settings. |
| 2 | +package logging |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/replicatedhq/helmvm/pkg/defaults" |
| 12 | + "github.com/sirupsen/logrus" |
| 13 | +) |
| 14 | + |
| 15 | +type LogrusFileHook struct { |
| 16 | + file *os.File |
| 17 | + flag int |
| 18 | + chmod os.FileMode |
| 19 | + formatter *logrus.TextFormatter |
| 20 | +} |
| 21 | + |
| 22 | +var Debug bool |
| 23 | + |
| 24 | +func NewLogrusFileHook(file string, flag int, chmod os.FileMode) (*LogrusFileHook, error) { |
| 25 | + plainFormatter := &logrus.TextFormatter{DisableColors: true} |
| 26 | + logFile, err := os.OpenFile(file, flag, chmod) |
| 27 | + if err != nil { |
| 28 | + fmt.Fprintf(os.Stderr, "unable to write file on filehook %v", err) |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + |
| 32 | + return &LogrusFileHook{logFile, flag, chmod, plainFormatter}, err |
| 33 | +} |
| 34 | + |
| 35 | +func (hook *LogrusFileHook) Fire(entry *logrus.Entry) error { |
| 36 | + |
| 37 | + plainformat, err := hook.formatter.Format(entry) |
| 38 | + if err != nil { |
| 39 | + fmt.Fprintf(os.Stderr, "unable to parse line %v", err) |
| 40 | + return err |
| 41 | + } |
| 42 | + line := string(plainformat) |
| 43 | + _, err = hook.file.WriteString(line) |
| 44 | + if err != nil { |
| 45 | + fmt.Fprintf(os.Stderr, "unable to write file on filehook(entry.String) %v", err) |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func (hook *LogrusFileHook) Levels() []logrus.Level { |
| 53 | + return logrus.AllLevels |
| 54 | +} |
| 55 | + |
| 56 | +type StandardHook struct { |
| 57 | + Writer io.Writer |
| 58 | + LogLevels []logrus.Level |
| 59 | + Formatter *logrus.TextFormatter |
| 60 | +} |
| 61 | + |
| 62 | +func (hook *StandardHook) Fire(entry *logrus.Entry) error { |
| 63 | + text, err := hook.Formatter.Format(entry) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + if !Debug && entry.Level == logrus.InfoLevel || entry.Level == logrus.DebugLevel { |
| 69 | + return nil |
| 70 | + } |
| 71 | + |
| 72 | + _, err = hook.Writer.Write(text) |
| 73 | + return err |
| 74 | +} |
| 75 | + |
| 76 | +func (hook *StandardHook) Levels() []logrus.Level { |
| 77 | + return hook.LogLevels |
| 78 | +} |
| 79 | + |
| 80 | +func SetupLogging() { |
| 81 | + logrus.SetOutput(io.Discard) |
| 82 | + |
| 83 | + logrus.AddHook(&StandardHook{ |
| 84 | + Writer: os.Stderr, |
| 85 | + LogLevels: logrus.AllLevels, |
| 86 | + Formatter: &logrus.TextFormatter{ForceColors: true}, |
| 87 | + }) |
| 88 | + |
| 89 | + now := time.Now().Format("20060102150405") |
| 90 | + |
| 91 | + dir := defaults.HelmVMLogsSubDir() |
| 92 | + path := filepath.Join(dir, "helmvm-"+now+".log") |
| 93 | + |
| 94 | + fileHook, err := NewLogrusFileHook(path, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666) |
| 95 | + if err == nil { |
| 96 | + logrus.AddHook(fileHook) |
| 97 | + } |
| 98 | +} |
0 commit comments