mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 23:44:22 +00:00
Harmonize logging
This commit is contained in:
@@ -10,10 +10,11 @@ import (
|
||||
// WithUserContextMiddleware extracts user information from JWT claims
|
||||
// and adds it to the request context
|
||||
func WithUserContextMiddleware(next http.Handler) http.Handler {
|
||||
log := getLogger()
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
claims, err := GetUserFromContext(r.Context())
|
||||
if err != nil {
|
||||
getLogger().Error("failed to get user from context",
|
||||
log.Error("failed to get user from context",
|
||||
"error", err,
|
||||
"path", r.URL.Path)
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
@@ -25,7 +26,7 @@ func WithUserContextMiddleware(next http.Handler) http.Handler {
|
||||
UserRole: claims.Role,
|
||||
}
|
||||
|
||||
getLogger().Debug("user context extracted from claims",
|
||||
log.Debug("user context extracted from claims",
|
||||
"userID", claims.UserID,
|
||||
"role", claims.Role,
|
||||
"path", r.URL.Path)
|
||||
@@ -38,6 +39,7 @@ func WithUserContextMiddleware(next http.Handler) http.Handler {
|
||||
// WithWorkspaceContextMiddleware adds workspace information to the request context
|
||||
func WithWorkspaceContextMiddleware(db db.WorkspaceReader) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
log := getLogger()
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, ok := GetRequestContext(w, r)
|
||||
if !ok {
|
||||
@@ -47,7 +49,7 @@ func WithWorkspaceContextMiddleware(db db.WorkspaceReader) func(http.Handler) ht
|
||||
workspaceName := chi.URLParam(r, "workspaceName")
|
||||
workspace, err := db.GetWorkspaceByName(ctx.UserID, workspaceName)
|
||||
if err != nil {
|
||||
getLogger().Error("failed to get workspace",
|
||||
log.Error("failed to get workspace",
|
||||
"error", err,
|
||||
"userID", ctx.UserID,
|
||||
"workspace", workspaceName,
|
||||
@@ -56,7 +58,7 @@ func WithWorkspaceContextMiddleware(db db.WorkspaceReader) func(http.Handler) ht
|
||||
return
|
||||
}
|
||||
|
||||
getLogger().Debug("workspace context added",
|
||||
log.Debug("workspace context added",
|
||||
"userID", ctx.UserID,
|
||||
"workspaceID", workspace.ID,
|
||||
"workspaceName", workspace.Name,
|
||||
|
||||
@@ -79,7 +79,8 @@ func New(url, username, token, workDir, commitName, commitEmail string) Client {
|
||||
|
||||
// Clone clones the Git repository to the local directory
|
||||
func (c *client) Clone() error {
|
||||
getLogger().Info("cloning git repository",
|
||||
log := getLogger()
|
||||
log.Info("cloning git repository",
|
||||
"url", c.URL,
|
||||
"workDir", c.WorkDir)
|
||||
|
||||
@@ -98,7 +99,7 @@ func (c *client) Clone() error {
|
||||
return fmt.Errorf("failed to clone repository: %w", err)
|
||||
}
|
||||
|
||||
getLogger().Info("repository cloned",
|
||||
log.Info("repository cloned",
|
||||
"url", c.URL,
|
||||
"workDir", c.WorkDir)
|
||||
return nil
|
||||
@@ -106,11 +107,13 @@ func (c *client) Clone() error {
|
||||
|
||||
// Pull pulls the latest changes from the remote repository
|
||||
func (c *client) Pull() error {
|
||||
log := getLogger()
|
||||
|
||||
if c.repo == nil {
|
||||
return fmt.Errorf("repository not initialized")
|
||||
}
|
||||
|
||||
getLogger().Debug("pulling repository changes",
|
||||
log.Debug("pulling repository changes",
|
||||
"workDir", c.WorkDir)
|
||||
|
||||
w, err := c.repo.Worktree()
|
||||
@@ -132,10 +135,10 @@ func (c *client) Pull() error {
|
||||
}
|
||||
|
||||
if err == git.NoErrAlreadyUpToDate {
|
||||
getLogger().Debug("repository already up to date",
|
||||
log.Debug("repository already up to date",
|
||||
"workDir", c.WorkDir)
|
||||
} else {
|
||||
getLogger().Info("pulled repository changes",
|
||||
log.Info("pulled repository changes",
|
||||
"workDir", c.WorkDir)
|
||||
}
|
||||
return nil
|
||||
@@ -143,11 +146,13 @@ func (c *client) Pull() error {
|
||||
|
||||
// Commit commits the changes in the repository with the given message
|
||||
func (c *client) Commit(message string) (CommitHash, error) {
|
||||
log := getLogger()
|
||||
|
||||
if c.repo == nil {
|
||||
return CommitHash(plumbing.ZeroHash), fmt.Errorf("repository not initialized")
|
||||
}
|
||||
|
||||
getLogger().Debug("preparing to commit changes",
|
||||
log.Debug("preparing to commit changes",
|
||||
"workDir", c.WorkDir,
|
||||
"message", message)
|
||||
|
||||
@@ -172,7 +177,7 @@ func (c *client) Commit(message string) (CommitHash, error) {
|
||||
return CommitHash(plumbing.ZeroHash), fmt.Errorf("failed to commit changes: %w", err)
|
||||
}
|
||||
|
||||
getLogger().Info("changes committed",
|
||||
log.Info("changes committed",
|
||||
"hash", hash.String(),
|
||||
"workDir", c.WorkDir,
|
||||
"message", message)
|
||||
@@ -181,11 +186,13 @@ func (c *client) Commit(message string) (CommitHash, error) {
|
||||
|
||||
// Push pushes the changes to the remote repository
|
||||
func (c *client) Push() error {
|
||||
log := getLogger()
|
||||
|
||||
if c.repo == nil {
|
||||
return fmt.Errorf("repository not initialized")
|
||||
}
|
||||
|
||||
getLogger().Debug("pushing repository changes",
|
||||
log.Debug("pushing repository changes",
|
||||
"workDir", c.WorkDir)
|
||||
|
||||
auth := &http.BasicAuth{
|
||||
@@ -202,10 +209,10 @@ func (c *client) Push() error {
|
||||
}
|
||||
|
||||
if err == git.NoErrAlreadyUpToDate {
|
||||
getLogger().Debug("remote already up to date",
|
||||
log.Debug("remote already up to date",
|
||||
"workDir", c.WorkDir)
|
||||
} else {
|
||||
getLogger().Info("pushed repository changes",
|
||||
log.Info("pushed repository changes",
|
||||
"workDir", c.WorkDir)
|
||||
}
|
||||
return nil
|
||||
@@ -213,11 +220,13 @@ func (c *client) Push() error {
|
||||
|
||||
// EnsureRepo ensures the local repository is cloned and up-to-date
|
||||
func (c *client) EnsureRepo() error {
|
||||
getLogger().Debug("ensuring repository exists and is up to date",
|
||||
log := getLogger()
|
||||
|
||||
log.Debug("ensuring repository exists and is up to date",
|
||||
"workDir", c.WorkDir)
|
||||
|
||||
if _, err := os.Stat(filepath.Join(c.WorkDir, ".git")); os.IsNotExist(err) {
|
||||
getLogger().Info("repository not found, initiating clone",
|
||||
log.Info("repository not found, initiating clone",
|
||||
"workDir", c.WorkDir)
|
||||
return c.Clone()
|
||||
}
|
||||
@@ -228,7 +237,7 @@ func (c *client) EnsureRepo() error {
|
||||
return fmt.Errorf("failed to open existing repository: %w", err)
|
||||
}
|
||||
|
||||
getLogger().Debug("repository opened, pulling latest changes",
|
||||
log.Debug("repository opened, pulling latest changes",
|
||||
"workDir", c.WorkDir)
|
||||
return c.Pull()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user