2022-12-16 07:29:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-10-26 08:16:35 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2022-12-16 07:29:01 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-10-26 08:16:35 +00:00
|
|
|
timestampFormat = "[2006-01-02 15:04:05] "
|
2022-12-16 07:29:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type LogFormatter struct{}
|
|
|
|
|
|
|
|
func (f *LogFormatter) Format(entry *log.Entry) ([]byte, error) {
|
2024-10-26 08:16:35 +00:00
|
|
|
levelStr := entry.Level.String()
|
|
|
|
if levelStr == "fatal" {
|
|
|
|
levelStr = "ERROR"
|
|
|
|
} else {
|
|
|
|
levelStr = strings.ToUpper(levelStr)
|
|
|
|
}
|
|
|
|
level := fmt.Sprintf("[%s] ", levelStr)
|
|
|
|
appName := ""
|
|
|
|
if logToStdout {
|
|
|
|
appName = "[notification-server] "
|
|
|
|
}
|
|
|
|
buf := make([]byte, 0, len(appName)+len(timestampFormat)+len(level)+len(entry.Message)+1)
|
|
|
|
if logToStdout {
|
|
|
|
buf = append(buf, appName...)
|
|
|
|
}
|
2022-12-16 07:29:01 +00:00
|
|
|
buf = entry.Time.AppendFormat(buf, timestampFormat)
|
2024-10-26 08:16:35 +00:00
|
|
|
buf = append(buf, level...)
|
2022-12-16 07:29:01 +00:00
|
|
|
buf = append(buf, entry.Message...)
|
|
|
|
buf = append(buf, '\n')
|
|
|
|
return buf, nil
|
|
|
|
}
|