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

63 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
)
var Logger = logrus.New()
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-13 11:43:54 +08:00
func CallerFormatter(funcStr string, fileStr string) string {
fName := filepath.Base(fileStr)
println(fileStr, funcStr, fName)
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
formatter.ForceFormatting = false
formatter.FullTimestamp = true
formatter.CallerFormatter = CallerFormatter
formatter.TimestampFormat = "2006-01-02 15:04:05.999"
formatter.SetColorScheme(&prefixed.ColorScheme{
TimestampStyle: "cyan",
CallerStyle: "cyan",
PrefixStyle: "green",
})
Logger.Formatter = formatter
2024-01-01 12:03:56 +08:00
Logger.SetReportCaller(true)
2023-12-30 17:40:20 +08:00
Logger.SetOutput(os.Stdout)
2024-01-13 11:43:54 +08:00
level := strings.ToLower(setting.AppSetting.LogLevel)
switch level {
case "debug":
Logger.SetLevel(logrus.DebugLevel)
case "info":
Logger.SetLevel(logrus.InfoLevel)
case "warn":
Logger.SetLevel(logrus.WarnLevel)
case "error":
Logger.SetLevel(logrus.ErrorLevel)
default:
Logger.SetLevel(logrus.DebugLevel)
}
2023-12-30 17:40:20 +08:00
}