Skip to content

Commit e41d3d8

Browse files
authored
Merge pull request #337 from ansforge/chore/converter-log-version
chore(converter): add version in logs
2 parents 258d80a + e571889 commit e41d3d8

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,4 @@ jobs:
7979
platforms: linux/amd64
8080
tags: ghcr.io/${{ github.repository_owner }}/hub-converter:${{ steps.extract_info.outputs.version }}
8181
context: ./converter
82+
build-args: CONVERTER_VERSION=${{ steps.extract_info.outputs.version }}

converter/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Use Python 3.11 slim as base image
22
FROM python:3.11-slim
33

4+
ARG CONVERTER_VERSION
5+
46
# Set environment variables
57
ENV PYTHONDONTWRITEBYTECODE=1 \
68
PYTHONUNBUFFERED=1 \
@@ -47,5 +49,7 @@ EXPOSE 8080
4749
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
4850
CMD curl -f http://localhost:8080/health || exit 1
4951

52+
ENV CONVERTER_VERSION=${CONVERTER_VERSION}
53+
5054
# Use Gunicorn for production deployment
5155
CMD ["uv", "run", "--no-dev", "gunicorn", "-w", "4", "-b", "0.0.0.0:8080", "converter.converter:app"]

converter/converter/logging_config.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from flask import has_request_context, g
55
from enum import Enum
66

7+
CONVERTER_VERSION = os.getenv("CONVERTER_VERSION")
8+
79

810
class LoggingKeys(Enum):
911
DISTRIBUTION_ID = "distributionId"
@@ -14,13 +16,20 @@ class LoggingKeys(Enum):
1416

1517
# Using filter to add context variable: https://docs.python.org/3/howto/logging-cookbook.html#using-filters-to-impart-contextual-information
1618
class DistributionContextFilter(logging.Filter):
17-
def filter(self, record):
19+
def filter(self, record: logging.LogRecord) -> bool:
1820
if has_request_context():
1921
for key in LoggingKeys:
2022
setattr(record, key.value, getattr(g, key.value, None))
2123
return True
2224

2325

26+
class VersionFilter(logging.Filter):
27+
def filter(self, record: logging.LogRecord) -> bool:
28+
if CONVERTER_VERSION:
29+
record.version = CONVERTER_VERSION
30+
return True
31+
32+
2433
def configure_logging():
2534
root = logging.getLogger()
2635
for h in root.handlers[:]:
@@ -37,6 +46,7 @@ def configure_logging():
3746
)
3847
handler.setFormatter(formatter)
3948
handler.addFilter(DistributionContextFilter())
49+
handler.addFilter(VersionFilter())
4050

4151
root.addHandler(handler)
4252

0 commit comments

Comments
 (0)