-
Notifications
You must be signed in to change notification settings - Fork 43
/
file.go
125 lines (108 loc) · 3.16 KB
/
file.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package logging
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/sirupsen/logrus"
)
const (
AutoLogFile = "auto"
logDateFormat = "2006-01-02T15-04-05"
)
// FileHook to send logs to the trace file regardless of CLI level.
type FileHook struct {
// Logger is a reference to the internal Logger that this utilizes.
Logger *logrus.Logger
// File is a reference to the file being written to.
File *os.File
// FilePath is the full path used when creating the file.
FilePath string
// Closed reports whether the hook has been closed. Once closed
// it can't be written to again.
Closed bool
}
func NewFileHook(path string) (*FileHook, error) {
if path == AutoLogFile {
path = filepath.Join(os.Getenv("HOME"), ".crashd", getLogNameFromTime(time.Now()))
}
file, err := os.Create(path)
logger := logrus.New()
logger.SetLevel(logrus.TraceLevel)
logger.SetOutput(file)
logrus.Infof("Detailed logs being written to: %v", path)
return &FileHook{Logger: logger, File: file, FilePath: path}, err
}
func (hook *FileHook) Fire(entry *logrus.Entry) error {
if hook.Closed {
return nil
}
switch entry.Level {
case logrus.PanicLevel:
hook.Logger.Panic(entry.Message)
case logrus.FatalLevel:
hook.Logger.Fatal(entry.Message)
case logrus.ErrorLevel:
hook.Logger.Error(entry.Message)
case logrus.WarnLevel:
hook.Logger.Warning(entry.Message)
case logrus.InfoLevel:
hook.Logger.Info(entry.Message)
case logrus.DebugLevel:
hook.Logger.Debug(entry.Message)
case logrus.TraceLevel:
hook.Logger.Trace(entry.Message)
default:
hook.Logger.Info(entry.Message)
}
return nil
}
func (hook *FileHook) Levels() []logrus.Level {
return logrus.AllLevels
}
// CloseFileHooks will close each file being used for each FileHook attached to the logger.
// If the logger passed is nil, will reference the logrus.StandardLogger().
func CloseFileHooks(l *logrus.Logger) error {
// All the hooks we utilize are just tied to the standard logger.
logrus.Debugln("Closing log file; future log calls will not be persisted.")
if l == nil {
l = logrus.StandardLogger()
}
for _, fh := range GetFileHooks(l) {
fh.File.Close()
fh.Closed = true
}
return nil
}
// GetFirstFileHook is a convenience method to take an object and returns the first
// FileHook attached to it. Accepts an interface{} since the logger objects may be put
// into context or thread objects. The obj should be a *logrus.Logger object.
func GetFirstFileHook(obj interface{}) *FileHook {
fhs := GetFileHooks(obj)
if len(fhs) > 0 {
return fhs[0]
}
return nil
}
// GetFileHooks is a convenience method to take an object and returns the
// FileHooks attached to it. Accepts an interface{} since the logger objects may be put
// into context or thread objects. The obj should be a *logrus.Logger object.
func GetFileHooks(obj interface{}) []*FileHook {
l, ok := obj.(*logrus.Logger)
if !ok {
return nil
}
result := []*FileHook{}
for _, hooks := range l.Hooks {
for _, hook := range hooks {
switch fh := hook.(type) {
case *FileHook:
result = append(result, fh)
}
}
}
return result
}
func getLogNameFromTime(t time.Time) string {
return fmt.Sprintf("crashd_%v.log", t.Format(logDateFormat))
}