Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ logger:
http_std_debug_format: '<n>[{request_id}]</n> {client_host} {user_id} "<u>{method} {url_path}</u> HTTP/{http_version}"'
http_std_msg_format: '<n><w>[{request_id}]</w></n> {client_host} {user_id} "<u>{method} {url_path}</u> HTTP/{http_version}" {status_code} {content_length}B {response_time}ms'
http_file_enabled: true
http_file_msg_format: '{client_host} {request_id} {user_id} [{datetime}] "{method} {url_path} HTTP/{http_version}" {status_code} {content_length} "{h_referer}" "{h_user_agent}" {response_time}'
http_log_path: "http/{app_name}.http.access.log"
http_err_path: "http/{app_name}.http.err.log"
http_json_enabled: true
Expand Down Expand Up @@ -325,7 +324,6 @@ app.add_middleware(
has_proxy_headers=True,
debug_format=logger_loader.config.extra.http_std_debug_format,
msg_format=logger_loader.config.extra.http_std_msg_format,
file_msg_format=logger_loader.config.extra.http_file_msg_format,
)

@app.get("/")
Expand Down
6 changes: 1 addition & 5 deletions beans_logging/fastapi/_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ def http_file_format(record: dict) -> str:
_http_info["datetime"] = record["time"].isoformat()
record["extra"]["http_info"] = _http_info

_msg_format = _MSG_FORMAT
if "http_file_msg_format" in record["extra"]:
_msg_format = record["extra"]["http_file_msg_format"]

_msg = _msg_format.format(**_http_info)
_msg = _MSG_FORMAT.format(**_http_info)
record["http_message"] = _msg

return "{http_message}\n"
Expand Down
26 changes: 16 additions & 10 deletions beans_logging/fastapi/_handlers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-

from typing import Union, Callable

from pydantic import validate_call

from beans_logging import LoggerLoader
Expand All @@ -13,28 +15,30 @@ def add_http_file_handler(
logger_loader: LoggerLoader,
log_path: str = "http/{app_name}.http.access.log",
err_path: str = "http/{app_name}.http.err.log",
formatter: Union[Callable, str] = http_file_format,
):
"""Add http access log file and error file handler.

Args:
logger_loader (LoggerLoader, required): LoggerLoader instance.
log_path (str, optional): Log file path. Defaults to "http/{app_name}.http.access.log".
err_path (str, optional): Error log file path. Defaults to "http/{app_name}.http.err.log".
logger_loader (LoggerLoader, required): LoggerLoader instance.
log_path (str, optional): Log file path. Defaults to "http/{app_name}.http.access.log".
err_path (str, optional): Error log file path. Defaults to "http/{app_name}.http.err.log".
formatter (Union[Callable, str], optional): Log formatter. Defaults to `http_file_format` function.
"""

logger_loader.add_custom_handler(
handler_name="FILE.HTTP",
sink=log_path,
filter=use_http_filter,
format=http_file_format,
format=formatter,
)

logger_loader.add_custom_handler(
handler_name="FILE.HTTP_ERR",
sink=err_path,
level="WARNING",
filter=use_http_filter,
format=http_file_format,
format=formatter,
)


Expand All @@ -43,26 +47,28 @@ def add_http_file_json_handler(
logger_loader: LoggerLoader,
log_path: str = "json.http/{app_name}.json.http.access.log",
err_path: str = "json.http/{app_name}.json.http.err.log",
formatter: Union[Callable, str] = http_file_json_format,
):
"""Add http access json log file and json error file handler.

Args:
logger_loader (LoggerLoader, required): LoggerLoader instance.
log_path (str, optional): Json log file path. Defaults to "http.json/{app_name}.json.http.access.log".
err_path (str, optional): Json error log file path. Defaults to "http.json/{app_name}.json.http.err.log".
logger_loader (LoggerLoader, required): LoggerLoader instance.
log_path (str, optional): Json log file path. Defaults to "http.json/{app_name}.json.http.access.log".
err_path (str, optional): Json error log file path. Defaults to "http.json/{app_name}.json.http.err.log".
formatter (Union[Callable, str], optional): Log formatter. Defaults to `http_file_json_format` function.
"""

logger_loader.add_custom_handler(
handler_name="FILE.JSON.HTTP",
sink=log_path,
filter=use_http_filter,
format=http_file_json_format,
format=formatter,
)

