8 rotatelogs "github.com/lestrrat-go/file-rotatelogs"
9 "github.com/sirupsen/logrus"
11 "github.com/vapor/config"
15 rotationTime int64 = 86400
19 var defaultFormatter = &logrus.TextFormatter{DisableColors: true}
21 func InitLogFile(config *config.Config) {
22 hook := newBtmHook(config.LogDir())
31 func newBtmHook(logPath string) *BtmHook {
32 hook := &BtmHook{lock: new(sync.Mutex)}
33 hook.logPath = logPath
37 // Write a log line to an io.Writer.
38 func (hook *BtmHook) ioWrite(entry *logrus.Entry) error {
40 if data, ok := entry.Data["module"]; ok {
41 module = data.(string)
44 logPath := filepath.Join(hook.logPath, module)
45 writer, err := rotatelogs.New(
47 rotatelogs.WithMaxAge(time.Duration(maxAge)*time.Second),
48 rotatelogs.WithRotationTime(time.Duration(rotationTime)*time.Second),
54 msg, err := defaultFormatter.Format(entry)
59 _, err = writer.Write(msg)
63 func (hook *BtmHook) Fire(entry *logrus.Entry) error {
65 defer hook.lock.Unlock()
66 return hook.ioWrite(entry)
69 // Levels returns configured log levels.
70 func (hook *BtmHook) Levels() []logrus.Level {
71 return logrus.AllLevels