Skip to content

Remove logging setting from global context #2720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2025

Conversation

vinibrsl
Copy link
Member

This commit fixes a bug where changing logging level would be overriden by src/crewai/project/crew_base.py. For example, the following snippet on top of a crew or flow would not work:

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

Crews and flows should be able to set their own log level, without being overriden by CrewAI library code.

This commit fixes a bug where changing logging level would be overriden
by `src/crewai/project/crew_base.py`. For example, the following snippet
on top of a crew or flow would not work:

```python
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
```

Crews and flows should be able to set their own log level, without being
overriden by CrewAI library code.
@mplachta
Copy link
Contributor

Disclaimer: This review was made by a crew of AI Agents.

Code Review for PR #2720: Remove logging setting from global context in crew_base.py


Summary of Key Findings

This PR removes the global logging configuration line:

logging.basicConfig(level=logging.WARNING)

from src/crewai/project/crew_base.py. This fixes a significant bug where any user-level logging configuration (e.g., logging.basicConfig() in user code) was unintentionally overridden by the library. By removing this, the library no longer imposes a global logging configuration, allowing applications using CrewAI to customize their logging output, level, and format freely.

This change aligns the library with best practices for Python libraries, which should never configure or alter the global logging state to avoid surprising users.


Specific Improvements & Examples

  • Correct Removal of Global Config: Removing logging.basicConfig(level=logging.WARNING) eliminates interference with application logging settings.

  • Library Logging Practices: Confirm that all internal modules (including crew_base.py) obtain loggers with:

    import logging
    
    logger = logging.getLogger(__name__)

    and use this logger for emitting logs, without calling basicConfig() or setting handlers/levels globally.

  • Documentation & Changelog Updates:
    Because this change affects user-visible behavior, update the user-facing documentation and changelog to inform users:

    The CrewAI library no longer sets a global default logging level.
    Users must configure logging (level, handlers, format) in their application entry points.

  • Example User Setup (suggested for docs):

    import logging
    
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    )

    This example demonstrates how users can set their desired logging configuration without the library overriding it.


Historical Context & Related PR Learnings

  • Previous library versions incorrectly set a WARNING level globally, which restricted application flexibility.
  • Common community practices and numerous Python library standards emphasize not configuring global logging in libraries.
  • It is typical for early PRs to introduce default logging for convenience, later corrected when user feedback highlights issues.
  • Related PR discussions (though not retrievable here) likely revolve around balancing sensible defaults versus respecting user control.
  • This patch represents a mature decision to uphold best practices and enhance user experience by relinquishing global logging control.

Implications for Related Files

  • Other modules under src/crewai/project/ and the entire CrewAI codebase should avoid performing global logging configuration.
  • Instead, ensure consistent use of logging.getLogger(__name__) in all modules.
  • Application or higher-level orchestration modules (e.g., flow launchers, CLI tools) are the appropriate places to establish logging configuration.

Recommendations for Further Improvements

  1. Update Documentation and CHANGELOG:
    Clearly state this change and guide users regarding logging setup responsibilities.

  2. Audit Codebase for Logging Usage:
    Verify that all internal modules follow the recommended pattern: create local loggers and emit logs without configuring global logging.

  3. Add Developer Guidelines:
    Consider adding a note in contribution guidelines or a developer README to prevent reintroducing global logging configuration in the future.

  4. Optional Enhancements:

    • Provide example logging setups for common use cases in documentation.
    • Optionally, provide a helper utility for development-time logging configuration that does not affect production code.

Conclusion

This PR is a well-justified and narrowly scoped fix addressing a known issue with global logging configuration in crew_base.py. It significantly improves user control over logging with no adverse effects on code quality or maintainability. Approving this PR is recommended with the strong caveat that documentation and changelog updates accompany the release to avoid user confusion.


Action: ✅ Approve after documentation updates and changelog inclusion.


End of code review comment.

@vinibrsl vinibrsl merged commit 55b0750 into main Apr 29, 2025
9 checks passed
@vinibrsl vinibrsl deleted the remove-global-logging-setting branch April 29, 2025 14:21
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.

4 participants