cognee icon indicating copy to clipboard operation
cognee copied to clipboard

fix: Removed automatic config of structlog on import, fixes 855

Open jspv opened this issue 7 months ago • 3 comments

Description

Per 855

  • Changed the automatic setup of structlog (and consequential changing of all existing log handlers) to a standalone routine setup_logging that can be called if desired rather than on import
  • Removed the thread locking that stopped multiple setups from occurring at the same time, no longer needed as each module will not try and install it automatically
  • Modified existing code with clear entry points (with __main__ fence) to call setup_logging for some backward compatibility.

Net effect: import cognee will no longer change the importing application's log setup.

DCO Affirmation

I affirm that all code in every commit of this pull request conforms to the terms of the Topoteretes Developer Certificate of Origin.

jspv avatar May 31 '25 14:05 jspv

Please make sure all the checkboxes are checked:

  • [ ] I have tested these changes locally.
  • [ ] I have reviewed the code changes.
  • [ ] I have added end-to-end and unit tests (if applicable).
  • [ ] I have updated the documentation and README.md file (if necessary).
  • [ ] I have removed unnecessary code and debug statements.
  • [ ] PR title is clear and follows the convention.
  • [ ] I have tagged reviewers or team members for feedback.

pull-checklist[bot] avatar May 31 '25 14:05 pull-checklist[bot]

[!IMPORTANT]

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

"""

Walkthrough

This change refactors logging initialization across multiple modules by replacing direct logger retrieval with explicit calls to setup_logging() at runtime. The logging utility module is simplified by removing threading locks and internal lazy initialization, relying instead on a single configuration flag. Example and server scripts are updated to initialize logging at startup.

Changes

File(s) Change Summary
cognee/shared/logging_utils.py Refactored logging state management: removed threading, made LoggerInterface a Protocol, updated setup and retrieval logic, clarified docstrings.
cognee-mcp/src/server.py Replaced global log file variable with dynamic retrieval; switched to setup_logging() for logger initialization.
cognee/api/client.py
cognee/api/v1/cognify/code_graph_pipeline.py
cognee/api/v1/visualize/visualize.py
Updated to call setup_logging() at runtime for logging initialization.
cognee/modules/retrieval/utils/description_to_codepart_search.py Updated to initialize logging with setup_logging() in main block.
examples/python/code_graph_example.py
examples/python/dynamic_steps_example.py
examples/python/graphiti_example.py
examples/python/multimedia_example.py
examples/python/ontology_demo_example.py
examples/python/ontology_demo_example_2.py
examples/python/simple_example.py
examples/python/simple_node_set_example.py
Changed logger initialization from get_logger to setup_logging() in example scripts.

Sequence Diagram(s)

sequenceDiagram
    participant Script
    participant LoggingUtils

    Script->>LoggingUtils: setup_logging(log_level)
    LoggingUtils-->>Script: Configures logging, sets flag

    Script->>LoggingUtils: get_logger(name, level)
    alt If logging configured
        LoggingUtils-->>Script: Returns structlog logger
    else
        LoggingUtils-->>Script: Returns standard logger
    end

Possibly related PRs

  • topoteretes/cognee#434: Both PRs modify the logging setup process, specifically setup_logging, focusing on restructuring and improving robustness.
  • topoteretes/cognee#674: Both PRs adjust how logging and log file locations are handled in cognee-mcp/src/server.py, with overlapping changes.
  • topoteretes/cognee#658: Both PRs modify logging usage, with this PR introducing explicit runtime setup and dynamic log file handling.

Suggested labels

run-checks

Poem

In the warren, logs now shine bright,
No more tangled threads at night.
With setup_logging called anew,
The rabbits hop with clearer view.
Simpler logs, less bunny stress—
Now every script can log with finesse!
🐇✨ """


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar May 31 '25 14:05 coderabbitai[bot]

@dexters1 is attempting to deploy a commit to the Topoteretes Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Jun 05 '25 09:06 vercel[bot]

@jspv Thanks a lot for your contribution! I've made a new branch from your branch (jspv-structlog-auto-config-fix) to run CI/CD on our side and add some changes. This new branch has been merged (https://github.com/topoteretes/cognee/pull/907)

Your commits are all stored there so you'll be part of our contributors 😃

It's currently merged to our dev branch, it will be a part of our next Cognee release

dexters1 avatar Jun 11 '25 13:06 dexters1