apifrom.logging_utils

Logging utilities module for APIFromAnything.

This module provides enhanced logging functionality for the application. It includes structured logging, log rotation, and integration with external logging services.

Overview

Classes

  • StructuredLogFormatter

Functions

Classes

StructuredLogFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None):bases: logging.Formatter

Formatter for structured JSON logs.

This formatter outputs logs in JSON format with additional context information such as timestamp, level, module, and process ID.

Initialize the formatter with specified format strings.

Initialize the formatter either with the specified format string, or a default as described above. Allow for specialized date formatting with the optional datefmt argument. If datefmt is omitted, you get an ISO8601-like (or RFC 3339-like) format.

Use a style parameter of β€˜%’, β€˜{’ or β€˜$’ to specify that you want to use one of %-formatting, str.format() ({}) formatting or string.Template formatting in your format string.

Changed in version 3.2: Added the style parameter.

apifrom.logging_utils.format(record)

Format the log record as a JSON string.

param record:

The log record to format.

returns:

A JSON string representation of the log record.

Functions

apifrom.logging_utils.configure_logging_dict(log_level='INFO', log_format='structured', log_file=None, max_bytes=10485760, backup_count=5)[source]
Parameters:
  • log_level (str)

  • log_format (str)

  • log_file (Optional[str])

  • max_bytes (int)

  • backup_count (int)

Return type:

Dict[str, Any]

Create a logging configuration dictionary.

param log_level:

The logging level.

param log_format:

The log format (β€˜structured’ for JSON, β€˜simple’ for text).

param log_file:

The path to the log file. If None, logs will be sent to stdout.

param max_bytes:

The maximum size of the log file before rotation.

param backup_count:

The number of backup log files to keep.

returns:

A logging configuration dictionary.

apifrom.logging_utils.get_logger(name, extra=None)[source]
Parameters:
  • name (str)

  • extra (Optional[Dict[str, Any]])

Return type:

logging.Logger

Get a logger with the specified name and extra context.

param name:

The name of the logger.

param extra:

Extra context to include in all log messages.

returns:

A configured logger instance.

apifrom.logging_utils.log_execution_time(logger=None, level=logging.DEBUG)[source]
Parameters:
Return type:

Callable

Decorator to log the execution time of a function.

param logger:

The logger to use. If None, a logger will be created.

param level:

The logging level to use.

returns:

A decorator function.

apifrom.logging_utils.setup_sentry(dsn, environment, release)[source]
Parameters:
Return type:

None

Set up Sentry for error tracking.

param dsn:

The Sentry DSN.

param environment:

The environment name.

param release:

The release version.