Skip to content

Commit

Permalink
✨ configurable log level
Browse files Browse the repository at this point in the history
  • Loading branch information
haliphax committed Apr 9, 2024
1 parent 1e2c0eb commit 5c2a86d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 10 deletions.
4 changes: 3 additions & 1 deletion data/config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ port = 6379
bind = "postgres://xthulu:xthulu@db:5432/xthulu"

[debug]
enabled = false
term = false

[logging]
level = "INFO"

[ssh]
host = "0.0.0.0"
host_keys = ["data/ssh_host_key"]
Expand Down
14 changes: 9 additions & 5 deletions tests/ssh/test_start_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Any

# stdlib
from logging import DEBUG, Logger
from unittest.async_case import IsolatedAsyncioTestCase
from unittest.mock import AsyncMock, Mock, patch

Expand Down Expand Up @@ -94,7 +95,6 @@ async def test_proxy_procotol(self, mock_listener: Mock):
patch_get_config(
{
**test_config,
"debug": {"enabled": True},
"ssh": {**test_ssh_config, "proxy_protocol": True},
}
),
Expand All @@ -103,8 +103,12 @@ async def test_proxy_procotol(self, mock_listener: Mock):
async def test_trace_malloc_start(self, mock_start: Mock):
"""Server should call tracemalloc.start if debugging is enabled."""

# act
await start_server()
with patch.object(Logger, "getEffectiveLevel") as mock_level:
# arrange
mock_level.return_value = DEBUG

# assert
mock_start.assert_called_once()
# act
await start_server()

# assert
mock_start.assert_called_once()
3 changes: 3 additions & 0 deletions xthulu/configuration/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"db": {
"bind": "postgres://xthulu:xthulu@db:5432/xthulu",
},
"logging": {
"level": "INFO",
},
"ssh": {
"auth": {
"bad_usernames": [
Expand Down
4 changes: 2 additions & 2 deletions xthulu/logger.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Logger setup"""

# stdlib
from logging import DEBUG, Formatter, getLogger, INFO, StreamHandler
from logging import Formatter, getLogger, StreamHandler
from logging.handlers import TimedRotatingFileHandler
from sys import stdout

Expand All @@ -26,4 +26,4 @@
streamHandler.setFormatter(formatter)
log.addHandler(fileHandler)
log.addHandler(streamHandler)
log.setLevel(DEBUG if bool(get_config("debug.enabled", False)) else INFO)
log.setLevel(str(get_config("logging.level", "INFO")).upper())
3 changes: 2 additions & 1 deletion xthulu/ssh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Any

# stdlib
from logging import DEBUG
from tracemalloc import start

# 3rd party
Expand Down Expand Up @@ -44,7 +45,7 @@ async def start_server():
log.info("Using PROXY protocol v1 listener")
kwargs["tunnel"] = ProxyProtocolListener()

if bool(get_config("debug.enabled", False)):
if log.getEffectiveLevel() == DEBUG:
start()

await res.db.set_bind(res.db.bind)
Expand Down
3 changes: 2 additions & 1 deletion xthulu/ssh/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""SSH server implementation"""

# stdlib
from logging import DEBUG
from secrets import compare_digest

# 3rd party
Expand All @@ -27,7 +28,7 @@ class SSHServer(AsyncSSHServer):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._debug_enabled = bool(get_config("debug.enabled", False))
self._debug_enabled = log.getEffectiveLevel() == DEBUG
self._no_entry = get_config("ssh.auth.bad_usernames", [])
self._no_password = get_config("ssh.auth.no_password", [])

Expand Down

0 comments on commit 5c2a86d

Please sign in to comment.