Skip to content

Commit 77acbd8

Browse files
Copilotbittner
andcommitted
Initial analysis of logging issue with ArgvContext
Co-authored-by: bittner <[email protected]>
1 parent ca80705 commit 77acbd8

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

cli_test_logging.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import sys
2+
import logging
3+
4+
def main():
5+
logfile = sys.argv[1]
6+
print("main")
7+
print(f"Handlers before basicConfig: {logging.root.handlers}")
8+
logger = logging.getLogger(__name__)
9+
logging.basicConfig(filename=logfile, encoding='utf-8', level=logging.INFO)
10+
print(f"Handlers after basicConfig: {logging.root.handlers}")
11+
logger.info("This is an info message")
12+
13+
if __name__ == "__main__":
14+
main()

test_cli.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from pathlib import Path
2+
from cli_test_helpers import ArgvContext
3+
import logging
4+
5+
import cli_test_logging
6+
7+
def test_cli():
8+
print(f"Handlers at test start: {logging.root.handlers}")
9+
logfile = "test.log"
10+
with ArgvContext('cli_test_logging.py', logfile):
11+
cli_test_logging.main()
12+
print(f"Handlers after main: {logging.root.handlers}")
13+
assert Path(logfile).exists() # fails

test_cli_workaround.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from pathlib import Path
2+
from cli_test_helpers import ArgvContext
3+
import logging
4+
5+
import cli_test_logging
6+
7+
def test_cli_with_workaround():
8+
"""Test with logging handlers reset"""
9+
# Save and clear existing handlers
10+
original_handlers = logging.root.handlers[:]
11+
logging.root.handlers.clear()
12+
13+
logfile = "test.log"
14+
try:
15+
with ArgvContext('cli_test_logging.py', logfile):
16+
cli_test_logging.main()
17+
assert Path(logfile).exists()
18+
finally:
19+
# Restore original handlers
20+
logging.root.handlers = original_handlers
21+
# Clean up test file
22+
if Path(logfile).exists():
23+
Path(logfile).unlink()

0 commit comments

Comments
 (0)