Files
Message-Push-Nest/pkg/logging/logger.go
T

64 lines
1.4 KiB
Go
Raw Normal View History

2023-12-30 17:40:20 +08:00
package logging
import (
2024-01-01 12:03:56 +08:00
"fmt"
2024-01-13 11:43:54 +08:00
"github.com/engigu/logrus-prefixed-formatter"
2023-12-30 17:40:20 +08:00
"github.com/sirupsen/logrus"
2024-01-13 11:43:54 +08:00
"message-nest/pkg/setting"
2023-12-30 17:40:20 +08:00
"os"
2024-01-01 12:03:56 +08:00
"path/filepath"
2024-01-13 11:43:54 +08:00
"strings"
2023-12-30 17:40:20 +08:00
)
2024-01-13 12:04:40 +08:00
//var Logger = logrus.New()
2023-12-30 17:40:20 +08:00
2024-01-13 11:43:54 +08:00
func getFuncOutStr(funcStr string) string {
parts := strings.Split(funcStr, ".")
if len(parts) > 0 {
return parts[len(parts)-1]
2024-01-01 12:03:56 +08:00
} else {
2024-01-13 11:43:54 +08:00
return ""
2024-01-01 12:03:56 +08:00
}
2024-01-13 11:43:54 +08:00
}
2024-01-01 12:03:56 +08:00
2024-01-17 23:15:47 +08:00
func CustomCallerFormatter(funcStr string, fileStr string) string {
2024-01-13 11:43:54 +08:00
fName := filepath.Base(fileStr)
funcOut := getFuncOutStr(funcStr)
return fmt.Sprintf(" [%s %s]", fName, funcOut)
2024-01-01 12:03:56 +08:00
}
2023-12-30 17:40:20 +08:00
func Setup() {
2024-01-13 11:43:54 +08:00
formatter := new(prefixed.TextFormatter)
formatter.DisableTimestamp = false
2024-01-13 12:38:32 +08:00
formatter.DisableColors = false
2024-01-13 12:41:53 +08:00
formatter.ForceColors = true
2024-01-13 12:07:30 +08:00
formatter.ForceFormatting = true
2024-01-13 11:43:54 +08:00
formatter.FullTimestamp = true
2024-01-17 23:15:47 +08:00
formatter.CallerFormatter = CustomCallerFormatter
2024-01-13 12:55:47 +08:00
formatter.TimestampFormat = "2006-01-02 15:04:05.000"
2024-01-13 11:43:54 +08:00
formatter.SetColorScheme(&prefixed.ColorScheme{
TimestampStyle: "cyan",
CallerStyle: "cyan",
PrefixStyle: "green",
})
2024-01-13 12:04:40 +08:00
logrus.SetFormatter(formatter)
logrus.SetReportCaller(true)
logrus.SetOutput(os.Stdout)
2024-01-13 11:43:54 +08:00
level := strings.ToLower(setting.AppSetting.LogLevel)
switch level {
case "debug":
2024-01-13 12:04:40 +08:00
logrus.SetLevel(logrus.DebugLevel)
2024-01-13 11:43:54 +08:00
case "info":
2024-01-13 12:04:40 +08:00
logrus.SetLevel(logrus.InfoLevel)
2024-01-13 11:43:54 +08:00
case "warn":
2024-01-13 12:04:40 +08:00
logrus.SetLevel(logrus.WarnLevel)
2024-01-13 11:43:54 +08:00
case "error":
2024-01-13 12:04:40 +08:00
logrus.SetLevel(logrus.ErrorLevel)
2024-01-13 11:43:54 +08:00
default:
2024-01-13 12:04:40 +08:00
logrus.SetLevel(logrus.DebugLevel)
2024-01-13 11:43:54 +08:00
}
2023-12-30 17:40:20 +08:00
}