1
0

Reworked logging. Store all actions in one file.log.

This commit is contained in:
Alexander Andreev 2023-05-21 21:27:44 +04:00
parent 31d2c4fd21
commit 33ec19647b
Signed by: Arav
GPG Key ID: D22A817D95815393
4 changed files with 15 additions and 36 deletions

View File

@ -44,23 +44,11 @@ func main() {
} }
defer logErr.Close() defer logErr.Close()
logUpload, err := logging.New(config.Log.Upload) logFile, err := logging.New(config.Log.File)
if err != nil { if err != nil {
log.Fatalln("upload logger:", err) log.Fatalln("file logger:", err)
} }
defer logUpload.Close() defer logFile.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()
watcha, err := watcher.NewInotifyWatcher() watcha, err := watcher.NewInotifyWatcher()
if err != nil { if err != nil {
@ -80,7 +68,7 @@ func main() {
watcha.WatchForMask(uploadDirNotify, watcher.CrDelMask) 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 := http.NewHttpServer()
srv.SetNotFoundHandler(http.NotFound) srv.SetNotFoundHandler(http.NotFound)
@ -109,8 +97,7 @@ func main() {
select { select {
case <-logReopenSignal: case <-logReopenSignal:
logErr.Reopen(config.Log.Error) logErr.Reopen(config.Log.Error)
logUpload.Reopen(config.Log.Upload) logFile.Reopen(config.Log.File)
logDownload.Reopen(config.Log.Download)
case <-uploadDirNotify: case <-uploadDirNotify:
uploadDirSize, err = utils.DirectorySize(config.Uploads.Directory) uploadDirSize, err = utils.DirectorySize(config.Uploads.Directory)
if err != nil { if err != nil {

View File

@ -9,9 +9,7 @@ hash_salt: "iyP3oZWHI3xO3XBF7s78Vg"
log: log:
# dwelling-upload logs. # dwelling-upload logs.
error: "/var/log/dwelling-upload/error.log" error: "/var/log/dwelling-upload/error.log"
upload: "/var/log/dwelling-upload/upload.log" file: "/var/log/dwelling-upload/file.log"
download: "/var/log/dwelling-upload/download.log"
delete: "/var/log/dwelling-upload/delete.log"
# dwelling-upload-clean logs. # dwelling-upload-clean logs.
clean: "/var/log/dwelling-upload/clean.log" clean: "/var/log/dwelling-upload/clean.log"
clean_error: "/var/log/dwelling-upload/clean_error.log" clean_error: "/var/log/dwelling-upload/clean_error.log"

View File

@ -15,9 +15,7 @@ type Configuration struct {
User string `yaml:"user"` User string `yaml:"user"`
Log struct { Log struct {
Error string `yaml:"error"` Error string `yaml:"error"`
Upload string `yaml:"upload"` File string `yaml:"file"`
Download string `yaml:"download"`
Delete string `yaml:"delete"`
Clean string `yaml:"clean"` Clean string `yaml:"clean"`
CleanError string `yaml:"clean_error"` CleanError string `yaml:"clean_error"`
} `yaml:"log"` } `yaml:"log"`

View File

@ -21,20 +21,16 @@ import (
type UploadHandlers struct { type UploadHandlers struct {
conf *configuration.Configuration conf *configuration.Configuration
logErr *logging.Logger logErr *logging.Logger
logUpload *logging.Logger logFile *logging.Logger
logDownload *logging.Logger
logDelete *logging.Logger
uploadDirSize *int64 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{ return &UploadHandlers{
conf: conf, conf: conf,
logErr: lErr, logErr: lErr,
logUpload: lUp, logFile: lFile,
logDownload: lDown,
logDelete: lDel,
uploadDirSize: uploadDirSize} uploadDirSize: uploadDirSize}
} }
@ -128,7 +124,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
typ, _ := utils.NetworkType(r.Host) 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()) fHandler.Filename, fHash, fSaltedHash, fHandler.Size, r.UserAgent())
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
@ -176,7 +172,7 @@ func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) {
netTyp, _ := utils.NetworkType(r.Host) 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) 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) 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) w.WriteHeader(http.StatusNoContent)
web.Deleted(utils.MainSite(r.Host)) web.Deleted(utils.MainSite(r.Host))