32 lines
798 B
Go
32 lines
798 B
Go
|
|
package log
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/sirupsen/logrus"
|
||
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
||
|
|
)
|
||
|
|
|
||
|
|
type LumberjackHook struct {
|
||
|
|
Logger *lumberjack.Logger
|
||
|
|
}
|
||
|
|
|
||
|
|
func (hook *LumberjackHook) Fire(entry *logrus.Entry) error {
|
||
|
|
|
||
|
|
// Write to the lumberjack.Logger (log file with rotation)
|
||
|
|
_, err := hook.Logger.Write(hook.Format(entry))
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
// Implement the logrus.Hook interface method Levels
|
||
|
|
func (hook *LumberjackHook) Levels() []logrus.Level {
|
||
|
|
return logrus.AllLevels
|
||
|
|
}
|
||
|
|
func (hook *LumberjackHook) Format(entry *logrus.Entry) []byte {
|
||
|
|
// Format the timestamp with timezone offset
|
||
|
|
timestamp := entry.Time.Format("2006-01-02T15:04:05-07:00")
|
||
|
|
|
||
|
|
// Format the log message
|
||
|
|
logMessage := fmt.Sprintf("%s[%s] %s\n", entry.Level.String(), timestamp, entry.Message)
|
||
|
|
return []byte(logMessage)
|
||
|
|
}
|