-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add log file handler for both app loggers and uvicorn loggers
- Loading branch information
Showing
2 changed files
with
70 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,45 @@ | ||
import logging | ||
import logging.config | ||
import logging.handlers | ||
import os | ||
from pathlib import Path | ||
|
||
from asgi_correlation_id import CorrelationIdFilter | ||
|
||
HOME_PATH = os.path.expanduser("~") | ||
LOG_FILE_PATH = Path(HOME_PATH + "/logs/app.log") | ||
LOG_FILE_PATH.parent.mkdir(exist_ok=True, parents=True) | ||
LOG_LEVEL = "INFO" | ||
|
||
# time | log level | logger name | trace id | process id | thread id | message | ||
# %(levelname)8s: max 8 characters for "CRITICAL" | ||
# ref: https://docs.python.org/3/library/logging.html#logrecord-attributes | ||
LOG_FORMAT_PATTERN = "%(asctime)s|%(levelname)-8s|%(name)s|%(correlation_id)s|%(process)d|%(thread)d| %(message)s" | ||
|
||
TRACE_ID_FILTER = CorrelationIdFilter(name="trace_id_filter", uuid_length=16, default_value="-") | ||
|
||
LOG_FORMAT_PATTERN = "%(asctime)s|%(levelname)-8s|%(name)s|%(correlation_id)s|%(process)d|%(thread)d| %(message)s" | ||
_HOME_PATH = os.path.expanduser("~") | ||
_LOG_FILE_PATH = Path(_HOME_PATH + "/logs/app.log") | ||
# create parent directories of log file if these do not exist | ||
_LOG_FILE_PATH.parent.mkdir(exist_ok=True, parents=True) | ||
|
||
LOGGING_CONFIG = { | ||
"version": 1, | ||
"disable_existing_loggers": False, | ||
"filters": { | ||
"correlation_id": { | ||
"()": "asgi_correlation_id.CorrelationIdFilter", | ||
"uuid_length": 16, | ||
"default_value": "-", | ||
}, | ||
}, | ||
"formatters": { | ||
"default": { | ||
# time | log level | logger name | trace id | process id | thread id | message | ||
# %(levelname)8s: max 8 characters for "CRITICAL" | ||
# ref: https://docs.python.org/3/library/logging.html#logrecord-attributes | ||
"format": LOG_FORMAT_PATTERN, | ||
}, | ||
"access": { | ||
# time | log level | logger name | trace id | process id | thread id | message | ||
# %(levelname)8s: max 8 characters for "CRITICAL" | ||
# ref: https://docs.python.org/3/library/logging.html#logrecord-attributes | ||
"format": LOG_FORMAT_PATTERN, | ||
} | ||
}, | ||
"handlers": { | ||
"console": { | ||
"formatter": "default", | ||
"class": "logging.StreamHandler", | ||
"stream": "ext://sys.stdout", | ||
}, | ||
"file": { | ||
"formatter": "default", | ||
# ref: https://docs.python.org/3/library/logging.handlers.html#timedrotatingfilehandler | ||
"class": "logging.handlers.TimedRotatingFileHandler", | ||
"filename": LOG_FILE_PATH, | ||
"when": "midnight", | ||
"interval": 1, | ||
"backupCount": 7, | ||
"encoding": "utf-8", | ||
}, | ||
}, | ||
"loggers": { | ||
"uvicorn.error": { | ||
"level": "INFO", | ||
"handlers": ["console", "file"], | ||
"filters": ["correlation_id"], | ||
"propagate": "no", | ||
}, | ||
"uvicorn.access": { | ||
"level": "INFO", | ||
"handlers": ["console", "file"], | ||
"filters": ["correlation_id"], | ||
"propagate": "no", | ||
} | ||
}, | ||
"root": { | ||
"level": "DEBUG", | ||
"handlers": ["console", "file"], | ||
"filters": ["correlation_id"], | ||
"propagate": "no", | ||
} | ||
# It will be added to logging config as "file_handler" for uvicorn config initializing | ||
FILE_HANDLER_CONFIG = { | ||
"formatter": "default", | ||
# ref: https://docs.python.org/3/library/logging.handlers.html#timedrotatingfilehandler | ||
"class": "logging.handlers.TimedRotatingFileHandler", | ||
"filename": _LOG_FILE_PATH, | ||
"when": "midnight", | ||
"backupCount": 7, | ||
"encoding": "utf-8", | ||
} | ||
|
||
|
||
def config_app_logging() -> None: | ||
def configure_app_logging(file_handler: logging.Handler) -> None: | ||
""" | ||
:param file_handler: Reuse the file handler instance created by uvicorn | ||
""" | ||
|
||
console_handler = logging.StreamHandler() | ||
console_handler.addFilter(TRACE_ID_FILTER) | ||
logging.basicConfig( | ||
handlers=[console_handler], | ||
level="INFO", | ||
handlers=[console_handler, file_handler], | ||
level=LOG_LEVEL, | ||
format=LOG_FORMAT_PATTERN) |
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