From f44280abb93e07f80e3d7863b64f846c7112d593 Mon Sep 17 00:00:00 2001 From: minoplhy Date: Thu, 11 Apr 2024 00:19:11 +0700 Subject: [PATCH] add: zerologger as log library --- go.mod | 8 +++++ go.sum | 15 +++++++++ main.go | 73 +++++++++++++++++++++++----------------- src/handler/file.go | 13 +++---- src/handler/templates.go | 15 ++++++--- 5 files changed, 82 insertions(+), 42 deletions(-) diff --git a/go.mod b/go.mod index 55bf367..c5ca88d 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,11 @@ module github.com/minoplhy/chibisafe_netstorage_middleman go 1.22.2 + +require github.com/rs/zerolog v1.32.0 + +require ( + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + golang.org/x/sys v0.12.0 // indirect +) diff --git a/go.sum b/go.sum index e69de29..adc0e97 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,15 @@ +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0= +github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go index a3c1657..533d0cc 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,14 @@ package main import ( "encoding/json" "fmt" - "log" + "io" "net/http" "os" "strconv" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + "github.com/minoplhy/chibisafe_netstorage_middleman/src/handler" ) @@ -22,17 +25,9 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) { maxUploadSize = 10 * 1024 * 1024 // 10 MB } - // Open or create a file for appending logs - log_file, err := os.OpenFile("activity.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) - if err != nil { - log.Fatal(err) - } - defer log_file.Close() - log.SetOutput(log_file) - if r.Method != "POST" { http.Error(w, handler.ErrorResponseBuild(http.StatusMethodNotAllowed, "Method not allowed"), http.StatusMethodNotAllowed) - handler.LogBuilder("ERROR", []string{r.RemoteAddr}, "Method not allowed") + handler.ErrorLogBuilder([]string{r.RemoteAddr}, "Method not allowed") return } @@ -40,14 +35,14 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) { API_Key := r.Header.Get("x-api-key") if API_Key == "" { http.Error(w, handler.ErrorResponseBuild(http.StatusBadRequest, "X-api-key is empty!"), http.StatusBadRequest) - handler.LogBuilder("ERROR", []string{r.RemoteAddr}, "X-api-key is empty!") + handler.ErrorLogBuilder([]string{r.RemoteAddr}, "X-api-key is empty!") return } // Validate x-api-key if !handler.Check_API_Key(Chibisafe_basepath, API_Key) { http.Error(w, handler.ErrorResponseBuild(http.StatusUnauthorized, "Failure to validate X-API-Key"), http.StatusUnauthorized) - handler.LogBuilder("ERROR", []string{r.RemoteAddr}, "Failure to validate X-API-Key") + handler.ErrorLogBuilder([]string{r.RemoteAddr}, "Failure to validate X-API-Key") return } @@ -58,11 +53,11 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) { if err != nil { if err.Error() == "http: request body too large" { http.Error(w, handler.ErrorResponseBuild(http.StatusRequestEntityTooLarge, "Request Body is too large!"), http.StatusRequestEntityTooLarge) - handler.LogBuilder("ERROR", []string{r.RemoteAddr}, "Request Body is too large!") + handler.ErrorLogBuilder([]string{r.RemoteAddr}, "Request Body is too large!") return } http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError) - handler.LogBuilder("ERROR", []string{r.RemoteAddr}, err.Error()) + handler.ErrorLogBuilder([]string{r.RemoteAddr}, err.Error()) return } @@ -73,14 +68,14 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) { file, fileHeader, err := r.FormFile("file") if err != nil { http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError) - handler.LogBuilder("ERROR", []string{r.RemoteAddr}, err.Error()) + handler.ErrorLogBuilder([]string{r.RemoteAddr}, err.Error()) return } defer file.Close() - handler.LogBuilder("INFO", []string{r.RemoteAddr}, "Received a successful POST") + handler.InfoLogBuilder([]string{r.RemoteAddr}, "Received a successful POST") tempfilepath := handler.GetTempFilename(fileHeader.Filename) - handler.LogBuilder("INFO", []string{r.RemoteAddr, tempfilepath}, "Successfully obtained temporary Filename") + handler.InfoLogBuilder([]string{r.RemoteAddr, tempfilepath}, "Successfully obtained temporary Filename") handler.SaveFile(tempfilepath, file) handler.DiscardFile(file) @@ -93,7 +88,7 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) { chibisafe_post, err := handler.UploadPost(Chibisafe_basepath, API_Key, PostData) if err != nil { http.Error(w, handler.ErrorResponseBuild(http.StatusBadRequest, "Something went wrong!"), http.StatusBadRequest) - handler.LogBuilder("ERROR", []string{r.RemoteAddr, tempfilepath}, err.Error()) + handler.ErrorLogBuilder([]string{r.RemoteAddr, tempfilepath}, err.Error()) return } @@ -101,18 +96,18 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) { err = json.Unmarshal(chibisafe_post, &chibisafe_Response_Metadata) if err != nil { http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError) - handler.LogBuilder("ERROR", []string{}, err.Error()) + handler.ErrorLogBuilder([]string{}, err.Error()) return } - handler.LogBuilder("INFO", []string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, "Successfully obtained PUT keys") + handler.InfoLogBuilder([]string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, "Successfully obtained PUT keys") _, err = handler.NetworkStoragePut(chibisafe_Response_Metadata.URL, PostData.ContentType, tempfilepath) if err != nil { http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError) - handler.LogBuilder("ERROR", []string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, err.Error()) + handler.ErrorLogBuilder([]string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, err.Error()) return } - handler.LogBuilder("INFO", []string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, "Successfully PUT file to Network Storage") + handler.InfoLogBuilder([]string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, "Successfully PUT file to Network Storage") // Build Struct for PostProcess Json // @@ -128,7 +123,7 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) { PostProcess, err := handler.UploadProcessPost(Chibisafe_basepath, API_Key, PostProcessData) if err != nil { http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError) - handler.LogBuilder("ERROR", []string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, err.Error()) + handler.ErrorLogBuilder([]string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, err.Error()) return } @@ -136,40 +131,56 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) { err = json.Unmarshal(PostProcess, &PostProcessResponse) if err != nil { http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError) - handler.LogBuilder("ERROR", []string{}, err.Error()) + handler.ErrorLogBuilder([]string{}, err.Error()) return } - handler.LogBuilder("INFO", []string{r.RemoteAddr, PostProcessResponse.Name, tempfilepath}, fmt.Sprintf("Successfully Processed Response with UUID: %s", PostProcessResponse.UUID)) + handler.InfoLogBuilder([]string{r.RemoteAddr, PostProcessResponse.Name, tempfilepath}, fmt.Sprintf("Successfully Processed Response with UUID: %s", PostProcessResponse.UUID)) err = handler.DeleteFile(tempfilepath) if err != nil { - handler.LogBuilder("ERROR", []string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, err.Error()) + handler.ErrorLogBuilder([]string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, err.Error()) return } - handler.LogBuilder("INFO", []string{r.RemoteAddr, tempfilepath}, "Successfully Deleted Temporary file from local disk") + handler.InfoLogBuilder([]string{r.RemoteAddr, tempfilepath}, "Successfully Deleted Temporary file from local disk") JsonResponse, _ := json.Marshal(PostProcessResponse) fmt.Fprintf(w, "%s", JsonResponse) } func main() { + // Open or create a file for appending logs + log_file, err := os.OpenFile("activity.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + log.Fatal().Msg(err.Error()) + } + defer log_file.Close() + + // Setup Logging Policy + // Multi level writer on logfile and console + // + // Format : Console -> Human Readable + // File -> Json + logger := zerolog.New(zerolog.MultiLevelWriter(log_file, os.Stdout)).With().Timestamp().Logger() + logger = logger.Output(io.MultiWriter(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "15:04:05"}, log_file)) + // Set as Global logger :) + log.Logger = logger + Chibisafe_basepath := os.Getenv("CHIBISAFE_BASEPATH") Max_Upload_Size := os.Getenv("MAX_UPLOAD_SIZE") if Chibisafe_basepath == "" { - log.Fatal("CHIBISAFE_BASEPATH environment is not set!") + log.Fatal().Msg("CHIBISAFE_BASEPATH environment is not set!") } if Max_Upload_Size != "" { _, err := strconv.Atoi(Max_Upload_Size) if err != nil { - log.Fatal("MAX_UPLOAD_SIZE environment is invaild!") + log.Fatal().Msg("MAX_UPLOAD_SIZE environment is invaild!") } } - mux := http.NewServeMux() mux.HandleFunc("/api/v1/upload", uploadHandler) if err := http.ListenAndServe(":4040", mux); err != nil { - log.Fatal(err) + log.Fatal().Msg(err.Error()) } } diff --git a/src/handler/file.go b/src/handler/file.go index 2bebd4a..960bcd7 100644 --- a/src/handler/file.go +++ b/src/handler/file.go @@ -3,11 +3,12 @@ package handler import ( "fmt" "io" - "log" "mime/multipart" "os" "path/filepath" "time" + + "github.com/rs/zerolog/log" ) func GetTempFilename(Filename string) string { @@ -18,13 +19,13 @@ func GetTempFilename(Filename string) string { func SaveFile(filename string, file multipart.File) error { err := os.MkdirAll("./uploads", os.ModePerm) if err != nil { - log.Panic(err) + log.Panic().Msg(err.Error()) return err } dst, err := os.Create(filename) if err != nil { - log.Panic(err) + log.Panic().Msg(err.Error()) return err } @@ -34,7 +35,7 @@ func SaveFile(filename string, file multipart.File) error { // at the specified destination _, err = io.Copy(dst, file) if err != nil { - log.Panic(err) + log.Panic().Msg(err.Error()) return err } return nil @@ -43,7 +44,7 @@ func SaveFile(filename string, file multipart.File) error { func DeleteFile(filePath string) error { err := os.Remove(filePath) if err != nil { - log.Panic(err) + log.Panic().Msg(err.Error()) return err } return nil @@ -53,7 +54,7 @@ func DiscardFile(file multipart.File) error { // Clear the data from memory _, err := io.Copy(io.Discard, file) if err != nil { - log.Panic(err) + log.Panic().Msg(err.Error()) return err } return nil diff --git a/src/handler/templates.go b/src/handler/templates.go index e706ee0..abbc303 100644 --- a/src/handler/templates.go +++ b/src/handler/templates.go @@ -2,8 +2,9 @@ package handler import ( "encoding/json" - "log" "strings" + + "github.com/rs/zerolog/log" ) func ErrorResponseBuild(StatusCode int64, Message string) string { @@ -15,8 +16,12 @@ func ErrorResponseBuild(StatusCode int64, Message string) string { return string(Response) } -func LogBuilder(Level string, headers []string, message string) { - logString := "[" + Level + "]" + " " - logString += "[" + strings.Join(headers, "] [") + "]" + " : " + message - log.Println(logString) +func InfoLogBuilder(headers []string, message string) { + logString := "[" + strings.Join(headers, "] [") + "]" + " : " + message + log.Info().Msgf(logString) +} + +func ErrorLogBuilder(headers []string, message string) { + logString := "[" + strings.Join(headers, "] [") + "]" + " : " + message + log.Error().Msgf(logString) }