Skip to content

Commit 228d7df

Browse files
Using debug logs
1 parent f46ea0d commit 228d7df

5 files changed

Lines changed: 29 additions & 15 deletions

File tree

app/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Settings(BaseSettings):
1919
app_db_min_con: int = 5
2020
app_db_max_con: int = 20
2121
app_db_log_sql: bool = False
22+
app_retry_consumer_log_debug: bool = False
2223
# db settings end
2324

2425
# rabbit mq broker settings start

app/consumer/retry_consumer.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313

1414
async def process_retry_message_task(message: AbstractIncomingMessage):
15-
logger = structlog.get_logger()
15+
logger = structlog.get_logger("retry_consumer")
1616
async with message.process(): # Automatically ACKs if no exception occurs
1717
payload_string: str = message.body.decode()
1818
headers = dict(message.headers) if message.headers else {}
19-
logger.info(
19+
logger.debug(
2020
f"Received retry message {payload_string}",
2121
payload=payload_string,
2222
headers=headers,
@@ -41,17 +41,17 @@ async def process_retry_message_task(message: AbstractIncomingMessage):
4141
await publish_to_inbound_retry(
4242
payload_string=payload_string, headers=headers
4343
)
44+
logger.debug(
45+
f"Sent to retry queue {payload_string}",
46+
headers=headers,
47+
payload_string=payload_string,
48+
)
4449
else:
4550
logger.info(
4651
f"Max retries exhausted, discarding message {payload_string}",
4752
payload=payload_string,
4853
headers=headers,
4954
)
50-
logger.info(
51-
f"Processed retry message {payload_string}",
52-
headers=headers,
53-
payload_string=payload_string,
54-
)
5555

5656

5757
async def start_retry_consumer() -> Task[str]:

app/logging/logging_config.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
from structlog.contextvars import merge_contextvars
77
from structlog.stdlib import LoggerFactory
88

9+
from app.config import Settings
910

10-
def setup_logging(json_logs: bool = False, db_logs: bool = False):
11+
12+
def setup_logging(settings: Settings):
1113
shared_processors = [
1214
CallsiteParameterAdder(
1315
{
@@ -22,7 +24,7 @@ def setup_logging(json_logs: bool = False, db_logs: bool = False):
2224
merge_contextvars,
2325
]
2426

25-
if json_logs:
27+
if settings.app_json_logs:
2628
log_renderer = structlog.processors.JSONRenderer()
2729
else:
2830
log_renderer = structlog.dev.ConsoleRenderer()
@@ -37,9 +39,12 @@ def setup_logging(json_logs: bool = False, db_logs: bool = False):
3739
handlers=[logging.StreamHandler(sys.stdout)],
3840
)
3941

40-
if db_logs:
42+
if settings.app_db_log_sql:
4143
logging.getLogger("databases").setLevel(logging.DEBUG)
4244

45+
if settings.app_retry_consumer_log_debug:
46+
logging.getLogger("retry_consumer").setLevel(logging.DEBUG)
47+
4348
structlog.configure(
4449
processors=processors,
4550
logger_factory=LoggerFactory(),

app/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from app.consumer.retry_consumer import start_retry_consumer
4242

4343
# Initialize logging at application startup
44-
setup_logging(json_logs=settings.app_json_logs, db_logs=settings.app_db_log_sql)
44+
setup_logging(settings)
4545
logger = structlog.get_logger()
4646

4747
context = FastAPI(openapi_url=settings.app_open_api_url)

tests_integration/producer/test_inbound_producer.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,16 @@ async def publish_outbound_side_effect_logic(*args, **kwargs):
147147
):
148148
with attempt:
149149
assert_that(len(captured_logs)).is_greater_than(0)
150-
retry_consumer_logs = list(
150+
retry_sent_to_inbound_logs = list(
151151
filter(
152152
lambda entry: str(entry["event"]).startswith(
153153
"Sent to inbound queue"
154154
),
155155
captured_logs,
156156
)
157157
)
158-
assert_that(retry_consumer_logs).is_length(2)
159-
retry_headers: HeadersType = retry_consumer_logs[0]["headers"]
158+
assert_that(retry_sent_to_inbound_logs).is_length(2)
159+
retry_headers: HeadersType = retry_sent_to_inbound_logs[0]["headers"]
160160
assert_that(retry_headers.get("test")).is_equal_to(headers.get("test"))
161161
assert_that(retry_headers.get("message_id")).is_equal_to(
162162
headers.get("message_id")
@@ -170,7 +170,7 @@ async def publish_outbound_side_effect_logic(*args, **kwargs):
170170
datetime.now(UTC) - timedelta(seconds=10),
171171
datetime.now(UTC) + timedelta(seconds=10),
172172
)
173-
retry_payload_string = retry_consumer_logs[0]["payload_string"]
173+
retry_payload_string = retry_sent_to_inbound_logs[0]["payload_string"]
174174
outbound_samples: list[dict[str, Any]] = json.loads(retry_payload_string)
175175
assert_that(
176176
DeepDiff(
@@ -180,6 +180,14 @@ async def publish_outbound_side_effect_logic(*args, **kwargs):
180180
)
181181
).is_empty()
182182

183+
retry_sent_to_retry_logs = list(
184+
filter(
185+
lambda entry: str(entry["event"]).startswith("Sent to retry queue"),
186+
captured_logs,
187+
)
188+
)
189+
assert_that(len(retry_sent_to_retry_logs)).is_greater_than(1)
190+
183191
for attempt in Retrying(
184192
stop=stop_after_delay(5), wait=wait_fixed(0.5), reraise=True
185193
):

0 commit comments

Comments
 (0)