gitversioned.logging¶
Configure logging infrastructure and provide utilities for GitVersioned.
This module initializes and manages the global logger via the
configure_logger interface, establishing log levels, target sinks, and
thread-safe queues. It supports standard library logging interception through
InterceptHandler and intercept_standard_logging, automated function
telemetry via the autolog decorator, and structured OpenTelemetry-compliant
JSON logging using the OtelSink wrapper.
Veteran maintainers can quickly use configure_logger with LoggingSettings to
configure logging behavior, while new contributors can leverage the autolog
decorator to auto-instrument functions.
InterceptHandler
¶
Bases: Handler
Standard logging handler to intercept and forward records to Loguru.
This class hooks into the standard Python logging module. When a
standard logging record is emitted, it translates the logging level
and redirects the message, caller context, and exception trace to the
Loguru pipeline, ensuring unified log aggregation.
Example
.. code-block:: python
import logging
from gitversioned.logging import InterceptHandler
logging.basicConfig(handlers=[InterceptHandler()], level=0)
Source code in src/gitversioned/logging.py
emit(record)
¶
Emit a standard library logging record by forwarding it to Loguru.
:param record: The standard library log record. :returns: None.
Source code in src/gitversioned/logging.py
LoggingSettings
¶
Bases: BaseSettings
Settings configuration for the GitVersioned logging subsystem.
This class defines the configuration schema for logging, loading parameters
from environment variables prefixed with GITVERSIONED__LOGGING__ or via
direct instantiation. It allows customizing the output sink, log level,
format template, OpenTelemetry integration, and thread-safe queueing.
Example
.. code-block:: python
from gitversioned.logging import LoggingSettings, configure_logger
settings = LoggingSettings(
enabled=True,
level="DEBUG",
sink="stdout"
)
configure_logger(settings)
:cvar model_config: Configuration dictionary dictating environment variable prefixes and nested delimiters.
Source code in src/gitversioned/logging.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
OtelSink
¶
Wrapper sink for OpenTelemetry-compliant JSON logging.
This class intercepts log messages emitted by Loguru, extracts metadata from the Loguru record (such as exception tracebacks, trace context, and process info), and serializes them into standard OpenTelemetry JSON format.
Example
.. code-block:: python
import sys
from gitversioned.logging import OtelSink
sink = OtelSink(sys.stderr)
sink.write("Hello log message")
Source code in src/gitversioned/logging.py
__init__(target)
¶
Initialize the OpenTelemetry sink wrapper.
:param target: Output stream or file path where log records are written. :returns: None.
Source code in src/gitversioned/logging.py
close()
¶
Close the underlying file descriptor if a file path was provided as target.
:returns: None.
write(message)
¶
Write a message to the target output after formatting it as OpenTelemetry JSON if possible.
:param message: The log message to serialize or write. :returns: None.
Source code in src/gitversioned/logging.py
autolog(func=None, *, exception_log_level='ERROR')
¶
Decorate a function to automatically log call inputs, outputs, and raised exceptions.
This decorator logs inputs before execution, logs the return value on success, and logs any raised exceptions with trace details before re-raising.
Example
.. code-block:: python
from gitversioned.logging import autolog
@autolog
def calculate_sum(a: int, b: int) -> int:
return a + b
:param func: Target function to wrap, defaults to None. :param exception_log_level: Log level for exception reporting, defaults to "ERROR". :returns: The decorated wrapper or a decorator factory function.
Source code in src/gitversioned/logging.py
configure_logger(settings=None, **default_overrides)
¶
Configure the global Loguru logger based on settings.
This function initializes or updates the active logger handler. If no settings are provided, it loads settings from environment variables. It enables interception of standard library log statements and configures formatting, sinks, filtering, and queue options.
Example
.. code-block:: python
from gitversioned.logging import LoggingSettings, configure_logger
configure_logger(
settings=LoggingSettings(level="INFO"),
clear_loggers=True
)
:param settings: Logging configurations, defaults to None (loads from environment). :param default_overrides: Parameter overrides merged into env settings if settings is None. :returns: None. :raises ImportError: Raised if OpenTelemetry formatting is enabled but the package is not installed.
Source code in src/gitversioned/logging.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
intercept_standard_logging(enable=True)
¶
Hook standard library logging into or detach it from the Loguru pipeline.
This function attaches InterceptHandler to the standard root logger
to redirect standard library logs, or removes it to restore the
previous logging handlers.
Example
.. code-block:: python
from gitversioned.logging import intercept_standard_logging
intercept_standard_logging(enable=True)
:param enable: True to intercept standard logging; False to detach and restore. :returns: None.