Skip to content

Commit e23f3a3

Browse files
committed
Configure PythonHere logging defaults
1 parent 0603672 commit e23f3a3

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

pythonhere/main.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
startup_script_exception = None # pylint: disable=invalid-name
1313

1414
import asyncio
15+
import logging
1516
import os
1617
import sys
1718
import threading
1819
from pathlib import Path
1920
from typing import Any
2021

22+
import asyncssh
2123
from enum_here import ScreenName, ServerState
2224
from exception_manager_here import install_exception_handler, show_exception_popup
2325
from kivy.app import App
@@ -31,6 +33,12 @@
3133
monkeypatch_kivy()
3234

3335

36+
def configure_logging():
37+
"""Configure logging for the app runtime."""
38+
logging.getLogger().setLevel(logging.INFO)
39+
asyncssh.set_log_level("WARNING")
40+
41+
3442
class PythonHereApp(App):
3543
"""PythonHere main app."""
3644

@@ -217,6 +225,7 @@ def chdir(self, path: str):
217225

218226
async def main():
219227
"""Run PythonHere."""
228+
configure_logging()
220229
app = PythonHereApp()
221230
await app.run_app()
222231

tests/test_main.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import asyncio
2+
import logging
23
from pathlib import Path
34
from types import SimpleNamespace
45
from unittest.mock import PropertyMock
56

67
import pytest
78
from asyncssh import PermissionDenied
89
from enum_here import ScreenName, ServerState
9-
from main import PythonHereApp
10+
from main import PythonHereApp, configure_logging, main
1011
from ui_here.server_screen_here import ServerScreenManager
1112
from version_here import __version__
1213

@@ -15,6 +16,36 @@ def test_dev_version_is_set():
1516
assert __version__ == "0.0.0"
1617

1718

19+
def test_configure_logging_sets_default_info_and_asyncssh_warning(mocker):
20+
set_log_level = mocker.patch("main.asyncssh.set_log_level")
21+
root_logger = logging.getLogger()
22+
23+
original_level = root_logger.level
24+
try:
25+
root_logger.setLevel(logging.NOTSET)
26+
27+
configure_logging()
28+
29+
assert root_logger.level == logging.INFO
30+
set_log_level.assert_called_once_with("WARNING")
31+
finally:
32+
root_logger.setLevel(original_level)
33+
34+
35+
@pytest.mark.asyncio
36+
async def test_main_configures_logging_before_running_app(mocker):
37+
configure = mocker.patch("main.configure_logging")
38+
app = mocker.Mock()
39+
app.run_app = mocker.AsyncMock()
40+
app_class = mocker.patch("main.PythonHereApp", return_value=app)
41+
42+
await main()
43+
44+
configure.assert_called_once_with()
45+
app_class.assert_called_once_with()
46+
app.run_app.assert_awaited_once_with()
47+
48+
1849
def test_server_screen_update_states(mocker):
1950
screen = SimpleNamespace(current=None)
2051
app = SimpleNamespace(

0 commit comments

Comments
 (0)