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