Files

46 lines
2.2 KiB
Markdown
Raw Permalink Normal View History

2026-11-14 19:16:24 +05:00
# Logging
For logging, stdlib's `slog` package should be used as it gives everything you need for:
- Structured logging.
- Debug (with appending LoC where logger's call happened).
- Leveled logging.
When specially mentioned, other logging packages can be used, but approach to its usage and configuration should be the same.
## Configuring logger
Logger's configuration should be implemented with these things in mind:
- Logger's write destination is **always** stdout/stderr.
- Level can be obtained from configuration or environment variable.
- Level should be switchable at runtime.
- LoC where logging call happened should always be added to log line.
- Logging formatting should be configurable in same way as level.
- Logging formatters to choose from: JSON and some pretty and readable one (like default in `slog`).
## Base logger
Base logger is initialized in Application singleton which should have function named `Logger(fields ...any) T` where `fields` are additional fields described below and `T` is a logger's type that should be returned like `*slog.Logger`. Its initialization happens when `application.New()` is called to make it accessible even in `main()` function, so when configuration is loaded it should re-initialize or re-configure (depends on used logger) base and all created sub-loggers with new logging level.
## Using logger in different parts of application
Every package in application should have own logger's instance configured that is derived from base logger with extra fields appended by default. These fields are:
- module - name of module (taken from `DomainName*` constants from domain-specific data, see [Domains](domains.md)).
- type - type of submodule (e.g. "storage", "delivery", etc.).
- name - name of submodule (e.g. "postgresql", "external HTTP", etc.).
For example:
- Main configuration domain's service:
- module: "core/config"
- type: "domain"
- PostgreSQL storage in core's configuration domain should have these fields appended:
- module: "core/config"
- type: "storage"
- name: "postgresql"
- External HTTP delivery which processes client's configuration for core's configuration package:
- module: "core/config"
- type: "delivery"
- name: "external HTTP"