mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
Refactor logging code: Remove unnecessary blank lines in logging setup and cleanup functions for improved readability. Update tests to reflect formatting changes.
This commit is contained in:
@ -104,7 +104,7 @@ def setup_logging() -> None:
|
|||||||
if _current_listener is not None:
|
if _current_listener is not None:
|
||||||
_current_listener.stop()
|
_current_listener.stop()
|
||||||
_current_listener = None
|
_current_listener = None
|
||||||
|
|
||||||
# Close and clear existing handlers
|
# Close and clear existing handlers
|
||||||
for handler in _current_handlers:
|
for handler in _current_handlers:
|
||||||
if isinstance(handler, logging.FileHandler):
|
if isinstance(handler, logging.FileHandler):
|
||||||
@ -200,7 +200,7 @@ def setup_logging() -> None:
|
|||||||
|
|
||||||
# Store handlers globally for cleanup
|
# Store handlers globally for cleanup
|
||||||
_current_handlers.extend(handlers)
|
_current_handlers.extend(handlers)
|
||||||
|
|
||||||
# Start the listener AFTER configuring all loggers
|
# Start the listener AFTER configuring all loggers
|
||||||
_current_listener = logging.handlers.QueueListener(
|
_current_listener = logging.handlers.QueueListener(
|
||||||
log_queue, *handlers, respect_handler_level=True
|
log_queue, *handlers, respect_handler_level=True
|
||||||
@ -219,7 +219,7 @@ def cleanup_logging() -> None:
|
|||||||
if _current_listener is not None:
|
if _current_listener is not None:
|
||||||
_current_listener.stop()
|
_current_listener.stop()
|
||||||
_current_listener = None
|
_current_listener = None
|
||||||
|
|
||||||
# Close all file handlers to ensure proper cleanup on Windows
|
# Close all file handlers to ensure proper cleanup on Windows
|
||||||
for handler in _current_handlers:
|
for handler in _current_handlers:
|
||||||
if isinstance(handler, logging.FileHandler):
|
if isinstance(handler, logging.FileHandler):
|
||||||
|
|||||||
@ -180,10 +180,10 @@ async def test_custom_log_file(clean_env):
|
|||||||
# Stop the listener to ensure all messages are written
|
# Stop the listener to ensure all messages are written
|
||||||
if _current_listener is not None:
|
if _current_listener is not None:
|
||||||
_current_listener.stop()
|
_current_listener.stop()
|
||||||
|
|
||||||
# Give a moment for the listener to fully stop
|
# Give a moment for the listener to fully stop
|
||||||
await trio.sleep(0.05)
|
await trio.sleep(0.05)
|
||||||
|
|
||||||
# Close all file handlers to release the file
|
# Close all file handlers to release the file
|
||||||
for handler in _current_handlers:
|
for handler in _current_handlers:
|
||||||
if isinstance(handler, logging.FileHandler):
|
if isinstance(handler, logging.FileHandler):
|
||||||
@ -203,12 +203,14 @@ async def test_default_log_file(clean_env):
|
|||||||
|
|
||||||
with patch("libp2p.utils.paths.create_temp_file") as mock_create_temp:
|
with patch("libp2p.utils.paths.create_temp_file") as mock_create_temp:
|
||||||
# Mock the temp file creation to return a predictable path
|
# Mock the temp file creation to return a predictable path
|
||||||
mock_temp_file = Path(tempfile.gettempdir()) / "test_py-libp2p_20240101_120000.log"
|
mock_temp_file = (
|
||||||
|
Path(tempfile.gettempdir()) / "test_py-libp2p_20240101_120000.log"
|
||||||
|
)
|
||||||
mock_create_temp.return_value = mock_temp_file
|
mock_create_temp.return_value = mock_temp_file
|
||||||
|
|
||||||
# Remove the log file if it exists
|
# Remove the log file if it exists
|
||||||
mock_temp_file.unlink(missing_ok=True)
|
mock_temp_file.unlink(missing_ok=True)
|
||||||
|
|
||||||
setup_logging()
|
setup_logging()
|
||||||
|
|
||||||
# Wait for the listener to be ready
|
# Wait for the listener to be ready
|
||||||
@ -223,10 +225,10 @@ async def test_default_log_file(clean_env):
|
|||||||
# Stop the listener to ensure all messages are written
|
# Stop the listener to ensure all messages are written
|
||||||
if _current_listener is not None:
|
if _current_listener is not None:
|
||||||
_current_listener.stop()
|
_current_listener.stop()
|
||||||
|
|
||||||
# Give a moment for the listener to fully stop
|
# Give a moment for the listener to fully stop
|
||||||
await trio.sleep(0.05)
|
await trio.sleep(0.05)
|
||||||
|
|
||||||
# Close all file handlers to release the file
|
# Close all file handlers to release the file
|
||||||
for handler in _current_handlers:
|
for handler in _current_handlers:
|
||||||
if isinstance(handler, logging.FileHandler):
|
if isinstance(handler, logging.FileHandler):
|
||||||
|
|||||||
@ -220,11 +220,11 @@ class TestCrossPlatformCompatibility:
|
|||||||
def test_config_dir_platform_specific_windows(self, monkeypatch):
|
def test_config_dir_platform_specific_windows(self, monkeypatch):
|
||||||
"""Test config directory respects Windows conventions."""
|
"""Test config directory respects Windows conventions."""
|
||||||
import platform
|
import platform
|
||||||
|
|
||||||
# Only run this test on Windows systems
|
# Only run this test on Windows systems
|
||||||
if platform.system() != "Windows":
|
if platform.system() != "Windows":
|
||||||
pytest.skip("This test only runs on Windows systems")
|
pytest.skip("This test only runs on Windows systems")
|
||||||
|
|
||||||
monkeypatch.setattr("os.name", "nt")
|
monkeypatch.setattr("os.name", "nt")
|
||||||
monkeypatch.setenv("APPDATA", "C:\\Users\\Test\\AppData\\Roaming")
|
monkeypatch.setenv("APPDATA", "C:\\Users\\Test\\AppData\\Roaming")
|
||||||
config_dir = get_config_dir()
|
config_dir = get_config_dir()
|
||||||
|
|||||||
Reference in New Issue
Block a user