logger_loader.add_custom_handler(
handler_name="FILE.JSON.HTTP_ERR",
sink=err_path,
level="WARNING",
filter=use_http_filter,
format=http_file_json_format,
format=formatter,
)
10 changes: 1 addition & 9 deletions beans_logging/fastapi/_middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@ class HttpAccessLogMiddleware(BaseHTTPMiddleware):
Attributes:
_DEBUG_FORMAT (str ): Default http access log debug message format. Defaults to '<n>[{request_id}]</n> {client_host} {user_id} "<u>{method} {url_path}</u> HTTP/{http_version}"'.
_MSG_FORMAT (str ): Default http access log message format. Defaults to '<n><w>[{request_id}]</w></n> {client_host} {user_id} "<u>{method} {url_path}</u> HTTP/{http_version}" {status_code} {content_length}B {response_time}ms'.
_FILE_MSG_FORMAT (str ): Default http access log file message format. Defaults to '{client_host} {request_id} {user_id} [{datetime}] "{method} {url_path} HTTP/{http_version}" {status_code} {content_length} "{h_referer}" "{h_user_agent}" {response_time}'.

has_proxy_headers (bool): If True, use proxy headers to get http request info. Defaults to False.
has_cf_headers (bool): If True, use cloudflare headers to get http request info. Defaults to False.
debug_format (str ): Http access log debug message format. Defaults to `HttpAccessLogMiddleware._DEBUG_FORMAT`.
msg_format (str ): Http access log message format. Defaults to `HttpAccessLogMiddleware._MSG_FORMAT`.
file_msg_format (str ): Http access log file message format. Defaults to `HttpAccessLogMiddleware._FILE_MSG_FORMAT`.
"""

_DEBUG_FORMAT = '<n>[{request_id}]</n> {client_host} {user_id} "<u>{method} {url_path}</u> HTTP/{http_version}"'
_MSG_FORMAT = '<n><w>[{request_id}]</w></n> {client_host} {user_id} "<u>{method} {url_path}</u> HTTP/{http_version}" {status_code} {content_length}B {response_time}ms'
_FILE_MSG_FORMAT = '{client_host} {request_id} {user_id} [{datetime}] "{method} {url_path} HTTP/{http_version}" {status_code} {content_length} "{h_referer}" "{h_user_agent}" {response_time}'

def __init__(
self,
Expand All @@ -40,14 +37,12 @@ def __init__(
has_cf_headers: bool = False,
debug_format: str = _DEBUG_FORMAT,
msg_format: str = _MSG_FORMAT,
file_msg_format: str = _FILE_MSG_FORMAT,
):
super().__init__(app)
self.has_proxy_headers = has_proxy_headers
self.has_cf_headers = has_cf_headers
self.debug_format = debug_format
self.msg_format = msg_format
self.file_msg_format = file_msg_format

async def dispatch(self, request: Request, call_next) -> Response:
_logger = logger.opt(colors=True, record=True)
Expand Down Expand Up @@ -236,10 +231,7 @@ async def dispatch(self, request: Request, call_next) -> Response:
_msg = _msg_format.format(**_http_info)
# _logger.bind(http_info=_http_info).log(_LEVEL, _msg)
await run_in_threadpool(
_logger.bind(
http_info=_http_info,
http_file_msg_format=self.file_msg_format,
).log,
_logger.bind(http_info=_http_info).log,
_LEVEL,
_msg,
)
Expand Down
1 change: 0 additions & 1 deletion examples/advanced/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ async def lifespan(app: FastAPI):
has_proxy_headers=True,
debug_format=logger_loader.config.extra.http_std_debug_format,
msg_format=logger_loader.config.extra.http_std_msg_format,
file_msg_format=logger_loader.config.extra.http_file_msg_format,
)


Expand Down
1 change: 0 additions & 1 deletion examples/advanced/configs/logger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ logger:
http_std_debug_format: '<n>[{request_id}]</n> {client_host} {user_id} "<u>{method} {url_path}</u> HTTP/{http_version}"'
http_std_msg_format: '<n><w>[{request_id}]</w></n> {client_host} {user_id} "<u>{method} {url_path}</u> HTTP/{http_version}" {status_code} {content_length}B {response_time}ms'
http_file_enabled: true
http_file_msg_format: '{client_host} {request_id} {user_id} [{datetime}] "{method} {url_path} HTTP/{http_version}" {status_code} {content_length} "{h_referer}" "{h_user_agent}" {response_time}'
http_log_path: "http/{app_name}.http.access.log"
http_err_path: "http/{app_name}.http.err.log"
http_json_enabled: true
Expand Down