apifrom.plugins.logging

Logging plugin for APIFromAnything.

This module provides a plugin for logging requests and responses.

Overview

Classes

Classes

class apifrom.plugins.logging.API(debug=False, title='APIFromAnything API', description='API created with APIFromAnything', version='1.0.0', docs_url='/docs', openapi_config=None, swagger_ui_config=None, enable_docs=True)[source]
Parameters:

Main application container for APIFromAnything.

This class serves as the central registry for all API endpoints, middleware, and configuration. It provides methods for registering endpoints, middleware, and starting the server.

apifrom.plugins.logging.router

The router instance for managing routes.

Type:

Router

apifrom.plugins.logging.middleware

List of middleware instances.

Type:

list

apifrom.plugins.logging.debug

Whether to run in debug mode.

Type:

bool

apifrom.plugins.logging.title

The title of the API.

Type:

str

apifrom.plugins.logging.description

The description of the API.

Type:

str

apifrom.plugins.logging.version

The version of the API.

Type:

str

apifrom.plugins.logging.docs_url

The URL for the API documentation.

Type:

str

Initialize a new API instance.

param debug:

Whether to run in debug mode.

param title:

The title of the API.

param description:

The description of the API.

param version:

The version of the API.

param docs_url:

The URL for the API documentation.

param openapi_config:

Configuration for OpenAPI documentation.

param swagger_ui_config:

Configuration for Swagger UI.

param enable_docs:

Whether to enable API documentation.

LoggingPlugin(logger = None, level = logging.INFO, log_request_body = False, log_response_body = False, log_headers = False, exclude_paths = None, exclude_methods = None):bases: apifrom.plugins.base.Plugin

Plugin for logging requests and responses.

Initialize the logging plugin.

param logger:

The logger to use (defaults to a new logger)

param level:

The logging level

param log_request_body:

Whether to log request bodies

param log_response_body:

Whether to log response bodies

param log_headers:

Whether to log headers

param exclude_paths:

Paths to exclude from logging

param exclude_methods:

HTTP methods to exclude from logging

apifrom.plugins.logging.initialize(api)

Initialize the plugin.

param api:

The API instance

apifrom.plugins.logging.on_error(error, request)

Log an error.

param error:

The error that occurred

param request:

The request object

returns:

None

apifrom.plugins.logging.on_shutdown()

Log server shutdown.

apifrom.plugins.logging.on_startup()

Log server startup.

apifrom.plugins.logging.post_response(response, request)

Log the response.

param response:

The response object

param request:

The request object

returns:

The response object

apifrom.plugins.logging.pre_request(request)

Log the request.

param request:

The request object

returns:

The request object

Plugin:bases: abc.ABC

Base class for all plugins.

This class defines the interface that all plugins must implement, providing hooks for various stages of the API lifecycle.

Initialize the plugin.

class apifrom.plugins.logging.Request(request=None, path_params=None, method=None, path=None, query_params=None, headers=None, body=None, client_ip=None)[source]
Parameters:
  • request (Optional[starlette.requests.Request])

  • path_params (Optional[dict[Any, Any]])

  • method (Optional[str])

  • path (Optional[str])

  • query_params (Optional[dict[Any, Any]])

  • headers (Optional[dict[Any, Any]])

  • body (Optional[Union[str, bytes]])

  • client_ip (Optional[str])

Request class for APIFromAnything.

This class wraps a Starlette request and provides methods for accessing request data in a convenient way.

apifrom.plugins.logging._request

The underlying Starlette request.

apifrom.plugins.logging.path_params

Path parameters extracted from the URL.

apifrom.plugins.logging.query_params

Query parameters extracted from the URL.

apifrom.plugins.logging.headers

HTTP headers.

apifrom.plugins.logging.method

HTTP method.

apifrom.plugins.logging.path

Request path.

apifrom.plugins.logging._body

Cached request body.

Initialize a new Request instance.

param request:

The underlying Starlette request.

param path_params:

Path parameters extracted from the URL.

param method:

The HTTP method.

param path:

The request path.

param query_params:

Query parameters.

param headers:

HTTP headers.

param body:

Request body.

param client_ip:

Client IP address.

class apifrom.plugins.logging.Response(content=None, status_code=200, headers=None, content_type='application/json')[source]
Parameters:
  • content (Any)

  • status_code (int)

  • headers (Optional[Dict[str, str]])

  • content_type (str)

Response class for APIFromAnything.

This class represents an HTTP response and provides methods for setting response data, status code, and headers.

apifrom.plugins.logging.content

The response content.

apifrom.plugins.logging.status_code

The HTTP status code.

apifrom.plugins.logging.headers

HTTP headers.

apifrom.plugins.logging.content_type

The content type of the response.

Initialize a new Response instance.

param content:

The response content.

param status_code:

The HTTP status code.

param headers:

HTTP headers.

param content_type:

The content type of the response.