From 0b8b9f9745cb2e7dbba144a9f292b6481121e7b4 Mon Sep 17 00:00:00 2001 From: wuhuizuo Date: Wed, 9 Oct 2024 13:40:31 +0800 Subject: [PATCH] chore(tiup-publisher): add logs (#177) Signed-off-by: wuhuizuo Signed-off-by: wuhuizuo --- tiup-publisher/handler.go | 6 ++++++ tiup-publisher/main.go | 13 +++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/tiup-publisher/handler.go b/tiup-publisher/handler.go index 972f968..993509d 100644 --- a/tiup-publisher/handler.go +++ b/tiup-publisher/handler.go @@ -50,20 +50,26 @@ func (h *Handler) handleImpl(data *PublishRequestEvent) cloudevents.Result { // 1. get the the tarball from data.From. saveTo, err := downloadFile(data) if err != nil { + log.Err(err).Msg("download file failed") return cloudevents.NewReceipt(true, "download file failed: %v", err) } + log.Info().Msg("download file success") // 2. publish the tarball to the mirror. if err := publish(saveTo, &data.Publish, h.mirrorsURL); err != nil { + log.Err(err).Msg("publish to mirror failed") return cloudevents.NewReceipt(true, "publish to mirror failed: %v", err) } + log.Info().Msg("publish to mirror success") // 3. check the package is in the mirror. // printf 'post_check "$(tiup mirror show)/%s-%s-%s-%s.tar.gz" "%s"\n' \ remoteURL := fmt.Sprintf("%s/%s-%s-%s-%s.tar.gz", h.mirrorsURL, data.Publish.Name, data.Publish.Version, data.Publish.OS, data.Publish.Arch) if err := postCheck(saveTo, remoteURL); err != nil { + log.Err(err).Str("remote", remoteURL).Msg("post check failed") return cloudevents.NewReceipt(true, "post check failed: %v", err) } + log.Info().Str("remote", remoteURL).Msg("post check success") return cloudevents.ResultACK } diff --git a/tiup-publisher/main.go b/tiup-publisher/main.go index 1dee688..bcae738 100644 --- a/tiup-publisher/main.go +++ b/tiup-publisher/main.go @@ -5,6 +5,7 @@ import ( "encoding/json" "flag" "os" + "strconv" "github.com/cloudevents/sdk-go/v2/event" "github.com/rs/zerolog" @@ -14,8 +15,16 @@ import ( ) func main() { - zerolog.TimeFieldFormat = zerolog.TimeFormatUnix - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + logLevel, err := strconv.Atoi(os.Getenv("LOG_LEVEL")) + if err != nil { + logLevel = int(zerolog.InfoLevel) // default to INFO + } + log.Logger = log.Level(zerolog.Level(logLevel)) + + if os.Getenv("APP_ENV") == "development" { + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + } configFile := flag.String("config", "./config.yaml", "Path to config file") flag.Parse()