Skip to content

Commit

Permalink
Merge pull request #516 from jmpsec/zerolog-osctrl-api
Browse files Browse the repository at this point in the history
Migrating native log to zerolog in osctrl-api
  • Loading branch information
javuto authored Sep 24, 2024
2 parents f346bcc + 8058973 commit 9cda55a
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 82 deletions.
4 changes: 2 additions & 2 deletions api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package main

import (
"context"
"log"
"net/http"
"strings"

"github.com/jmpsec/osctrl/settings"
"github.com/jmpsec/osctrl/utils"
"github.com/rs/zerolog/log"
)

// contextValue to hold session data in the context
Expand Down Expand Up @@ -70,7 +70,7 @@ func handlerAuthCheck(h http.Handler) http.Handler {
}
// Update metadata for the user
if err := apiUsers.UpdateTokenIPAddress(utils.GetIP(r), claims.Username); err != nil {
log.Printf("error updating token for user %s: %v", claims.Username, err)
log.Err(err).Msgf("error updating token for user %s", claims.Username)
}
// Set middleware values
s := make(contextValue)
Expand Down
4 changes: 2 additions & 2 deletions api/handlers/carves.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package handlers
import (
"encoding/json"
"fmt"
"log"
"net/http"

"github.com/jmpsec/osctrl/carves"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/jmpsec/osctrl/types"
"github.com/jmpsec/osctrl/users"
"github.com/jmpsec/osctrl/utils"
"github.com/rs/zerolog/log"
)

// GET Handler to return a single carve in JSON
Expand Down Expand Up @@ -59,7 +59,7 @@ func (h *HandlersApi) CarveShowHandler(w http.ResponseWriter, r *http.Request) {
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Printf("DebugService: Returned carve %s", name)
log.Debug().Msgf("DebugService: Returned carve %s", name)
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, carve)
h.Inc(metricAPICarvesOK)
Expand Down
10 changes: 5 additions & 5 deletions api/handlers/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package handlers
import (
"encoding/json"
"fmt"
"log"
"net/http"

"github.com/jmpsec/osctrl/environments"
"github.com/jmpsec/osctrl/settings"
"github.com/jmpsec/osctrl/types"
"github.com/jmpsec/osctrl/users"
"github.com/jmpsec/osctrl/utils"
"github.com/rs/zerolog/log"
)

// EnvironmentHandler - GET Handler to return one environment as JSON
Expand Down Expand Up @@ -44,7 +44,7 @@ func (h *HandlersApi) EnvironmentHandler(w http.ResponseWriter, r *http.Request)
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Printf("DebugService: Returned environment %s", env.Name)
log.Debug().Msgf("DebugService: Returned environment %s", env.Name)
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, env)
h.Inc(metricAPIEnvsOK)
Expand All @@ -70,7 +70,7 @@ func (h *HandlersApi) EnvironmentsHandler(w http.ResponseWriter, r *http.Request
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Println("DebugService: Returned environments")
log.Debug().Msg("DebugService: Returned environments")
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, envAll)
h.Inc(metricAPIEnvsOK)
Expand Down Expand Up @@ -141,7 +141,7 @@ func (h *HandlersApi) EnvEnrollHandler(w http.ResponseWriter, r *http.Request) {
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Printf("DebugService: Returned environment %s", returnData)
log.Debug().Msgf("DebugService: Returned environment %s", returnData)
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, types.ApiDataResponse{Data: returnData})
h.Inc(metricAPIEnvsOK)
Expand Down Expand Up @@ -206,7 +206,7 @@ func (h *HandlersApi) EnvRemoveHandler(w http.ResponseWriter, r *http.Request) {
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Printf("DebugService: Returned environment %s", types.ApiDataResponse{Data: returnData})
log.Debug().Msgf("DebugService: Returned environment %s", types.ApiDataResponse{Data: returnData})
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, env)
h.Inc(metricAPIEnvsOK)
Expand Down
4 changes: 2 additions & 2 deletions api/handlers/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package handlers
import (
"encoding/json"
"fmt"
"log"
"net/http"

"github.com/jmpsec/osctrl/settings"
"github.com/jmpsec/osctrl/types"
"github.com/jmpsec/osctrl/users"
"github.com/jmpsec/osctrl/utils"
"github.com/rs/zerolog/log"
)

