Files
test-iris-web-framework/app/lib/logging/logging.go
2023-10-26 16:49:24 +02:00

66 lines
1.2 KiB
Go

package logging
import (
"encoding/json"
"fmt"
"io"
"iris-test/app/lib/cfg"
"os"
"github.com/sirupsen/logrus"
)
var Log = logrus.New()
func Debug(message string) {
Log.Debug(message)
}
func Info(message string) {
Log.Info(message)
}
func Error(message string) {
Log.Error(message)
}
func Warn(message string) {
Log.Warn(message)
}
func InitLogging() {
logLevel, err := logrus.ParseLevel(cfg.Config.Application.LogLevel)
if err != nil {
panic(fmt.Sprintf("Invalid configured logLevel: %s\n", cfg.Config.Application.LogLevel))
}
Log.SetLevel(logLevel)
Log.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
PadLevelText: true,
DisableQuote: true,
})
LogFile := cfg.Config.Application.LogFile
file, err := os.OpenFile(
LogFile,
os.O_CREATE|os.O_WRONLY|os.O_APPEND,
0655,
)
if err != nil {
msg := fmt.Sprintf("Failed to log to file %s: %s", cfg.Config.Application.LogFile, err)
Log.Warning(msg)
panic(msg)
}
mw := io.MultiWriter(os.Stdout, file)
Log.SetOutput(mw)
configJson, err := json.Marshal(cfg.Config)
if err == nil {
Info(fmt.Sprintf("Using config: %s", configJson))
}
}