create custom logger to log at debug level webhook requests

This commit is contained in:
HgO 2022-10-29 14:18:16 +02:00
parent 21c568312b
commit 17f24cb086
3 changed files with 41 additions and 5 deletions

View file

@ -92,18 +92,20 @@ ENV PATH="$VIRTUAL_ENV/bin:$PATH"
WORKDIR "${PROJECT_DIR}"
# Install any native runtime dependencies
RUN apk add --no-cache libstdc++
# Copy python dependencies from the "builder" container
COPY --from=builder "${PROJECT_DIR}" "${PROJECT_DIR}"
# Copy libolm from the "builder" container
COPY --from=builder /usr/local/lib/libolm* /usr/local/lib/
# Install any native runtime dependencies
RUN apk add --no-cache libstdc++
# Specify a volume that holds the config file, SQLite3 database,
# and the matrix-nio store
VOLUME ["/data"]
# Start the bot
CMD ["matrix-alertbot", "/data/config.yaml"]
EXPOSE 8080

View file

@ -40,6 +40,8 @@ services:
restart: always
networks:
- matrix-alertbot
ports:
- 8080:8080
volumes:
- matrix-alertbot:/data
# Used for allowing connections to homeservers hosted on the host machine
@ -48,4 +50,3 @@ services:
# Defaults to 127.0.0.1 and is set in docker/.env
extra_hosts:
- "localhost:${HOST_IP_ADDRESS}"

View file

@ -4,6 +4,7 @@ import logging
import prometheus_client
from aiohttp import ClientError, web, web_request
from aiohttp.abc import AbstractAccessLogger
from aiohttp_prometheus_exporter.handler import metrics
from aiohttp_prometheus_exporter.middleware import prometheus_middleware_factory
from diskcache import Cache
@ -24,6 +25,38 @@ logger = logging.getLogger(__name__)
routes = web.RouteTableDef()
class AccessLogger(AbstractAccessLogger):
def log(
self,
request: web_request.BaseRequest,
response: web.StreamResponse,
time: float,
) -> None:
if request is None:
remote_address = "-"
request_info = "-"
referer_header = "-"
user_agent_header = "-"
else:
if request.remote is None:
remote_address = "-"
else:
remote_address = request.remote
request_info = (
f"{request.method} {request.path_qs} "
f"HTTP/{request.version.major}.{request.version.minor}"
)
referer_header = request.headers.get("Referer", "-")
user_agent_header = request.headers.get("User-Agent", "-")
self.logger.debug(
f'{remote_address} "{request_info}" {response.status} '
f'{response.body_length} "{referer_header}" "{user_agent_header}"'
)
@routes.get("/health")
async def get_health(request: web_request.Request) -> web.Response:
return web.Response(status=200)
@ -153,7 +186,7 @@ class Webhook:
)
self.app.router.add_get("/metrics", metrics())
self.runner = web.AppRunner(self.app)
self.runner = web.AppRunner(self.app, access_log_class=AccessLogger)
self.config = config
self.address = config.address