// LoginHandler - POST Handler for API login request
Expand Down Expand Up @@ -67,7 +67,7 @@ func (h *HandlersApi) LoginHandler(w http.ResponseWriter, r *http.Request) {
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Printf("DebugService: Returning token for %s", user.Username)
log.Debug().Msgf("DebugService: Returning token for %s", user.Username)
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, types.ApiLoginResponse{Token: user.APIToken})
h.Inc(metricAPILoginOK)
Expand Down
12 changes: 6 additions & 6 deletions api/handlers/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package handlers
import (
"encoding/json"
"fmt"
"log"
"net/http"

"github.com/jmpsec/osctrl/nodes"
"github.com/jmpsec/osctrl/settings"
"github.com/jmpsec/osctrl/types"
"github.com/jmpsec/osctrl/users"
"github.com/jmpsec/osctrl/utils"
"github.com/rs/zerolog/log"
)

// NodeHandler - GET Handler for single JSON nodes
Expand Down Expand Up @@ -59,7 +59,7 @@ func (h *HandlersApi) NodeHandler(w http.ResponseWriter, r *http.Request) {
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Printf("DebugService: Returned node %s", nodeVar)
log.Debug().Msgf("DebugService: Returned node %s", nodeVar)
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, node)
h.Inc(metricAPINodesOK)
Expand Down Expand Up @@ -104,7 +104,7 @@ func (h *HandlersApi) ActiveNodesHandler(w http.ResponseWriter, r *http.Request)
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Println("DebugService: Returned nodes")
log.Debug().Msg("DebugService: Returned nodes")
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, nodes)
h.Inc(metricAPINodesOK)
Expand Down Expand Up @@ -149,7 +149,7 @@ func (h *HandlersApi) InactiveNodesHandler(w http.ResponseWriter, r *http.Reques
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Println("DebugService: Returned nodes")
log.Debug().Msg("DebugService: Returned nodes")
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, nodes)
h.Inc(metricAPINodesOK)
Expand Down Expand Up @@ -194,7 +194,7 @@ func (h *HandlersApi) AllNodesHandler(w http.ResponseWriter, r *http.Request) {
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Println("DebugService: Returned nodes")
log.Debug().Msg("DebugService: Returned nodes")
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, nodes)
h.Inc(metricAPINodesOK)
Expand Down Expand Up @@ -243,7 +243,7 @@ func (h *HandlersApi) DeleteNodeHandler(w http.ResponseWriter, r *http.Request)
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Printf("DebugService: Returned node %s", n.UUID)
log.Debug().Msgf("DebugService: Returned node %s", n.UUID)
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, types.ApiGenericResponse{Message: "node deleted"})
h.Inc(metricAPINodesOK)
Expand Down
6 changes: 3 additions & 3 deletions api/handlers/platforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package handlers

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

"github.com/jmpsec/osctrl/settings"
"github.com/jmpsec/osctrl/users"
"github.com/jmpsec/osctrl/utils"
"github.com/rs/zerolog/log"
)

// PlatformsHandler - GET Handler for multiple JSON platforms
Expand All @@ -30,7 +30,7 @@ func (h *HandlersApi) PlatformsHandler(w http.ResponseWriter, r *http.Request) {
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Println("DebugService: Returned platforms")
log.Debug().Msg("DebugService: Returned platforms")
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, platforms)
h.Inc(metricAPIPlatformsOK)
Expand Down Expand Up @@ -74,7 +74,7 @@ func (h *HandlersApi) PlatformsEnvHandler(w http.ResponseWriter, r *http.Request
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Println("DebugService: Returned platforms")
log.Debug().Msg("DebugService: Returned platforms")
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, platforms)
h.Inc(metricAPIPlatformsOK)
Expand Down
4 changes: 2 additions & 2 deletions api/handlers/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package handlers
import (
"encoding/json"
"fmt"
"log"
"net/http"

"github.com/jmpsec/osctrl/queries"
"github.com/jmpsec/osctrl/settings"
"github.com/jmpsec/osctrl/types"
"github.com/jmpsec/osctrl/users"
"github.com/jmpsec/osctrl/utils"
"github.com/rs/zerolog/log"
)

// QueryShowHandler - GET Handler to return a single query in JSON
Expand Down Expand Up @@ -58,7 +58,7 @@ func (h *HandlersApi) QueryShowHandler(w http.ResponseWriter, r *http.Request) {
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Printf("DebugService: Returned query %s", name)
log.Debug().Msgf("DebugService: Returned query %s", name)
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, query)
h.Inc(metricAPIQueriesOK)
Expand Down
12 changes: 6 additions & 6 deletions api/handlers/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package handlers

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

"github.com/jmpsec/osctrl/settings"
"github.com/jmpsec/osctrl/users"
"github.com/jmpsec/osctrl/utils"
"github.com/rs/zerolog/log"
)

