Logging service¶
consortium.server.services.logging_service
¶
SinkInfo(handler_id, sink, level, label, is_server_default, sink_kwargs=dict())
dataclass
¶
LoggingService()
¶
logging_config = LoggingConfigModel()
instance-attribute
¶
add_sink(sink, level, label, format=None, is_server_default=False, **kwargs)
¶
Adds a new log sink to the logging service.
All additional keyword arguments are forwarded directly to loguru.logger.add.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sink
|
Any
|
The sink target, for example |
required |
level
|
str
|
The minimum log level for this sink, for example |
required |
label
|
str
|
A unique label for identifying and referencing this sink. |
required |
format
|
str | Callable[[Any], str] | None
|
A loguru format string or callable. When |
None
|
is_server_default
|
bool
|
When |
False
|
**kwargs
|
Additional keyword arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
int
|
The handler ID returned by |
Raises:
| Type | Description |
|---|---|
SinkLabelAlreadyExistsError
|
If a sink with the given label is already registered. |
SinkConfigurationError
|
If |
SinkFileSystemError
|
If |
remove_sink(label)
¶
Removes a registered log sink by its label.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the sink to remove. |
required |
Raises:
| Type | Description |
|---|---|
SinkNotFoundError
|
If no sink with the given label is registered. |
StaleSinkHandlerError
|
If the sink's handler ID is no longer registered with loguru. |
remove_all_sinks()
¶
Removes all registered log sinks.
Raises:
| Type | Description |
|---|---|
StaleSinkHandlerError
|
If a sink's handler ID is no longer registered with
loguru (raised via |
modify_sink(label, sink=_UNSET, **overrides)
¶
Modifies an existing log sink in-place by rebuilding it with updated parameters.
Because loguru provides no update API, the existing sink is torn down and
reconstructed with the merged configuration. Successive calls layer correctly
because SinkInfo.sink_kwargs is kept up to date after each modification.
The existing handler is removed via loguru.logger.remove before the
replacement is added via loguru.logger.add. If rebuilding the replacement
fails, the sink is re-added with its previous configuration so that logging
through it continues uninterrupted, and the label keeps pointing at the
(restored) handler's current ID, before the typed error describing the
rebuild failure is raised. If that rollback itself fails, the sink is dropped
from this service's bookkeeping (it is no longer registered with loguru
either way) and the rollback failure is logged, while the original error is
still raised to the caller so the reason the modification failed is never
masked.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the sink to modify. |
required |
sink
|
Any
|
A replacement sink target. When omitted, the existing sink target is preserved. |
_UNSET
|
**overrides
|
Additional loguru |
{}
|
Raises:
| Type | Description |
|---|---|
SinkNotFoundError
|
If no sink with the given label is registered. |
StaleSinkHandlerError
|
If the existing handler's ID is no longer registered with loguru. |
SinkConfigurationError
|
If |
SinkFileSystemError
|
If the replacement sink is a file path that cannot be opened. |
get_all_sinks()
¶
configure_default_logging(logging_config)
¶
Configures default log level colors and registers the server's default sinks.
Sets display colors for each log level and adds a stdout sink. A file sink is
also added when logging_config.log_file is not None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logging_config
|
LoggingConfigModel
|
The logging configuration model specifying the log level, colorize flag, and optional log file path, rotation policy, and retention policy. |
required |
Raises:
| Type | Description |
|---|---|
StaleSinkHandlerError
|
If a stale handler ID is encountered while removing
the existing server-default sinks (raised via |
SinkLabelAlreadyExistsError
|
If the |
SinkConfigurationError
|
If |
SinkFileSystemError
|
If |