Skip to content

# `logger`

Here's the reference for the logging utilities.

You can import them directly from `fastapi_toolsets.logger`:

    from fastapi_toolsets.logger import configure_logging, get_logger

## `fastapi_toolsets.logger.configure_logging(level='INFO', fmt=DEFAULT_FORMAT, logger_name=None)`

Configure logging with a stdout handler and consistent format.

Sets up a :class:`~logging.StreamHandler` writing to stdout with the given format and level. Also configures the uvicorn loggers so that FastAPI access logs use the same format.

Calling this function multiple times is safe -- existing handlers are replaced rather than duplicated.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`level` | `LogLevel | int` | Log level (e.g. `"DEBUG"`, `"INFO"`, or `logging.DEBUG`). | `'INFO'`
`fmt` | `str` | Log format string. Defaults to `"%(asctime)s - %(name)s - %(levelname)s - %(message)s"`. | `DEFAULT_FORMAT`
`logger_name` | `str | None` | Logger name to configure. `None` (the default) configures the root logger so all loggers inherit the settings. | `None`

Returns:

Type | Description
--- | ---
`Logger` | The configured Logger instance.

Example

    from fastapi_toolsets.logger import configure_logging

    logger = configure_logging("DEBUG")
    logger.info("Application started")

## `fastapi_toolsets.logger.get_logger(name=_SENTINEL)`

Return a logger with the given name.

A thin convenience wrapper around :func:`logging.getLogger` that keeps logging imports consistent across the codebase.

When called without arguments, the caller's `__name__` is used automatically, so `get_logger()` in a module is equivalent to `logging.getLogger(__name__)`. Pass `None` explicitly to get the root logger.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`name` | `str | None` | Logger name. Defaults to the caller's `__name__`. Pass `None` to get the root logger. | `_SENTINEL`

Returns:

Type | Description
--- | ---
`Logger` | A Logger instance.

Example

    from fastapi_toolsets.logger import get_logger

    logger = get_logger()          # uses caller's __name__
    logger = get_logger("myapp")   # explicit name
    logger = get_logger(None)      # root logger