Skip to content

Logger

The logger package provides a structured logger used internally by ss-keel-core and accessible from your application code.

import "github.com/slice-soft/ss-keel-core/logger"
// Auto-selects format based on environment
log := logger.NewLogger(isProduction bool)
// Explicit format
log := logger.NewLoggerWithFormat(isProduction bool, format logger.LogFormat)
Parameterfalse (development)true (production)
FormatTextJSON
Debug logsEnabledDisabled
// Development
log := logger.NewLogger(false)
// Production
log := logger.NewLogger(true)
// Force JSON in development (e.g. for log aggregation)
log := logger.NewLoggerWithFormat(false, logger.LogFormatJSON)
logger.LogFormatText // "text"
logger.LogFormatJSON // "json"

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 3000

JSON 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 worth 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("failed to connect to database: %v", err)
// process exits here
}

Redirect output to any io.Writer — useful for testing or forwarding to a log aggregator:

// Write to a file
f, _ := os.OpenFile("app.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
log := logger.NewLogger(true).WithWriter(f)
// Capture in tests
var buf bytes.Buffer
log := logger.NewLogger(false).WithWriter(&buf)

WithWriter returns a new Logger — the original is not modified.

The 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))
}

ss-keel-core automatically logs every HTTP request. You don’t need to configure this — it’s included in the Fiber middleware stack set up by core.New().

Example output (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.8ms

Example output (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"}
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("failed to create user: %v", err)
return nil, core.Internal("failed to create user", err)
}
s.log.Info("user created: %s", user.ID)
return user, nil
}