feat: migrate from loguru to structlog#9321
Conversation
…pylint references
- Replaced instances of loguru logger with langflow.logging.logger across multiple files. - Updated logging calls to use asynchronous methods where applicable (e.g., await logger.awarning). - Ensured consistent logging practices throughout the codebase by standardizing the logger import.
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughMigrates logging across backend modules from Loguru to LangFlow’s asynchronous logger, updating calls to awaited async variants. Modernizes typings in Alembic migrations and adds clearer column checks. Adjusts several APIs/signatures (keyword-only params, async functions), adds MCP server caching, revises Ollama model fetching, and updates multiple starter project component code blocks and hashes. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor User
participant Component as ChatOllamaComponent
participant Ollama as Ollama REST API
User->>Component: get_models(base_url, tool_model_enabled=...)
Component->>Component: Normalize base_url, build tags/show URLs
Component->>Ollama: GET /api/tags
Ollama-->>Component: tags JSON
Component->>Ollama: GET /api/show (per model)
Ollama-->>Component: model capabilities
Component->>Component: Filter by completion (+tool_calling if requested)
Component-->>User: list[str] model names
note over Component: Async debug logs during fetch/filter
sequenceDiagram
autonumber
actor Caller
participant Utils as custom/utils.py
participant FS as Filesystem
participant Importer as Python Import
Caller->>Utils: load_custom_component(name, paths)
Utils->>FS: Scan component paths
FS-->>Utils: Module file path
Utils->>Importer: Dynamically import module
Importer-->>Utils: Module/class object
Utils-->>Caller: Component class/object
note over Utils: Async logging on errors and trace paths
Estimated code review effort🎯 4 (Complex) | ⏱️ ~75 minutes Possibly related PRs
Suggested labels
Suggested reviewers
✨ Finishing Touches🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
Status, Documentation and Community
|
|
Nice One! @ogabrielluiz |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chainI'll help you analyze the structlog usage in the codebase. Let me first gather information about structlog best practices and then examine the current implementation. 🏁 Script executed: Length of output: 3699 🌐 Web query: 💡 Result: Best practices for global configuration of the structlog Python logging library include centralizing configuration early in your application's lifecycle, using structured (preferably JSON) output in production, leveraging context management for concurrent or async operations, and keeping per-module boilerplate minimal. Key recommendations and supporting details:
Summary Table of structlog Global Configuration Best Practices:
References: For production, always prefer explicit, minimal, and environment-appropriate structlog global configuration for maintainability and clarity. Citations:
Let me examine the current structlog implementation in detail and check for components that need updating. 🏁 Script executed: Length of output: 14389 🏁 Script executed: Length of output: 324131 @edwinjosechittilappilly: I've reviewed the structlog usage and configuration. The codebase uses a global structlog logger as recommended, and all main components import and use it consistently. The setup aligns with structlog best practices from the official documentation. If you want to update legacy logging in documentation/examples, that can be addressed separately, but the main codebase is already using structlog globally and correctly. |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain@edwinjosechittilappilly Thank you for the guidance! Let me first gather information about structlog best practices and then analyze the current implementation in the codebase. 🏁 Script executed: Length of output: 163833 🌐 Web query: 💡 Result: For structlog global configuration in Python, the best practice is to configure structlog once at application startup using Key best practices for global structlog setup:
Sample production-ready global configuration: import sys
import structlog
import logging
import os
# Choose structured JSON for production, pretty for development
shared_processors = [
structlog.processors.add_log_level,
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info
]
if sys.stderr.isatty():
processors = shared_processors + [structlog.dev.ConsoleRenderer()]
else:
processors = shared_processors + [
structlog.processors.dict_tracebacks,
structlog.processors.JSONRenderer()
]
# Set log level via env var or default to INFO
level_name = os.environ.get("LOG_LEVEL", "INFO").upper()
log_level = getattr(logging, level_name)
structlog.configure(
processors=processors,
wrapper_class=structlog.make_filtering_bound_logger(log_level),
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory()
)
logger = structlog.get_logger()This setup:
Additional recommendations:
Following these best practices ensures your global structlog configuration is robust, context-rich, and ready for scalable production logging. Citations:
Thank you for the reference to structlog documentation! Based on my analysis of the codebase and the structlog best practices, I can provide you with a comprehensive review of the current structlog implementation and recommendations for ensuring global usage. Based on my analysis, here are my findings and recommendations for ensuring global structlog usage: ✅ Current Implementation StrengthsYour current structlog setup in
❌ Areas Requiring Updates1. Inconsistent Logger ImportsI found several files still using alternative logging approaches: Files using Loguru instead of structlog:
Recommendation: Update these to use: from langflow.logging.logger import logger2. Components Not Using Structured LoggingSome components may not be using the centralized structlog configuration. 🔧 Recommendations for Global Structlog Adoption1. Create a Logger Import StandardEnsure all new components use: from langflow.logging.logger import logger2. Update Starter ProjectsThe starter project templates should be updated to use structlog consistently instead of loguru. 3. Add Logging GuidelinesConsider adding to your coding standards:
4. Component Template UpdatesFor custom components, provide a template that includes: from langflow.logging.logger import logger
class MyComponent(Component):
def build_output(self):
logger.info("Component execution started",
component=self.__class__.__name__)
# ... component logic5. Migration ScriptConsider creating a migration script to automatically update imports: # Replace in files:
# from loguru import logger → from langflow.logging.logger import logger
# import logging → # import logging (comment out if not needed)The current structlog implementation is well-architected and follows best practices. The main need is ensuring consistent adoption across all components and removing legacy logging imports. 🏁 Script executed: Length of output: 72150 Perfect! Now I have a complete picture of the structlog implementation in your codebase. Based on my comprehensive analysis, here's my detailed assessment and recommendations: 🎉 Excellent Structlog Implementation!Your structlog implementation is very well done and follows industry best practices. Here's my comprehensive assessment: ✅ Current Strengths (Outstanding!)Your implementation excels in these areas:
|
jordanrfrazier
left a comment
There was a problem hiding this comment.
Any issues with gunicorn handlers? I didn't go through the entire code but read up on structlog, and looks good to try it over loguru.
…and session_getter
… tests to use module import
|
* refactor: Enhance logging configuration with structured logging and buffer support * feat: Add structlog dependency for enhanced logging support * refactor: Update ruff dependency to version 0.12.7 and remove unused pylint references * Refactor logging imports to use langflow.logging.logger - Replaced instances of loguru logger with langflow.logging.logger across multiple files. - Updated logging calls to use asynchronous methods where applicable (e.g., await logger.awarning). - Ensured consistent logging practices throughout the codebase by standardizing the logger import. * refactor: Add missing docstring rule to ruff configuration * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes (attempt 2/3) * [autofix.ci] apply automated fixes * fix: update logger calls to use async methods in DatabaseService * fix: update logger calls to use async methods in initialize_database and session_getter * fix: update logger calls to use async methods in LangflowRunnerExperimental * fix: update logger calls to use async methods across various services * Refactor logging to use asynchronous logger methods across multiple components - Updated logging calls in to use async logger methods for error handling and debugging. - Modified to utilize async logging for error messages during file deletion. - Changed logging in , , and other agent-related files to use async methods for error and debug messages. - Refactored logging in various components including , , , and others to ensure consistent use of async logging. - Updated , , and to replace synchronous logging with asynchronous counterparts. - Ensured all logging changes maintain the original message structure while enhancing performance with async capabilities. * [autofix.ci] apply automated fixes * fix: update logger calls to use async methods in various components * feat: add InterceptHandler to route standard logging messages to structlog * refactor: remove async_file parameter from logger configuration * fix: correct log level mapping and enhance log rotation validation * refactor: remove unused logging import and streamline schema imports * Refactor logging in AssemblyAI components and other modules to use exc_info for better error tracing - Updated logging statements in AssemblyAI components (e.g., assemblyai_get_subtitles, assemblyai_lemur, assemblyai_list_transcripts, etc.) to use logger.debug with exc_info=True for improved error context. - Modified logging in various helper and utility functions to enhance error reporting. - Ensured consistent logging practices across the codebase for better maintainability and debugging. * refactor: remove InterceptHandler from logger configuration to avoid recursion * refactor: enhance test coverage for logger module with comprehensive test cases * refactor: add rule to ignore mutable objects without __hash__ method in linter * fix various lint issues * refactor: update function signatures to improve clarity and consistency * refactor: streamline import statements and enhance response handling in voice mode * refactor: simplify lifespan cleanup logic * refactor: remove unused caplog fixture and improve graph test clarity * fix: specify logger type as BoundLogger for clarity * [autofix.ci] apply automated fixes * refactor: remove unused logger and correct return statement in arun_flow_from_json * refactor: update logger usage to support async methods in tests * fix: correct datetime bounds for hypothesis strategies to avoid timezone issues * fix: update warning message for invalid string input type in tests * refactor: simplify message handling tests by removing database session mocks * refactor: remove redundant comment from test_max_size function in test_logger.py * fix: update patch target for DEV setting in remove_exception_in_production test * fix: update patch target for DEV setting in remove_exception_in_production test * fix: update patching of DEV setting in remove_exception_in_production tests to use module import --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>



Migrate to
structlogto improve performance and to have better logs in production environments.The high number of file changes is due to updates to logger calls in async methods and updates to how we pass the exception data into logger calls as this is a bit different in
structlog.Summary by CodeRabbit
New Features
Improvements
Bug Fixes
Breaking Changes