Skip to content

Commit dc498cc

Browse files
committed
add test
1 parent 7376cf0 commit dc498cc

File tree

1 file changed

+151
-2
lines changed

1 file changed

+151
-2
lines changed

packages/service-library/tests/test_logging_utils.py

Lines changed: 151 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
log_decorator,
1919
log_exceptions,
2020
set_parent_module_log_level,
21+
setup_async_loggers,
2122
)
2223

2324
_logger = logging.getLogger(__name__)
@@ -325,8 +326,9 @@ def test_log_exceptions_and_suppress_without_exc_info(
325326
caplog.set_level(level)
326327

327328
exc_msg = "logs exceptions and suppresses"
328-
with suppress(ValueError), log_exceptions(
329-
_logger, level, "CONTEXT", exc_info=False
329+
with (
330+
suppress(ValueError),
331+
log_exceptions(_logger, level, "CONTEXT", exc_info=False),
330332
):
331333
raise ValueError(exc_msg)
332334

@@ -410,3 +412,150 @@ def test_set_parent_module_log_level_(caplog: pytest.LogCaptureFixture):
410412

411413
assert "parent warning" in caplog.text
412414
assert "child warning" in caplog.text
415+
416+
417+
@pytest.mark.parametrize("log_format_local_dev_enabled", [True, False])
418+
async def test_setup_async_loggers_basic(
419+
caplog: pytest.LogCaptureFixture,
420+
log_format_local_dev_enabled: bool,
421+
):
422+
"""Test basic async logging setup without filters."""
423+
caplog.clear()
424+
caplog.set_level(logging.INFO)
425+
426+
async with setup_async_loggers(
427+
log_format_local_dev_enabled=log_format_local_dev_enabled,
428+
):
429+
test_logger = logging.getLogger("test_async_logger")
430+
test_logger.info("Test async log message")
431+
432+
# Give some time for async logging to process
433+
import asyncio
434+
435+
await asyncio.sleep(0.1)
436+
437+
# Check that the log message was captured
438+
assert "Test async log message" in caplog.text
439+
assert "Async logging setup completed" in caplog.text
440+
441+
442+
async def test_setup_async_loggers_with_filters(
443+
caplog: pytest.LogCaptureFixture,
444+
):
445+
"""Test async logging setup with logger filters."""
446+
caplog.clear()
447+
caplog.set_level(logging.INFO)
448+
449+
# Define filter mapping
450+
filter_mapping = {
451+
"test_filtered_logger": ["filtered_message"],
452+
}
453+
454+
async with setup_async_loggers(
455+
log_format_local_dev_enabled=True,
456+
logger_filter_mapping=filter_mapping,
457+
):
458+
test_logger = logging.getLogger("test_filtered_logger")
459+
unfiltered_logger = logging.getLogger("test_unfiltered_logger")
460+
461+
# This should be filtered out
462+
test_logger.info("This is a filtered_message")
463+
464+
# This should pass through
465+
test_logger.info("This is an unfiltered message")
466+
unfiltered_logger.info("This is from unfiltered logger")
467+
468+
# Give some time for async logging to process
469+
import asyncio
470+
471+
await asyncio.sleep(0.1)
472+
473+
# Check that filtered message was not captured
474+
assert "This is a filtered_message" not in caplog.text
475+
476+
# Check that unfiltered messages were captured
477+
assert "This is an unfiltered message" in caplog.text
478+
assert "This is from unfiltered logger" in caplog.text
479+
480+
481+
async def test_setup_async_loggers_with_tracing_settings(
482+
caplog: pytest.LogCaptureFixture,
483+
):
484+
"""Test async logging setup with tracing settings."""
485+
caplog.clear()
486+
caplog.set_level(logging.INFO)
487+
488+
# Note: We can't easily test actual tracing without setting up OpenTelemetry
489+
# But we can test that the function accepts the parameter
490+
async with setup_async_loggers(
491+
log_format_local_dev_enabled=False,
492+
tracing_settings=None, # Would normally be TracingSettings object
493+
):
494+
test_logger = logging.getLogger("test_tracing_logger")
495+
test_logger.info("Test message with tracing settings")
496+
497+
# Give some time for async logging to process
498+
import asyncio
499+
500+
await asyncio.sleep(0.1)
501+
502+
assert "Test message with tracing settings" in caplog.text
503+
504+
505+
async def test_setup_async_loggers_context_manager_cleanup(
506+
caplog: pytest.LogCaptureFixture,
507+
):
508+
"""Test that async logging context manager properly cleans up."""
509+
caplog.clear()
510+
caplog.set_level(logging.DEBUG)
511+
512+
test_logger = logging.getLogger("test_cleanup_logger")
513+
514+
async with setup_async_loggers(log_format_local_dev_enabled=True):
515+
# During the context, handlers should be replaced
516+
test_logger.info("Message during context")
517+
518+
# Give some time for async logging to process
519+
import asyncio
520+
521+
await asyncio.sleep(0.1)
522+
523+
# After context exit, check cleanup message
524+
assert "Async logging context exiting" in caplog.text
525+
526+
# Note: We can't easily test handler restoration without more complex setup
527+
# but we can verify the function completed without errors
528+
529+
530+
async def test_setup_async_loggers_exception_handling(
531+
caplog: pytest.LogCaptureFixture,
532+
):
533+
"""Test that async logging handles exceptions gracefully."""
534+
caplog.clear()
535+
caplog.set_level(logging.DEBUG) # Set to DEBUG to capture cleanup messages
536+
537+
def _raise_test_exception():
538+
"""Helper function to raise exception for testing."""
539+
exc_msg = "Test exception"
540+
raise ValueError(exc_msg)
541+
542+
try:
543+
async with setup_async_loggers(log_format_local_dev_enabled=True):
544+
test_logger = logging.getLogger("test_exception_logger")
545+
test_logger.info("Message before exception")
546+
547+
# Give some time for async logging to process
548+
import asyncio
549+
550+
await asyncio.sleep(0.1)
551+
552+
# Raise an exception to test cleanup
553+
_raise_test_exception()
554+
555+
except ValueError:
556+
# Expected exception
557+
pass
558+
559+
# Check that the message was logged and cleanup happened
560+
assert "Message before exception" in caplog.text
561+
assert "Async logging context exiting" in caplog.text

0 commit comments

Comments
 (0)