gee
08/06/2025, 1:32 PMfrom prefect import XXX
silences all the logsMarvin
08/06/2025, 1:32 PMMarvin
08/06/2025, 1:33 PMpython
import os
os.environ["PREFECT_LOGGING_CONFIGURE_ROOT_LOGGER"] = "False"
# Now import Prefect
from prefect import flow # or whatever you're importing
2. Custom Logging Configuration
You can create a custom logging configuration file at ~/.prefect/logging.yml
that preserves your existing logging setup. Here's a minimal example:
yaml
version: 1
disable_existing_loggers: False # This is important!
# Your existing handlers and formatters here...
loggers:
prefect:
level: "WARNING" # Or whatever level you prefer
handlers: ["console"] # Define your handlers
propagate: False # Prevent propagation to root logger
root:
level: WARNING
handlers: ["console"]
3. Initialize FastAPI Logging After Prefect
If the above solutions don't work, you can try re-initializing your FastAPI logging setup after importing Prefect:
python
import os
os.environ["PREFECT_LOGGING_LEVEL"] = "WARNING" # Optional: set Prefect's log level
from prefect import flow # Import Prefect first
import logging
import fastapi
from fastapi.logger import logger as fastapi_logger
# Reconfigure FastAPI logging
logging.basicConfig(
level=<http://logging.INFO|logging.INFO>,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
app = fastapi.FastAPI()
The issue you're experiencing is documented in GitHub issue #9831, which specifically deals with Prefect's interference with uvicorn's logging.
Important Notes:
1. The first solution (using PREFECT_LOGGING_CONFIGURE_ROOT_LOGGER=False
) is the most straightforward and recommended approach.
2. Make sure to set any logging configuration before importing Prefect.
3. If you're using uvicorn's logging features, you might need to explicitly reconfigure uvicorn's logging after setting up Prefect.
Would you like me to provide more details about any of these solutions or help you implement a specific approach for your use case?gee
08/06/2025, 1:46 PMMarvin
08/06/2025, 1:46 PMMarvin
08/06/2025, 1:47 PM~/.prefect/logging.yml
file that preserves your existing logging setup:
yaml
version: 1
disable_existing_loggers: False # Critical!
handlers:
console:
class: logging.StreamHandler
level: INFO
formatter: standard
formatters:
standard:
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
loggers:
prefect:
level: WARNING
handlers: [console]
propagate: False # Prevent propagation to root logger
# Your FastAPI logger configuration
fastapi:
level: INFO
handlers: [console]
propagate: False
uvicorn:
level: INFO
handlers: [console]
propagate: False
root:
level: INFO
handlers: [console]
B. Reconfigure Logging After Import
python
# First, import Prefect
from prefect import flow
# Then reconfigure logging
import logging
logging.basicConfig(
level=<http://logging.INFO|logging.INFO>,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
force=True # This is important!
)
# Now set up FastAPI
from fastapi import FastAPI
app = FastAPI()
3. Related GitHub Issues
- There's an open feature request (#10051) to add a way to disable Prefect's logging configuration completely.
- The issue is also discussed in #9831 specifically regarding uvicorn logging.
4. Best Practice
The most reliable current solution is to use a combination of a custom logging configuration file and explicit reconfiguration after Prefect import. This gives you the most control over your logging setup.
Would you like me to provide more specific details about implementing any of these solutions for your particular use case?