Logger
The logger package provides a structured logger used internally by ss-keel-core and accessible from your application code.
Import
Section titled “Import”import "github.com/slice-soft/ss-keel-core/logger"Creating a logger
Section titled “Creating a logger”// Automatically selects format based on environmentlog := logger.NewLogger(isProduction bool)
// Explicit formatlog := logger.NewLoggerWithFormat(isProduction bool, format logger.LogFormat)| Parameter | false (development) | true (production) |
|---|---|---|
| Format | Text | JSON |
| Debug logs | Enabled | Disabled |
// Developmentlog := logger.NewLogger(false)
// Productionlog := logger.NewLogger(true)
// Force JSON in development (e.g. for log aggregation)log := logger.NewLoggerWithFormat(false, logger.LogFormatJSON)Format constants
Section titled “Format constants”logger.LogFormatText // "text"logger.LogFormatJSON // "json"Log levels
Section titled “Log levels”General informational messages.
log.Info("Server started on port %d", 3000)log.Info("User %s registered", userID)Text output:
[INFO] 2024-01-15 10:23:45 Server started on port 3000JSON output:
{"level":"info","time":"2024-01-15T10:23:45Z","message":"Server started on port 3000"}Detailed diagnostic output. Disabled in production.
log.Debug("Cache miss for key %s", key)log.Debug("SQL: %s | args: %v", query, args)Non-fatal conditions that deserve attention.
log.Warn("Rate limit approaching for IP %s", ip)log.Warn("Slow query detected: %dms", duration.Milliseconds())// Only at startup, for fatal configuration errors:db, err := sql.Open("postgres", dsn)if err != nil { log.Error("database connection failed: %v", err) // process terminates here}Custom writer
Section titled “Custom writer”Redirect output to any io.Writer; useful for tests or sending to aggregators:
// Write to filef, _ := os.OpenFile("app.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)log := logger.NewLogger(true).WithWriter(f)
// Capture in testsvar buf bytes.Bufferlog := logger.NewLogger(false).WithWriter(&buf)WithWriter returns a new Logger (does not mutate the original).
Accessing the App logger
Section titled “Accessing the App logger”App exposes the logger it uses internally:
app := core.New(cfg)log := app.Logger()
log.Info("Starting background worker")log.Debug("Worker config: %+v", workerCfg)Pass it to modules and services:
func (m *UserModule) Register(app *core.App) { log := app.Logger() service := NewUserService(repo, log) app.RegisterController(NewUserController(service))}Automatic request logging
Section titled “Automatic request logging”ss-keel-core logs every HTTP request automatically. You don’t need to configure it: it’s already in the Fiber middleware stack created by core.New().
Example (text format):
[INFO] 2024-01-15 10:23:45 GET /users 200 1.2ms[INFO] 2024-01-15 10:23:46 POST /users 201 3.4ms[WARN] 2024-01-15 10:23:47 GET /users/999 404 0.8msExample (JSON format):
{"level":"info","time":"2024-01-15T10:23:45Z","method":"GET","path":"/users","status":200,"duration":"1.2ms"}{"level":"warn","time":"2024-01-15T10:23:47Z","method":"GET","path":"/users/999","status":404,"duration":"0.8ms"}Usage in services
Section titled “Usage in services”type UserService struct { repo UserRepository log *logger.Logger}
func NewUserService(repo UserRepository, log *logger.Logger) *UserService { return &UserService{repo: repo, log: log}}
func (s *UserService) Create(ctx context.Context, dto *CreateUserDTO) (*User, error) { user, err := s.repo.Create(ctx, dto) if err != nil { s.log.Warn("user creation failed: %v", err) return nil, core.Internal("user creation failed", err) } s.log.Info("user created: %s", user.ID) return user, nil}