-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from authorizon/nicer_logging
Increased log verbosity
- Loading branch information
Showing
6 changed files
with
38 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
from logging.config import dictConfig | ||
def hijack_uvicorn_logs(): | ||
# Intercept future UVICORN | ||
from uvicorn.config import LOGGING_CONFIG | ||
LOGGING_CONFIG["handlers"] = { | ||
"default": { | ||
"formatter": "default", | ||
"class": "opal_common.logging.intercept.InterceptHandler", | ||
}, | ||
"access": { | ||
"formatter": "access", | ||
"class": "opal_common.logging.intercept.InterceptHandler", | ||
}, | ||
} | ||
# force existing UVICORN logging | ||
dictConfig(LOGGING_CONFIG) | ||
import logging | ||
|
||
def hijack_uvicorn_logs(intercept_handler: logging.Handler): | ||
""" | ||
Uvicorn loggers are configured to use special handlers. | ||
Adding an intercept handler to the root logger manages to intercept logs from uvicorn, however, the log messages are duplicated. | ||
This is happening because uvicorn loggers are propagated by default - we get a log message once for the "uvicorn" / "uvicorn.error" | ||
logger and once for the root logger). Another stupid issue is that the "uvicorn.error" logger is not just for errors, which is confusing. | ||
This method is doing 2 things for each uvicorn logger: | ||
1) remove all existing handlers and replace them with the intercept handler (i.e: will be logged via loguru) | ||
2) cancel propagation - which will mean messages will not propagate to the root logger (which also has an InterceptHandler), fixing the duplication | ||
""" | ||
# get loggers directly from uvicorn config - if they will change something - we will know. | ||
from uvicorn.config import LOGGING_CONFIG | ||
uvicorn_logger_names = list(LOGGING_CONFIG.get("loggers", {}).keys()) or ["uvicorn", "uvicorn.access", "uvicorn.error"] | ||
for logger_name in uvicorn_logger_names: | ||
logger = logging.getLogger(logger_name) | ||
logger.handlers = [intercept_handler] | ||
logger.propagate = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters