Skip to content

Logger Module

Provides structured, configurable logging across the application.

Logger

Factory class for creating and managing loggers throughout Ethicrawl.

This class provides a centralized way to create and configure loggers based on the application's configuration settings. It supports:

  • Resource-specific loggers with hierarchical naming
  • Component-specific log levels
  • Console output with optional color formatting
  • File output with configurable paths
  • Initialization management to prevent duplicate configuration

The Logger class is designed as a static utility class rather than being instantiated. All methods are static and operate on the global logging configuration.

Example

from ethicrawl.logger import Logger from ethicrawl.core import Resource, Url

Setup logging (happens automatically when first logger is created)

Logger.setup_logging()

Get a logger for a specific resource

resource = Resource(Url("https://example.com")) logger = Logger.logger(resource, "http") logger.info("Making request to %s", resource.url)

logger(resource, component=None) staticmethod

Get a logger for the specified resource, optionally with a component name.

Creates or retrieves a logger with a hierarchical name based on the resource URL and optional component. Automatically initializes logging if not already done.

Parameters:

Name Type Description Default
resource Resource

The resource to create a logger for

required
component str | None

Optional component name (e.g., "robots", "sitemaps")

None

Returns:

Type Description
Logger

A logger instance configured according to application settings

Example

from ethicrawl.core import Resource resource = Resource("https://example.com") logger = Logger.logger(resource, "http") logger.debug("Processing %s", resource.url)

reset() staticmethod

Reset logging configuration to initial state.

Removes all handlers and resets the initialization flag, allowing logging to be reconfigured. Primarily used for testing.

Example

In a test setup

def setUp(self): Logger.reset() # Ensure clean logging state

setup_logging() staticmethod

Configure the logging system based on current configuration.

This method reads the logger configuration from the global Config singleton and sets up handlers, formatters, and log levels accordingly. It should be called once at application startup, but is also called automatically by logger() if needed.

The method configures: - Root logger with WARNING level - Main application logger with configured level - Console output (if enabled) - File output (if enabled) - Component-specific log levels

This method is idempotent - calling it multiple times has no effect after the initial setup.