Skip to content

Conversation

@Msameim181
Copy link
Owner

This pull request introduces support for custom logging handlers in the chromatrace library, allowing users to extend logging functionality by specifying their own handlers. The changes also include a version bump and a minor type fix.

Custom Logging Handlers Support:

  • Added a custom_handlers field to the LoggingSettings class, allowing users to specify a list of custom logging handlers.
  • Implemented the _setup_custom_handlers method in logging_config.py to initialize and configure custom handlers when present in the settings.

Other Changes:

  • Bumped the package version from 0.2.15 to 0.2.16 in pyproject.toml.
  • Fixed the type annotation for the handler parameter in _set_logger_settings from logging.handlers to logging.Handler.
  • Imported Field from pydantic to support the new field definition in LoggingSettings.

* Introduced `custom_handlers` field in `LoggingSettings` to allow users to specify custom logging handlers.
* Updated `_setup_custom_handlers` method in `LoggingConfig` to configure these handlers if provided.
* Incremented the version number in `pyproject.toml` to reflect the latest changes.
Copilot AI review requested due to automatic review settings November 27, 2025 10:35
@Msameim181 Msameim181 merged commit b95cec4 into develop Nov 27, 2025
7 checks passed
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds support for custom logging handlers in the chromatrace library, allowing users to extend logging functionality by providing their own handler instances. The implementation follows the existing pattern of setting up handlers with formatters and filters, though there are some areas that could be improved for better type safety, error handling, and test coverage.

Key changes:

  • Added custom_handlers field to LoggingSettings for users to specify custom logging handlers
  • Implemented _setup_custom_handlers method to configure custom handlers with appropriate formatters and filters
  • Fixed type annotation for handler parameter from logging.handlers to logging.Handler

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
src/chromatrace/logging_settings.py Adds custom_handlers field to LoggingSettings with Field import to support default factory
src/chromatrace/logging_config.py Implements custom handler setup logic and fixes type annotation for handler parameter
pyproject.toml Bumps version from 0.2.15 to 0.2.16

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

return

for handler in self.settings.custom_handlers:
if not isinstance(handler, logging.Handler):
Copy link

Copilot AI Nov 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invalid handlers are silently skipped with continue, which could lead to confusion when a user's custom handler doesn't appear in the logger. Consider logging a warning when an invalid handler is encountered to help with debugging.

Suggested change:

for handler in self.settings.custom_handlers:
    if not isinstance(handler, logging.Handler):
        logging.getLogger(__name__).warning(
            f"Skipping invalid custom handler: {type(handler).__name__}. "
            "Expected logging.Handler instance."
        )
        continue
Suggested change
if not isinstance(handler, logging.Handler):
if not isinstance(handler, logging.Handler):
logging.getLogger(__name__).warning(
f"Skipping invalid custom handler: {type(handler).__name__}. "
"Expected logging.Handler instance."
)

Copilot uses AI. Check for mistakes.
Comment on lines +47 to +57
def _setup_custom_handlers(self, logger: logging.Logger):
if not hasattr(self.settings, "custom_handlers"):
return

for handler in self.settings.custom_handlers:
if not isinstance(handler, logging.Handler):
continue
formatter = self._get_formatter(
colored=self.settings.use_console_colored_formatter
)
self._set_logger_settings(handler, formatter, logger)
Copy link

Copilot AI Nov 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new custom_handlers feature lacks test coverage. Since the repository has comprehensive test coverage for other logging features (see test/logger_test.py), this new functionality should also be tested. Consider adding tests that verify:

  1. Custom handlers are properly added to the logger
  2. Custom handlers receive the correct formatter
  3. Custom handlers get the appropriate filters (tracing, application level)
  4. Invalid handlers are handled gracefully

Copilot uses AI. Check for mistakes.
Comment on lines +54 to +56
formatter = self._get_formatter(
colored=self.settings.use_console_colored_formatter
)
Copy link

Copilot AI Nov 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Custom handlers always receive a formatter based on use_console_colored_formatter, which may not be appropriate for all custom handlers. For example, if a user adds a file-based custom handler, they might want use_file_colored_formatter instead, or a completely different formatter.

Consider one of these approaches:

  1. Allow users to pass handlers that already have formatters set, and only apply a formatter if the handler doesn't already have one
  2. Add a configuration option to specify which formatter setting applies to custom handlers
  3. Document this behavior clearly so users know custom handlers will use the console formatter settings

Copilot uses AI. Check for mistakes.
use_syslog_colored_formatter: bool = False
use_file_colored_formatter: bool = False
show_process_id: bool = False
custom_handlers: list = Field(default_factory=list)
Copy link

Copilot AI Nov 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type annotation for custom_handlers should be more specific. Since this field is expected to contain logging handlers, it should be typed as list[logging.Handler] instead of just list. This improves type safety and makes the API clearer to users.

Suggested change:

custom_handlers: list[logging.Handler] = Field(default_factory=list)
Suggested change
custom_handlers: list = Field(default_factory=list)
custom_handlers: list[logging.Handler] = Field(default_factory=list)

Copilot uses AI. Check for mistakes.
Comment on lines +48 to +50
if not hasattr(self.settings, "custom_handlers"):
return

Copy link

Copilot AI Nov 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hasattr check on line 48 is unnecessary since custom_handlers is now a defined field in the LoggingSettings Pydantic model with a default value. The attribute will always exist, so this check can be removed.

The method can be simplified to:

def _setup_custom_handlers(self, logger: logging.Logger):
    for handler in self.settings.custom_handlers:
        if not isinstance(handler, logging.Handler):
            continue
        formatter = self._get_formatter(
            colored=self.settings.use_console_colored_formatter
        )
        self._set_logger_settings(handler, formatter, logger)
Suggested change
if not hasattr(self.settings, "custom_handlers"):
return

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants