mirror of
https://github.com/akyaiy/GoSally-mvp.git
synced 2026-01-07 02:32:24 +00:00
Refactor error handling and utility functions; remove deprecated code and improve logging
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
// Package logs provides a logger setup function that configures the logger based on the environment.
|
||||
// It supports different logging levels for development and production environments.
|
||||
// It uses the standard library's slog package for structured logging.
|
||||
package logs
|
||||
|
||||
import (
|
||||
@@ -5,11 +8,15 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// Environment constants for logger setup
|
||||
const (
|
||||
envDev = "dev"
|
||||
// envDev enables development logging with debug level
|
||||
envDev = "dev"
|
||||
// envProd enables production logging with info level
|
||||
envProd = "prod"
|
||||
)
|
||||
|
||||
// SetupLogger initializes and returns a logger based on the provided environment.
|
||||
func SetupLogger(env string) *slog.Logger {
|
||||
var log *slog.Logger
|
||||
switch env {
|
||||
|
||||
@@ -6,30 +6,20 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// MockHandler is a mock implementation of slog.Handler for testing purposes.
|
||||
type MockHandler struct {
|
||||
mu sync.Mutex
|
||||
mu sync.Mutex
|
||||
// Logs stores the log records captured by the handler.
|
||||
Logs []slog.Record
|
||||
}
|
||||
|
||||
func NewMockHandler() *MockHandler {
|
||||
return &MockHandler{}
|
||||
}
|
||||
|
||||
func (h *MockHandler) Enabled(_ context.Context, _ slog.Level) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func NewMockHandler() *MockHandler { return &MockHandler{} }
|
||||
func (h *MockHandler) Enabled(_ context.Context, _ slog.Level) bool { return true }
|
||||
func (h *MockHandler) WithAttrs(_ []slog.Attr) slog.Handler { return h }
|
||||
func (h *MockHandler) WithGroup(_ string) slog.Handler { return h }
|
||||
func (h *MockHandler) Handle(_ context.Context, r slog.Record) error {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.Logs = append(h.Logs, r.Clone())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *MockHandler) WithAttrs(_ []slog.Attr) slog.Handler {
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *MockHandler) WithGroup(_ string) slog.Handler {
|
||||
return h
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user