// SettingsHandler - GET Handler for all settings including JSON
Expand All @@ -30,7 +30,7 @@ func (h *HandlersApi) SettingsHandler(w http.ResponseWriter, r *http.Request) {
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Println("DebugService: Returned settings")
log.Debug().Msg("DebugService: Returned settings")
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, serviceSettings)
h.Inc(metricAPISettingsOK)
Expand Down Expand Up @@ -69,7 +69,7 @@ func (h *HandlersApi) SettingsServiceHandler(w http.ResponseWriter, r *http.Requ
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Println("DebugService: Returned settings")
log.Debug().Msg("DebugService: Returned settings")
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, serviceSettings)
h.Inc(metricAPISettingsOK)
Expand Down Expand Up @@ -126,7 +126,7 @@ func (h *HandlersApi) SettingsServiceEnvHandler(w http.ResponseWriter, r *http.R
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Println("DebugService: Returned settings")
log.Debug().Msg("DebugService: Returned settings")
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, serviceSettings)
h.Inc(metricAPISettingsOK)
Expand Down Expand Up @@ -165,7 +165,7 @@ func (h *HandlersApi) SettingsServiceJSONHandler(w http.ResponseWriter, r *http.
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Println("DebugService: Returned settings")
log.Debug().Msg("DebugService: Returned settings")
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, serviceSettings)
h.Inc(metricAPISettingsOK)
Expand Down Expand Up @@ -222,7 +222,7 @@ func (h *HandlersApi) SettingsServiceEnvJSONHandler(w http.ResponseWriter, r *ht
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Println("DebugService: Returned settings")
log.Debug().Msg("DebugService: Returned settings")
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, serviceSettings)
h.Inc(metricAPISettingsOK)
Expand Down
6 changes: 3 additions & 3 deletions api/handlers/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package handlers

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

"github.com/jmpsec/osctrl/settings"
"github.com/jmpsec/osctrl/users"
"github.com/jmpsec/osctrl/utils"
"github.com/rs/zerolog/log"
)

// TagsHandler - GET Handler for multiple JSON tags
Expand All @@ -30,7 +30,7 @@ func (h *HandlersApi) AllTagsHandler(w http.ResponseWriter, r *http.Request) {
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Println("DebugService: Returned tags")
log.Debug().Msg("DebugService: Returned tags")
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, tags)
h.Inc(metricAPITagsOK)
Expand Down Expand Up @@ -74,7 +74,7 @@ func (h *HandlersApi) TagsEnvHandler(w http.ResponseWriter, r *http.Request) {
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Println("DebugService: Returned tags")
log.Debug().Msg("DebugService: Returned tags")
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, tags)
h.Inc(metricAPITagsOK)
Expand Down
6 changes: 3 additions & 3 deletions api/handlers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package handlers

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

"github.com/jmpsec/osctrl/settings"
"github.com/jmpsec/osctrl/users"
"github.com/jmpsec/osctrl/utils"
"github.com/rs/zerolog/log"
)

// UserHandler - GET Handler for single JSON nodes
Expand Down Expand Up @@ -37,7 +37,7 @@ func (h *HandlersApi) UserHandler(w http.ResponseWriter, r *http.Request) {
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Printf("DebugService: Returned user %s", usernameVar)
log.Debug().Msgf("DebugService: Returned user %s", usernameVar)
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, user)
h.Inc(metricAPIUsersOK)
Expand Down Expand Up @@ -68,7 +68,7 @@ func (h *HandlersApi) UsersHandler(w http.ResponseWriter, r *http.Request) {
}
// Serialize and serve JSON
if h.Settings.DebugService(settings.ServiceAPI) {
log.Println("DebugService: Returned users")
log.Debug().Msg("DebugService: Returned users")
}
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, users)
h.Inc(metricAPIUsersOK)
Expand Down
4 changes: 2 additions & 2 deletions api/handlers/utils.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package handlers

import (
"log"
"net/http"

"github.com/jmpsec/osctrl/logging"
"github.com/jmpsec/osctrl/types"
"github.com/jmpsec/osctrl/utils"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -40,7 +40,7 @@ func postgresQueryLogs(db *gorm.DB, name string) (APIQueryData, error) {

// Helper to handle API error responses
func apiErrorResponse(w http.ResponseWriter, msg string, code int, err error) {
log.Printf("apiErrorResponse %s: %v", msg, err)
log.Debug().Msgf("apiErrorResponse %s: %v", msg, err)
utils.HTTPResponse(w, utils.JSONApplicationUTF8, code, types.ApiErrorResponse{Error: msg})
}

Expand Down
Loading

0 comments on commit 9cda55a

Please sign in to comment.