2022-11-30 22:59:33 +01:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2022-12-09 00:40:50 +01:00
|
|
|
"io"
|
2022-11-30 22:59:33 +01:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
func InitTests() {
|
2022-12-09 00:40:50 +01:00
|
|
|
log.Logger = createLogger(createConsoleWriter())
|
|
|
|
|
|
|
|
gin.SetMode(gin.TestMode)
|
2024-09-20 20:56:22 +02:00
|
|
|
|
|
|
|
if llstr, ok := os.LookupEnv("SCN_TEST_LOGLEVEL"); ok {
|
|
|
|
ll, err := zerolog.ParseLevel(llstr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
zerolog.SetGlobalLevel(ll)
|
|
|
|
} else {
|
|
|
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
|
|
|
}
|
|
|
|
|
2022-12-09 00:40:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func createConsoleWriter() *zerolog.ConsoleWriter {
|
|
|
|
return &zerolog.ConsoleWriter{
|
2022-11-30 22:59:33 +01:00
|
|
|
Out: os.Stdout,
|
|
|
|
TimeFormat: "2006-01-02 15:04:05.000 Z07:00",
|
|
|
|
}
|
2022-12-09 00:40:50 +01:00
|
|
|
}
|
2022-11-30 22:59:33 +01:00
|
|
|
|
2022-12-09 00:40:50 +01:00
|
|
|
func createLogger(cw io.Writer) zerolog.Logger {
|
2022-11-30 22:59:33 +01:00
|
|
|
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
|
2022-12-09 00:40:50 +01:00
|
|
|
|
2022-11-30 22:59:33 +01:00
|
|
|
multi := zerolog.MultiLevelWriter(cw)
|
|
|
|
logger := zerolog.New(multi).With().
|
|
|
|
Timestamp().
|
|
|
|
Caller().
|
|
|
|
Logger()
|
|
|
|
|
2022-12-09 00:40:50 +01:00
|
|
|
return logger
|
2022-11-30 22:59:33 +01:00
|
|
|
}
|