From 8bf1b65340705a26dc7b8260a9c3f521fe57a7f3 Mon Sep 17 00:00:00 2001 From: Joe Constant Date: Sun, 10 Dec 2023 19:27:02 -0700 Subject: [PATCH 1/6] Fix package name --- cmd/federation.go | 2 +- go.mod | 2 +- internal/activitypub/post.go | 2 +- internal/activitypub/user.go | 2 +- internal/http/routes/activity.go | 4 ++-- internal/http/routes/post.go | 4 ++-- internal/http/routes/user.go | 4 ++-- internal/http/server.go | 2 +- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cmd/federation.go b/cmd/federation.go index 236d342..4884765 100644 --- a/cmd/federation.go +++ b/cmd/federation.go @@ -2,7 +2,7 @@ package main import ( "os" - "participating-online/sublinks-federation/internal/http" + "sublinks/federation/internal/http" ) func main() { diff --git a/go.mod b/go.mod index 062994a..0be337f 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module participating-online/sublinks-federation +module sublinks/federation go 1.21.5 diff --git a/internal/activitypub/post.go b/internal/activitypub/post.go index 3e80b07..3f4bb1a 100644 --- a/internal/activitypub/post.go +++ b/internal/activitypub/post.go @@ -2,7 +2,7 @@ package activitypub import ( "fmt" - "participating-online/sublinks-federation/internal/lemmy" + "sublinks/federation/internal/lemmy" "time" ) diff --git a/internal/activitypub/user.go b/internal/activitypub/user.go index d8e1c22..be474a4 100644 --- a/internal/activitypub/user.go +++ b/internal/activitypub/user.go @@ -2,7 +2,7 @@ package activitypub import ( "fmt" - "participating-online/sublinks-federation/internal/lemmy" + "sublinks/federation/internal/lemmy" "time" ) diff --git a/internal/http/routes/activity.go b/internal/http/routes/activity.go index 5f156da..3fe7b9b 100644 --- a/internal/http/routes/activity.go +++ b/internal/http/routes/activity.go @@ -5,8 +5,8 @@ import ( "encoding/json" "log" "net/http" - "participating-online/sublinks-federation/internal/activitypub" - "participating-online/sublinks-federation/internal/lemmy" + "sublinks/federation/internal/activitypub" + "sublinks/federation/internal/lemmy" "fmt" diff --git a/internal/http/routes/post.go b/internal/http/routes/post.go index 4410f9b..042c4d5 100644 --- a/internal/http/routes/post.go +++ b/internal/http/routes/post.go @@ -5,8 +5,8 @@ import ( "encoding/json" "log" "net/http" - "participating-online/sublinks-federation/internal/activitypub" - "participating-online/sublinks-federation/internal/lemmy" + "sublinks/federation/internal/activitypub" + "sublinks/federation/internal/lemmy" "github.com/gorilla/mux" ) diff --git a/internal/http/routes/user.go b/internal/http/routes/user.go index 526bfb3..29fea79 100644 --- a/internal/http/routes/user.go +++ b/internal/http/routes/user.go @@ -6,8 +6,8 @@ import ( "fmt" "log" "net/http" - "participating-online/sublinks-federation/internal/activitypub" - "participating-online/sublinks-federation/internal/lemmy" + "sublinks/federation/internal/activitypub" + "sublinks/federation/internal/lemmy" "github.com/gorilla/mux" ) diff --git a/internal/http/server.go b/internal/http/server.go index 4524287..86b7081 100644 --- a/internal/http/server.go +++ b/internal/http/server.go @@ -7,7 +7,7 @@ import ( "net/http" "os" "os/signal" - "participating-online/sublinks-federation/internal/http/routes" + "sublinks/federation/internal/http/routes" "time" ) From 58e8f25c744901abeaa7758780c7c75da03a851c Mon Sep 17 00:00:00 2001 From: Joe Constant Date: Sun, 10 Dec 2023 19:27:44 -0700 Subject: [PATCH 2/6] Added logging using zerolog --- go.mod | 7 +++++++ go.sum | 15 +++++++++++++++ internal/http/routes/activity.go | 6 +++--- internal/http/routes/post.go | 4 ++-- internal/http/routes/user.go | 6 +++--- internal/http/server.go | 6 +++--- internal/logging/logger.go | 17 +++++++++++++++++ 7 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 internal/logging/logger.go diff --git a/go.mod b/go.mod index 0be337f..7315a21 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,12 @@ go 1.21.5 require ( github.com/gorilla/mux v1.8.1 + github.com/rs/zerolog v1.31.0 golang.org/x/text v0.14.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 eb8c2be..79eea1f 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,19 @@ +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/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +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.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A= +github.com/rs/zerolog v1.31.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= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= diff --git a/internal/http/routes/activity.go b/internal/http/routes/activity.go index 3fe7b9b..7210bf1 100644 --- a/internal/http/routes/activity.go +++ b/internal/http/routes/activity.go @@ -3,10 +3,10 @@ package routes import ( "context" "encoding/json" - "log" "net/http" "sublinks/federation/internal/activitypub" "sublinks/federation/internal/lemmy" + "sublinks/federation/internal/logging/logger" "fmt" @@ -27,7 +27,7 @@ func getActivityHandler(w http.ResponseWriter, r *http.Request) { case "create": obj, err := GetPostActivityObject(vars["id"]) if err != nil { - log.Println("Error reading object", err) + logger.GetLogger().Println("Error reading object", err) w.WriteHeader(http.StatusInternalServerError) return } @@ -59,7 +59,7 @@ func GetPostActivityObject(id string) (*activitypub.Post, error) { c := lemmy.GetLemmyClient(ctx) post, err := c.GetPost(ctx, id) if err != nil { - log.Println("Error reading post", err) + logger.GetLogger().Println("Error reading post", err) return nil, err } return activitypub.ConvertPostToApub(post), nil diff --git a/internal/http/routes/post.go b/internal/http/routes/post.go index 042c4d5..3a4aef8 100644 --- a/internal/http/routes/post.go +++ b/internal/http/routes/post.go @@ -3,10 +3,10 @@ package routes import ( "context" "encoding/json" - "log" "net/http" "sublinks/federation/internal/activitypub" "sublinks/federation/internal/lemmy" + "sublinks/federation/internal/logging/logger" "github.com/gorilla/mux" ) @@ -21,7 +21,7 @@ func getPostHandler(w http.ResponseWriter, r *http.Request) { c := lemmy.GetLemmyClient(ctx) post, err := c.GetPost(ctx, vars["postId"]) if err != nil { - log.Println("Error reading post", err) + logger.GetLogger().Println("Error reading post", err) return } postLd := activitypub.ConvertPostToApub(post) diff --git a/internal/http/routes/user.go b/internal/http/routes/user.go index 29fea79..f62e425 100644 --- a/internal/http/routes/user.go +++ b/internal/http/routes/user.go @@ -4,10 +4,10 @@ import ( "context" "encoding/json" "fmt" - "log" "net/http" "sublinks/federation/internal/activitypub" "sublinks/federation/internal/lemmy" + "sublinks/federation/internal/logging/logger" "github.com/gorilla/mux" ) @@ -20,10 +20,10 @@ func getUserInfoHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) ctx := context.Background() c := lemmy.GetLemmyClient(ctx) - log.Println(fmt.Sprintf("Looking up user %s", vars["user"])) + logger.GetLogger().Println(fmt.Sprintf("Looking up user %s", vars["user"])) user, err := c.GetUser(ctx, vars["user"]) if err != nil { - log.Println("Error reading user", err) + logger.GetLogger().Println("Error reading user", err) return } diff --git a/internal/http/server.go b/internal/http/server.go index 86b7081..ac81077 100644 --- a/internal/http/server.go +++ b/internal/http/server.go @@ -3,11 +3,11 @@ package http import ( "context" "flag" - "log" "net/http" "os" "os/signal" "sublinks/federation/internal/http/routes" + "sublinks/federation/internal/logging/logger" "time" ) @@ -30,7 +30,7 @@ func RunServer() { // Run our server in a goroutine so that it doesn't block. go func() { if err := srv.ListenAndServe(); err != nil { - log.Println(err) + logger.getLogger().Println(err) } }() @@ -51,5 +51,5 @@ func RunServer() { // Optionally, you could run srv.Shutdown in a goroutine and block on // <-ctx.Done() if your application should wait for other services // to finalize based on context cancellation. - log.Println("shutting down") + logger.getLogger().Println("shutting down") } diff --git a/internal/logging/logger.go b/internal/logging/logger.go new file mode 100644 index 0000000..5102c24 --- /dev/null +++ b/internal/logging/logger.go @@ -0,0 +1,17 @@ +package logging + +import ( + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + "os" +) + +func init() { + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + log.Print("Logger started") +} + +func GetLogger() *zerolog.Logger { + return &zerolog.Logger +} From 34f0e4b15b2866860963e0b754bf3ad9b1098425 Mon Sep 17 00:00:00 2001 From: Joe Constant Date: Tue, 12 Dec 2023 03:04:26 +0000 Subject: [PATCH 3/6] Fix logging --- internal/http/routes/activity.go | 6 +++--- internal/http/routes/post.go | 4 ++-- internal/http/routes/user.go | 6 +++--- internal/http/server.go | 7 ++++--- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/internal/http/routes/activity.go b/internal/http/routes/activity.go index 7210bf1..76c4450 100644 --- a/internal/http/routes/activity.go +++ b/internal/http/routes/activity.go @@ -6,7 +6,7 @@ import ( "net/http" "sublinks/federation/internal/activitypub" "sublinks/federation/internal/lemmy" - "sublinks/federation/internal/logging/logger" + "sublinks/federation/internal/logging" "fmt" @@ -27,7 +27,7 @@ func getActivityHandler(w http.ResponseWriter, r *http.Request) { case "create": obj, err := GetPostActivityObject(vars["id"]) if err != nil { - logger.GetLogger().Println("Error reading object", err) + logging.Error("Error reading object", err) w.WriteHeader(http.StatusInternalServerError) return } @@ -59,7 +59,7 @@ func GetPostActivityObject(id string) (*activitypub.Post, error) { c := lemmy.GetLemmyClient(ctx) post, err := c.GetPost(ctx, id) if err != nil { - logger.GetLogger().Println("Error reading post", err) + logging.Error("Error reading post", err) return nil, err } return activitypub.ConvertPostToApub(post), nil diff --git a/internal/http/routes/post.go b/internal/http/routes/post.go index 3a4aef8..c52b0a0 100644 --- a/internal/http/routes/post.go +++ b/internal/http/routes/post.go @@ -6,7 +6,7 @@ import ( "net/http" "sublinks/federation/internal/activitypub" "sublinks/federation/internal/lemmy" - "sublinks/federation/internal/logging/logger" + "sublinks/federation/internal/logging" "github.com/gorilla/mux" ) @@ -21,7 +21,7 @@ func getPostHandler(w http.ResponseWriter, r *http.Request) { c := lemmy.GetLemmyClient(ctx) post, err := c.GetPost(ctx, vars["postId"]) if err != nil { - logger.GetLogger().Println("Error reading post", err) + logging.Error("Error reading post", err) return } postLd := activitypub.ConvertPostToApub(post) diff --git a/internal/http/routes/user.go b/internal/http/routes/user.go index f62e425..186c1c0 100644 --- a/internal/http/routes/user.go +++ b/internal/http/routes/user.go @@ -7,7 +7,7 @@ import ( "net/http" "sublinks/federation/internal/activitypub" "sublinks/federation/internal/lemmy" - "sublinks/federation/internal/logging/logger" + "sublinks/federation/internal/logging" "github.com/gorilla/mux" ) @@ -20,10 +20,10 @@ func getUserInfoHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) ctx := context.Background() c := lemmy.GetLemmyClient(ctx) - logger.GetLogger().Println(fmt.Sprintf("Looking up user %s", vars["user"])) + logging.Info(fmt.Sprintf("Looking up user %s", vars["user"])) user, err := c.GetUser(ctx, vars["user"]) if err != nil { - logger.GetLogger().Println("Error reading user", err) + logging.Error("Error reading user", err) return } diff --git a/internal/http/server.go b/internal/http/server.go index ac81077..fce86eb 100644 --- a/internal/http/server.go +++ b/internal/http/server.go @@ -7,7 +7,7 @@ import ( "os" "os/signal" "sublinks/federation/internal/http/routes" - "sublinks/federation/internal/logging/logger" + "sublinks/federation/internal/logging" "time" ) @@ -29,8 +29,9 @@ func RunServer() { // Run our server in a goroutine so that it doesn't block. go func() { + logging.Info("Starting server") if err := srv.ListenAndServe(); err != nil { - logger.getLogger().Println(err) + logging.Error("Error starting server", err) } }() @@ -51,5 +52,5 @@ func RunServer() { // Optionally, you could run srv.Shutdown in a goroutine and block on // <-ctx.Done() if your application should wait for other services // to finalize based on context cancellation. - logger.getLogger().Println("shutting down") + logging.Info("shutting down") } From 9d55fefd40f17b780b28bae844046b519fabb4b7 Mon Sep 17 00:00:00 2001 From: Joe Constant Date: Tue, 12 Dec 2023 03:05:01 +0000 Subject: [PATCH 4/6] Attach logging to router --- internal/http/routes/routes.go | 38 ++++++++++++++++++++- internal/logging/logger.go | 62 ++++++++++++++++++++++++++++++---- 2 files changed, 92 insertions(+), 8 deletions(-) diff --git a/internal/http/routes/routes.go b/internal/http/routes/routes.go index 6c2404a..902f52f 100644 --- a/internal/http/routes/routes.go +++ b/internal/http/routes/routes.go @@ -1,6 +1,12 @@ package routes -import "github.com/gorilla/mux" +import ( + "encoding/json" + "net/http" + "sublinks/federation/internal/logging" + + "github.com/gorilla/mux" +) func SetupRoutes() *mux.Router { r := mux.NewRouter() @@ -8,5 +14,35 @@ func SetupRoutes() *mux.Router { SetupPostRoutes(r) SetupApubRoutes(r) SetupActivityRoutes(r) + r.NotFoundHandler = http.HandlerFunc(notFound) + r.MethodNotAllowedHandler = http.HandlerFunc(notAllowedMethod) + r.Use(loggingMiddleware) return r } + +func loggingMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + logging.Request("", r) + next.ServeHTTP(w, r) + }) +} + +type RequestError struct { + Msg string `json:"message"` +} + +func notFound(w http.ResponseWriter, r *http.Request) { + logging.Request("404 Not Found", r) + w.WriteHeader(http.StatusNotFound) + w.Header().Add("content-type", "application/activity+json") + content, _ := json.Marshal(RequestError{Msg: "not found"}) + w.Write(content) +} + +func notAllowedMethod(w http.ResponseWriter, r *http.Request) { + logging.Request("405 Method Not Allowed", r) + w.WriteHeader(http.StatusNotFound) + w.Header().Add("content-type", "application/activity+json") + content, _ := json.Marshal(RequestError{Msg: "method not allowed"}) + w.Write(content) +} diff --git a/internal/logging/logger.go b/internal/logging/logger.go index 5102c24..f940900 100644 --- a/internal/logging/logger.go +++ b/internal/logging/logger.go @@ -1,17 +1,65 @@ package logging import ( - "github.com/rs/zerolog" + "encoding/json" + "io" + "net/http" + "github.com/rs/zerolog/log" - "os" ) func init() { - zerolog.TimeFieldFormat = zerolog.TimeFormatUnix - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) - log.Print("Logger started") + log.Debug().Msg("Logger started") +} + +func Info(msg string) { + log.Info().Msg(msg) +} + +func Debug(msg string) { + log.Debug().Msg(msg) +} + +func Error(msg string, err error) { + log.Error().Err(err).Msg(msg) +} + +func Warn(msg string) { + log.Warn().Msg(msg) } -func GetLogger() *zerolog.Logger { - return &zerolog.Logger +func Request(msg string, r *http.Request) { + defer r.Body.Close() + var body interface{} + rawbody, err := io.ReadAll(r.Body) + if err != nil { + Error("Error reading request body", err) + body = nil + } + if r.ContentLength > 0 && r.Header.Get("Content-Type") == "application/json" { + err = json.Unmarshal(rawbody, &body) + if err != nil { + Error("Error parsing request body into json", err) + body = nil + } + } + log.Debug(). + Str("method", r.Method). + Str("url", r.URL.String()). + Str("user-agent", r.UserAgent()). + Int64("content-length", r.ContentLength). + Str("ip", r.RemoteAddr). + Str("real-ip", r.Header.Get("X-Real-Ip")). + Str("dnt", r.Header.Get("Dnt")). + Str("host", r.Host). + Str("proto", r.Proto). + Str("referer", r.Referer()). + Str("accept", r.Header.Get("Accept")). + Str("accept-language", r.Header.Get("Accept-Language")). + Str("content-type", r.Header.Get("Content-Type")). + Str("forwarded-for", r.Header.Get("X-Forwarded-For")). + Any("body", body). + Str("user", r.URL.User.Username()). + Str("query", r.URL.Query().Encode()). + Msg(msg) } From 3804923a9d0cbdb1d55e7546ab1ee3e8c9f29be0 Mon Sep 17 00:00:00 2001 From: Joe Constant Date: Tue, 12 Dec 2023 03:10:26 +0000 Subject: [PATCH 5/6] Rename logging package to log --- internal/http/routes/activity.go | 6 +++--- internal/http/routes/post.go | 4 ++-- internal/http/routes/routes.go | 12 ++++++------ internal/http/routes/user.go | 6 +++--- internal/http/server.go | 8 ++++---- internal/{logging/logger.go => log/log.go} | 2 +- 6 files changed, 19 insertions(+), 19 deletions(-) rename internal/{logging/logger.go => log/log.go} (98%) diff --git a/internal/http/routes/activity.go b/internal/http/routes/activity.go index 76c4450..6b0fd11 100644 --- a/internal/http/routes/activity.go +++ b/internal/http/routes/activity.go @@ -6,7 +6,7 @@ import ( "net/http" "sublinks/federation/internal/activitypub" "sublinks/federation/internal/lemmy" - "sublinks/federation/internal/logging" + "sublinks/federation/internal/log" "fmt" @@ -27,7 +27,7 @@ func getActivityHandler(w http.ResponseWriter, r *http.Request) { case "create": obj, err := GetPostActivityObject(vars["id"]) if err != nil { - logging.Error("Error reading object", err) + log.Error("Error reading object", err) w.WriteHeader(http.StatusInternalServerError) return } @@ -59,7 +59,7 @@ func GetPostActivityObject(id string) (*activitypub.Post, error) { c := lemmy.GetLemmyClient(ctx) post, err := c.GetPost(ctx, id) if err != nil { - logging.Error("Error reading post", err) + log.Error("Error reading post", err) return nil, err } return activitypub.ConvertPostToApub(post), nil diff --git a/internal/http/routes/post.go b/internal/http/routes/post.go index c52b0a0..27382f8 100644 --- a/internal/http/routes/post.go +++ b/internal/http/routes/post.go @@ -6,7 +6,7 @@ import ( "net/http" "sublinks/federation/internal/activitypub" "sublinks/federation/internal/lemmy" - "sublinks/federation/internal/logging" + "sublinks/federation/internal/log" "github.com/gorilla/mux" ) @@ -21,7 +21,7 @@ func getPostHandler(w http.ResponseWriter, r *http.Request) { c := lemmy.GetLemmyClient(ctx) post, err := c.GetPost(ctx, vars["postId"]) if err != nil { - logging.Error("Error reading post", err) + log.Error("Error reading post", err) return } postLd := activitypub.ConvertPostToApub(post) diff --git a/internal/http/routes/routes.go b/internal/http/routes/routes.go index 902f52f..06b4a8e 100644 --- a/internal/http/routes/routes.go +++ b/internal/http/routes/routes.go @@ -3,7 +3,7 @@ package routes import ( "encoding/json" "net/http" - "sublinks/federation/internal/logging" + "sublinks/federation/internal/log" "github.com/gorilla/mux" ) @@ -16,13 +16,13 @@ func SetupRoutes() *mux.Router { SetupActivityRoutes(r) r.NotFoundHandler = http.HandlerFunc(notFound) r.MethodNotAllowedHandler = http.HandlerFunc(notAllowedMethod) - r.Use(loggingMiddleware) + r.Use(logMiddleware) return r } -func loggingMiddleware(next http.Handler) http.Handler { +func logMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - logging.Request("", r) + log.Request("", r) next.ServeHTTP(w, r) }) } @@ -32,7 +32,7 @@ type RequestError struct { } func notFound(w http.ResponseWriter, r *http.Request) { - logging.Request("404 Not Found", r) + log.Request("404 Not Found", r) w.WriteHeader(http.StatusNotFound) w.Header().Add("content-type", "application/activity+json") content, _ := json.Marshal(RequestError{Msg: "not found"}) @@ -40,7 +40,7 @@ func notFound(w http.ResponseWriter, r *http.Request) { } func notAllowedMethod(w http.ResponseWriter, r *http.Request) { - logging.Request("405 Method Not Allowed", r) + log.Request("405 Method Not Allowed", r) w.WriteHeader(http.StatusNotFound) w.Header().Add("content-type", "application/activity+json") content, _ := json.Marshal(RequestError{Msg: "method not allowed"}) diff --git a/internal/http/routes/user.go b/internal/http/routes/user.go index 186c1c0..baca9b1 100644 --- a/internal/http/routes/user.go +++ b/internal/http/routes/user.go @@ -7,7 +7,7 @@ import ( "net/http" "sublinks/federation/internal/activitypub" "sublinks/federation/internal/lemmy" - "sublinks/federation/internal/logging" + "sublinks/federation/internal/log" "github.com/gorilla/mux" ) @@ -20,10 +20,10 @@ func getUserInfoHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) ctx := context.Background() c := lemmy.GetLemmyClient(ctx) - logging.Info(fmt.Sprintf("Looking up user %s", vars["user"])) + log.Info(fmt.Sprintf("Looking up user %s", vars["user"])) user, err := c.GetUser(ctx, vars["user"]) if err != nil { - logging.Error("Error reading user", err) + log.Error("Error reading user", err) return } diff --git a/internal/http/server.go b/internal/http/server.go index fce86eb..4c5c0a9 100644 --- a/internal/http/server.go +++ b/internal/http/server.go @@ -7,7 +7,7 @@ import ( "os" "os/signal" "sublinks/federation/internal/http/routes" - "sublinks/federation/internal/logging" + "sublinks/federation/internal/log" "time" ) @@ -29,9 +29,9 @@ func RunServer() { // Run our server in a goroutine so that it doesn't block. go func() { - logging.Info("Starting server") + log.Info("Starting server") if err := srv.ListenAndServe(); err != nil { - logging.Error("Error starting server", err) + log.Error("Error starting server", err) } }() @@ -52,5 +52,5 @@ func RunServer() { // Optionally, you could run srv.Shutdown in a goroutine and block on // <-ctx.Done() if your application should wait for other services // to finalize based on context cancellation. - logging.Info("shutting down") + log.Info("shutting down") } diff --git a/internal/logging/logger.go b/internal/log/log.go similarity index 98% rename from internal/logging/logger.go rename to internal/log/log.go index f940900..d503f41 100644 --- a/internal/logging/logger.go +++ b/internal/log/log.go @@ -1,4 +1,4 @@ -package logging +package log import ( "encoding/json" From 5eead2adcf54ea43640ebb38aab625674877c4e8 Mon Sep 17 00:00:00 2001 From: Joe Constant Date: Tue, 12 Dec 2023 03:22:28 +0000 Subject: [PATCH 6/6] Log body when not json --- internal/log/log.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/log/log.go b/internal/log/log.go index d503f41..90a5217 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -42,6 +42,8 @@ func Request(msg string, r *http.Request) { Error("Error parsing request body into json", err) body = nil } + } else { + body = rawbody } log.Debug(). Str("method", r.Method).