Skip to content

Commit

Permalink
add file logging and rotation
Browse files Browse the repository at this point in the history
Signed-off-by: Rajiv Harlalka <[email protected]>
  • Loading branch information
rajivharlalka committed Sep 9, 2024
1 parent 6536685 commit e906bf2
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ qp.tar.xz

go.work.sum

frontend/public/pdf.worker.min.mjs
frontend/public/pdf.worker.min.mjs
*.log
1 change: 1 addition & 0 deletions backend/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
- iqps-backend
metaploy-private-network:
volumes:
- ./logs:~/iqps/logs
- ./db:/db
- nginx-config-volume:/etc/nginx/sites-enabled
- odins-vault:/srv/static
Expand Down
3 changes: 3 additions & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
)

require (
Expand Down
7 changes: 7 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo=
github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand All @@ -28,9 +30,14 @@ golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
11 changes: 5 additions & 6 deletions backend/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"log"
"log/slog"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -127,7 +126,7 @@ func HandleQPSearch(w http.ResponseWriter, r *http.Request) {
qps = append(qps, qp)
}

config.Get().Logger.Info("HandleQPSearch: Question paper search query %s returned %d results", course, len(qps))
config.Get().Logger.Info("HandleQPSearch: Question paper search query:$%", course, ":$%result count:", len(qps))
sendResponse(w, http.StatusOK, qps)
}

Expand All @@ -151,7 +150,7 @@ func ListUnapprovedPapers(w http.ResponseWriter, r *http.Request) {
qp.FileLink = fmt.Sprintf("%s/%s", config.Get().StaticFilesUrl, qp.FileLink)
qps = append(qps, qp)
}
config.Get().Logger.Info("listUnapprovedPapers: Unapproved Question paper count", slog.Int("QP count", len(qps)))
config.Get().Logger.Info("listUnapprovedPapers: Unapproved Question paper count: ", len(qps))
sendResponse(w, http.StatusOK, qps)
}

Expand All @@ -177,7 +176,7 @@ func HandleFileUpload(w http.ResponseWriter, r *http.Request) {
files := r.MultipartForm.File["files"]
log.Printf("/upload: Received %d files.", len(files))
if len(files) > maxLimit {
config.Get().Logger.Error("HandleFileUpload: Allowed %s file uploads, found %d", slog.Int("max Limit", maxLimit), slog.Int("Upload Length", len(files)))
config.Get().Logger.Error("HandleFileUpload: Allowed max Limit: ", maxLimit, " file uploads, found:", "Upload Length", len(files))
sendErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("maximum %d files allowed", maxLimit), nil)
return
}
Expand All @@ -186,7 +185,7 @@ func HandleFileUpload(w http.ResponseWriter, r *http.Request) {
resp := uploadEndpointRes{Filename: fileHeader.Filename, Status: "success"}

if fileHeader.Size > 10<<20 {
config.Get().Logger.Error("HandleFileUpload: File of size %s uploaded", slog.Int64("filesize", fileHeader.Size))
config.Get().Logger.Errorf("HandleFileUpload: File of size %d uploaded", fileHeader.Size)
resp.Status = "failed"
resp.Description = "file size exceeds 10MB"
response = append(response, resp)
Expand All @@ -204,7 +203,7 @@ func HandleFileUpload(w http.ResponseWriter, r *http.Request) {

fileType := fileHeader.Header.Get("Content-Type")
if fileType != "application/pdf" {
config.Get().Logger.Error("HandleFileUpload:", slog.String("Invalid file type", fileType))
config.Get().Logger.Error("HandleFileUpload: Invalid file type ", fileType)
resp.Status = "failed"
resp.Description = "invalid file type. Only PDFs are supported"
response = append(response, resp)
Expand Down
3 changes: 2 additions & 1 deletion backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"errors"
"fmt"
"log/slog"
"net/http"

Expand Down Expand Up @@ -30,7 +31,7 @@ func main() {
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowedOrigins: []string{"https://qp.metakgp.org", "http://localhost:3000", "http://localhost:5173"},
})

fmt.Print("Logs are in ~/iqps/logs/ \n")
logger.Info("Main: Starting server on port 5000")
err := http.ListenAndServe(":5000", c.Handler(http.DefaultServeMux))
if errors.Is(err, http.ErrServerClosed) {
Expand Down
14 changes: 11 additions & 3 deletions backend/pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package config

import (
"log/slog"
"os"
"strconv"

"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
)

type Config struct {
Expand All @@ -14,7 +16,7 @@ type Config struct {
StaticFilesStorageLocation string
UploadedQPsPath string
MaxUploadLimit int
Logger *slog.Logger
Logger *logrus.Logger
}

type githubSecrets struct {
Expand Down Expand Up @@ -66,14 +68,20 @@ func Get() *Config {
maxUploadLimit = 10
}

log := logrus.New()
log.SetOutput(&lumberjack.Logger{
Filename: "~/iqps/logs/application.log",
MaxSize: 10, // Max size in MB
})

config = &Config{
DB: pgConf,
GithubSecrets: githubSecrets,
JWTSecret: os.Getenv("JWT_SECRET"),
StaticFilesUrl: os.Getenv("STATIC_FILES_URL"),
StaticFilesStorageLocation: os.Getenv("STATIC_FILES_STORAGE_LOCATION"),
UploadedQPsPath: os.Getenv("UPLOADED_QPS_PATH"),
Logger: slog.Default(),
Logger: log,
MaxUploadLimit: maxUploadLimit,
}
config.Logger.Info("config successfully setup")
Expand Down

0 comments on commit e906bf2

Please sign in to comment.