package rslogger import ( "encoding/json" "fmt" "os" "time" "github.com/gofiber/fiber/v2" "github.com/rs/zerolog" "github.com/rs/zerolog/log" ) var usedLogManagerServerUrl string func InitLogger(debug bool, colorizedOutput bool, logManagerServerUrl string) { zerolog.TimeFieldFormat = zerolog.TimeFormatUnix if debug { zerolog.SetGlobalLevel(zerolog.DebugLevel) } else { zerolog.SetGlobalLevel(zerolog.InfoLevel) } if colorizedOutput { log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "15:04:05"}) } if logManagerServerUrl != "" { usedLogManagerServerUrl = logManagerServerUrl } } func getTime() string { return time.Now().Format("15:04:05 02-01-2006") + " " } func AddSystemLog(format string, v ...any) { log.Info().Msgf("used log manager server url: %s", usedLogManagerServerUrl) go LogManagerRequestClient(fiber.MethodPost, usedLogManagerServerUrl+"/v1/log", LogManagerRequestBody{ Type: "system", Logs: []string{"I " + getTime() + fmt.Sprintf(format, v...)}}) } func AddGroupTasksLog(format string, v ...any) { go LogManagerRequestClient(fiber.MethodPost, usedLogManagerServerUrl+"/v1/log", LogManagerRequestBody{ Type: "grouptasks", Logs: []string{"I " + getTime() + fmt.Sprintf(format, v...)}}) } type LogManagerRequestBody struct { Type string Logs []string } func LogManagerRequestClient(requestMethod string, url string, body interface{}) { a := fiber.AcquireAgent() req := a.Request() req.Header.SetMethod(requestMethod) req.SetRequestURI(url) req.Header.SetContentType("application/json") reqestBodyBytes, err := json.Marshal(body) if err != nil { log.Error().Msgf("Failed to marshal request body, err: %s", err) return } req.SetBody(reqestBodyBytes) if err := a.Parse(); err != nil { log.Error().Msgf("Failed to parse request, err: %s", err) return } code, body, _ := a.Bytes() log.Info().Msgf("Log manager request, code: %d, body: %s", code, body) }