79 lines
1.7 KiB
Go
79 lines
1.7 KiB
Go
package rslogger
|
|
|
|
import (
|
|
"encoding/json"
|
|
"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") + " "
|
|
}
|
|
|
|
type LogManagerRequestBody struct {
|
|
Type string // like system, grouptasks, etc
|
|
Logs []string // array of logs
|
|
}
|
|
|
|
const (
|
|
LogTypeInfo = "I "
|
|
LogTypeWarning = "W "
|
|
LogTypeError = "E "
|
|
)
|
|
|
|
func LogManagerRequestClient(requestMethod string, requestBody LogManagerRequestBody) {
|
|
if usedLogManagerServerUrl == "" {
|
|
log.Error().Msgf("Log manager server url is not set, skipping request")
|
|
return
|
|
}
|
|
|
|
a := fiber.AcquireAgent()
|
|
|
|
req := a.Request()
|
|
req.Header.SetMethod(requestMethod)
|
|
req.SetRequestURI(usedLogManagerServerUrl + "/v1/log")
|
|
req.Header.SetContentType("application/json")
|
|
|
|
reqestBodyBytes, err := json.Marshal(requestBody)
|
|
|
|
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.Debug().Msgf("Log manager request, code: %d, body: %s", code, body)
|
|
}
|