From 33ec19647b03c928408eac7aa2feecd54ffa1d50 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 21 May 2023 21:27:44 +0400 Subject: [PATCH] Reworked logging. Store all actions in one file.log. --- cmd/dwelling-upload/main.go | 23 +++++------------------ configs/config.yaml | 4 +--- internal/configuration/configuration.go | 4 +--- internal/http/handlers.go | 20 ++++++++------------ 4 files changed, 15 insertions(+), 36 deletions(-) diff --git a/cmd/dwelling-upload/main.go b/cmd/dwelling-upload/main.go index f98b55f..99b0198 100644 --- a/cmd/dwelling-upload/main.go +++ b/cmd/dwelling-upload/main.go @@ -44,23 +44,11 @@ func main() { } defer logErr.Close() - logUpload, err := logging.New(config.Log.Upload) + logFile, err := logging.New(config.Log.File) if err != nil { - log.Fatalln("upload logger:", err) + log.Fatalln("file logger:", err) } - defer logUpload.Close() - - logDownload, err := logging.New(config.Log.Download) - if err != nil { - log.Fatalln("download logger:", err) - } - defer logDownload.Close() - - logDelete, err := logging.New(config.Log.Delete) - if err != nil { - log.Fatalln("delete logger:", err) - } - defer logDelete.Close() + defer logFile.Close() watcha, err := watcher.NewInotifyWatcher() if err != nil { @@ -80,7 +68,7 @@ func main() { watcha.WatchForMask(uploadDirNotify, watcher.CrDelMask) - hand := http.NewUploadHandlers(config, logErr, logUpload, logDownload, logDelete, &uploadDirSize) + hand := http.NewUploadHandlers(config, logErr, logFile, &uploadDirSize) srv := http.NewHttpServer() srv.SetNotFoundHandler(http.NotFound) @@ -109,8 +97,7 @@ func main() { select { case <-logReopenSignal: logErr.Reopen(config.Log.Error) - logUpload.Reopen(config.Log.Upload) - logDownload.Reopen(config.Log.Download) + logFile.Reopen(config.Log.File) case <-uploadDirNotify: uploadDirSize, err = utils.DirectorySize(config.Uploads.Directory) if err != nil { diff --git a/configs/config.yaml b/configs/config.yaml index 444e850..62c9ff4 100644 --- a/configs/config.yaml +++ b/configs/config.yaml @@ -9,9 +9,7 @@ hash_salt: "iyP3oZWHI3xO3XBF7s78Vg" log: # dwelling-upload logs. error: "/var/log/dwelling-upload/error.log" - upload: "/var/log/dwelling-upload/upload.log" - download: "/var/log/dwelling-upload/download.log" - delete: "/var/log/dwelling-upload/delete.log" + file: "/var/log/dwelling-upload/file.log" # dwelling-upload-clean logs. clean: "/var/log/dwelling-upload/clean.log" clean_error: "/var/log/dwelling-upload/clean_error.log" diff --git a/internal/configuration/configuration.go b/internal/configuration/configuration.go index b432504..1ea0136 100644 --- a/internal/configuration/configuration.go +++ b/internal/configuration/configuration.go @@ -15,9 +15,7 @@ type Configuration struct { User string `yaml:"user"` Log struct { Error string `yaml:"error"` - Upload string `yaml:"upload"` - Download string `yaml:"download"` - Delete string `yaml:"delete"` + File string `yaml:"file"` Clean string `yaml:"clean"` CleanError string `yaml:"clean_error"` } `yaml:"log"` diff --git a/internal/http/handlers.go b/internal/http/handlers.go index 2b61ab4..9997d48 100644 --- a/internal/http/handlers.go +++ b/internal/http/handlers.go @@ -19,22 +19,18 @@ import ( ) type UploadHandlers struct { - conf *configuration.Configuration - logErr *logging.Logger - logUpload *logging.Logger - logDownload *logging.Logger - logDelete *logging.Logger + conf *configuration.Configuration + logErr *logging.Logger + logFile *logging.Logger uploadDirSize *int64 } -func NewUploadHandlers(conf *configuration.Configuration, lErr, lUp, lDown, lDel *logging.Logger, uploadDirSize *int64) *UploadHandlers { +func NewUploadHandlers(conf *configuration.Configuration, lErr, lFile *logging.Logger, uploadDirSize *int64) *UploadHandlers { return &UploadHandlers{ conf: conf, logErr: lErr, - logUpload: lUp, - logDownload: lDown, - logDelete: lDel, + logFile: lFile, uploadDirSize: uploadDirSize} } @@ -128,7 +124,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) { typ, _ := utils.NetworkType(r.Host) - h.logUpload.Printf("| %s | %s | %s | SHA256 %s | %s | %d | %s", r.Header.Get("X-Real-IP"), typ, + h.logFile.Printf("up | %s | %s | %s | SHA256 %s | %s | %d | %s", r.Header.Get("X-Real-IP"), typ, fHandler.Filename, fHash, fSaltedHash, fHandler.Size, r.UserAgent()) w.WriteHeader(http.StatusCreated) @@ -176,7 +172,7 @@ func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) { netTyp, _ := utils.NetworkType(r.Host) - h.logDownload.Printf("| %s | %s | %s | %s | %s", r.Header.Get("X-Real-IP"), netTyp, name, saltedHash, r.UserAgent()) + h.logFile.Printf("dw | %s | %s | %s | %s | %s", r.Header.Get("X-Real-IP"), netTyp, name, saltedHash, r.UserAgent()) http.ServeContent(w, r, path, stat.ModTime(), fd) } @@ -207,7 +203,7 @@ func (h *UploadHandlers) Delete(w http.ResponseWriter, r *http.Request) { netTyp, _ := utils.NetworkType(r.Host) - h.logDelete.Printf("| %s | %s | %s | %s", r.Header.Get("X-Real-IP"), netTyp, saltedHash, r.UserAgent()) + h.logFile.Printf("dt | %s | %s | %s | %s", r.Header.Get("X-Real-IP"), netTyp, saltedHash, r.UserAgent()) w.WriteHeader(http.StatusNoContent) web.Deleted(utils.MainSite(r.Host))