feat: improved logging with zerolog

This commit is contained in:
Nicolas Meienberger
2025-08-10 00:08:00 +02:00
parent d0bf890386
commit 18654426fa
14 changed files with 148 additions and 58 deletions

View File

@@ -2,13 +2,10 @@ package driver
import (
"encoding/json"
"log"
"net/http"
)
func Activate(w http.ResponseWriter, r *http.Request) {
log.Printf("Received activation request: %s", r.URL.Path)
resp := map[string]any{
"Implements": []string{"VolumeDriver"},
}

View File

@@ -2,25 +2,28 @@ package driver
import (
"encoding/json"
"ironmount/internal/constants"
"ironmount/internal/core"
"ironmount/internal/db"
"log"
"net/http"
"os"
"path/filepath"
"github.com/rs/zerolog/log"
)
func Create(w http.ResponseWriter, r *http.Request) {
log.Printf("Received create request: %s", r.URL.Path)
var req struct {
Name string
Opts map[string]string `json:"Opts,omitempty"`
}
_ = json.NewDecoder(r.Body).Decode(&req)
volPath := filepath.Join(constants.VolumeRoot, req.Name)
cfg := core.LoadConfig()
volPath := filepath.Join(cfg.VolumeRoot, req.Name)
log.Info().Str("path", volPath).Msg("Creating volume directory")
if err := os.MkdirAll(volPath, 0755); err != nil {
log.Error().Err(err).Str("path", volPath).Msg("Failed to create volume directory")
_ = json.NewEncoder(w).Encode(map[string]string{"Err": err.Error()})
return
}

View File

@@ -3,13 +3,12 @@ package driver
import (
"encoding/json"
"ironmount/internal/db"
"log"
"net/http"
"github.com/rs/zerolog/hlog"
)
func Get(w http.ResponseWriter, r *http.Request) {
log.Printf("Received get request: %s", r.URL.Path)
var req struct {
Name string
}
@@ -18,7 +17,7 @@ func Get(w http.ResponseWriter, r *http.Request) {
vol, err := db.GetVolumeByName(req.Name)
if err != nil {
log.Printf("Error retrieving volume: %s", err.Error())
hlog.FromRequest(r).Error().Err(err).Msg("Error retrieving volume")
response := map[string]string{
"Err": err.Error(),

View File

@@ -3,16 +3,15 @@ package driver
import (
"encoding/json"
"ironmount/internal/db"
"log"
"net/http"
"github.com/rs/zerolog/hlog"
)
func List(w http.ResponseWriter, r *http.Request) {
log.Printf("Received list request: %s", r.URL.Path)
volumes, err := db.ListVolumes()
if err != nil {
log.Printf("Error listing volumes: %s", err.Error())
hlog.FromRequest(r).Error().Err(err).Msg("Error listing volumes")
json.NewEncoder(w).Encode(map[string]any{
"Volumes": nil,
"Err": err.Error(),

View File

@@ -3,30 +3,34 @@ package driver
import (
"encoding/json"
"ironmount/internal/db"
"log"
"net/http"
"github.com/rs/zerolog/hlog"
"github.com/rs/zerolog/log"
)
func Mount(w http.ResponseWriter, r *http.Request) {
log.Printf("Received mount request: %s", r.URL.Path)
var req MountRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
hlog.FromRequest(r).Error().Err(err).Msg("Invalid request body")
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
vol, err := db.GetVolumeByName(req.Name)
if err != nil {
log.Printf("Error retrieving volume: %s", err.Error())
log.Error().Err(err).Str("volume", req.Name).Msg("Failed to get volume")
_ = json.NewEncoder(w).Encode(map[string]string{
"Err": err.Error(),
})
return
}
log.Info().Str("volume", vol.Name).Str("path", vol.Path).Msg("Mounting volume")
_ = json.NewEncoder(w).Encode(map[string]string{
"Mountpoint": vol.Path,
"Err": "",

View File

@@ -3,18 +3,18 @@ package driver
import (
"encoding/json"
"ironmount/internal/db"
"log"
"net/http"
"github.com/rs/zerolog/hlog"
)
func Path(w http.ResponseWriter, r *http.Request) {
log.Printf("Received path request: %s", r.URL.Path)
var req PathRequest
_ = json.NewDecoder(r.Body).Decode(&req)
vol, err := db.GetVolumeByName(req.Name)
if err != nil {
hlog.FromRequest(r).Error().Err(err).Msg("Error retrieving volume")
_ = json.NewEncoder(w).Encode(map[string]string{
"Err": err.Error(),
})

View File

@@ -3,20 +3,20 @@ package driver
import (
"encoding/json"
"ironmount/internal/db"
"log"
"net/http"
"os"
"github.com/rs/zerolog/hlog"
"github.com/rs/zerolog/log"
)
func Remove(w http.ResponseWriter, r *http.Request) {
log.Printf("Received remove request: %s", r.URL.Path)
var req RemoveRequest
_ = json.NewDecoder(r.Body).Decode(&req)
vol, err := db.GetVolumeByName(req.Name)
if err != nil {
log.Printf("Error retrieving volume: %s", err.Error())
hlog.FromRequest(r).Error().Err(err).Msg("Error retrieving volume")
_ = json.NewEncoder(w).Encode(map[string]string{
"Err": err.Error(),
})
@@ -24,6 +24,8 @@ func Remove(w http.ResponseWriter, r *http.Request) {
}
db.RemoveVolume(vol.Name)
log.Info().Str("path", vol.Path).Msg("Removing volume directory")
os.RemoveAll(vol.Path)
_ = json.NewEncoder(w).Encode(map[string]string{"Err": ""})

View File

@@ -2,12 +2,9 @@ package driver
import (
"encoding/json"
"log"
"net/http"
)
func Unmount(w http.ResponseWriter, r *http.Request) {
log.Printf("Received unmount request: %s", r.URL.Path)
_ = json.NewEncoder(w).Encode(map[string]string{"Err": ""})
}