mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
feat: improved logging with zerolog
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
package constants
|
||||
|
||||
var VolumeRoot = "/home/nicolas/ironmount/tmp"
|
||||
var VolumeRoot = "/tmp/ironmount"
|
||||
|
||||
26
internal/core/log.go
Normal file
26
internal/core/log.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
stdlog "log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func init() {
|
||||
console := zerolog.ConsoleWriter{
|
||||
Out: os.Stderr,
|
||||
TimeFormat: time.ANSIC,
|
||||
}
|
||||
|
||||
logger := zerolog.New(console).With().Timestamp().Caller().Logger()
|
||||
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
|
||||
log.Logger = logger
|
||||
|
||||
stdlog.SetFlags(0)
|
||||
stdlog.SetOutput(console)
|
||||
}
|
||||
@@ -2,8 +2,8 @@ package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
@@ -59,10 +59,9 @@ func CreateVolume(name, path string) error {
|
||||
func RemoveVolume(name string) error {
|
||||
_, err := DB.Exec("DELETE FROM volumes WHERE name = ?", name)
|
||||
|
||||
log.Printf("Removing volume: %s", name)
|
||||
log.Printf("Error removing volume: %v", err)
|
||||
|
||||
log.Info().Str("volume", name).Msg("Removing volume")
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("volume", name).Msg("Error removing volume")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -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"},
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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": "",
|
||||
|
||||
@@ -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(),
|
||||
})
|
||||
|
||||
@@ -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": ""})
|
||||
|
||||
@@ -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": ""})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user