mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-04-24 07:17:17 +00:00
check log record in handler instead of client
This commit is contained in:
parent
81fa73f725
commit
ae9118654d
@ -74,7 +74,11 @@ class HttpLogHandler(logging.Handler):
|
|||||||
Args:
|
Args:
|
||||||
record(logging.LogRecord): log record to log
|
record(logging.LogRecord): log record to log
|
||||||
"""
|
"""
|
||||||
|
package_base = getattr(record, "package_base", None)
|
||||||
|
if package_base is None:
|
||||||
|
return # in case if no package base supplied we need just skip log message
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.reporter.logs(record)
|
self.reporter.logs(package_base, record)
|
||||||
except Exception:
|
except Exception:
|
||||||
self.handleError(record)
|
self.handleError(record)
|
||||||
|
@ -89,11 +89,12 @@ class Client:
|
|||||||
"""
|
"""
|
||||||
return InternalStatus(status=BuildStatus())
|
return InternalStatus(status=BuildStatus())
|
||||||
|
|
||||||
def logs(self, record: logging.LogRecord) -> None:
|
def logs(self, package_base: str, record: logging.LogRecord) -> None:
|
||||||
"""
|
"""
|
||||||
post log record
|
post log record
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
package_base(str) package base
|
||||||
record(logging.LogRecord): log record to post to api
|
record(logging.LogRecord): log record to post to api
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -204,17 +204,14 @@ class WebClient(Client, LazyLogging):
|
|||||||
self.logger.exception("could not get web service status")
|
self.logger.exception("could not get web service status")
|
||||||
return InternalStatus(status=BuildStatus())
|
return InternalStatus(status=BuildStatus())
|
||||||
|
|
||||||
def logs(self, record: logging.LogRecord) -> None:
|
def logs(self, package_base: str, record: logging.LogRecord) -> None:
|
||||||
"""
|
"""
|
||||||
post log record
|
post log record
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
package_base(str) package base
|
||||||
record(logging.LogRecord): log record to post to api
|
record(logging.LogRecord): log record to post to api
|
||||||
"""
|
"""
|
||||||
package_base = getattr(record, "package_base", None)
|
|
||||||
if package_base is None:
|
|
||||||
return # in case if no package base supplised we need just skip log message
|
|
||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
"created": record.created,
|
"created": record.created,
|
||||||
"message": record.getMessage(),
|
"message": record.getMessage(),
|
||||||
|
@ -4,6 +4,7 @@ from pytest_mock import MockerFixture
|
|||||||
|
|
||||||
from ahriman.core.configuration import Configuration
|
from ahriman.core.configuration import Configuration
|
||||||
from ahriman.core.log.http_log_handler import HttpLogHandler
|
from ahriman.core.log.http_log_handler import HttpLogHandler
|
||||||
|
from ahriman.models.package import Package
|
||||||
|
|
||||||
|
|
||||||
def test_load(configuration: Configuration, mocker: MockerFixture) -> None:
|
def test_load(configuration: Configuration, mocker: MockerFixture) -> None:
|
||||||
@ -33,24 +34,40 @@ def test_load_exist(configuration: Configuration) -> None:
|
|||||||
assert handler is new_handler
|
assert handler is new_handler
|
||||||
|
|
||||||
|
|
||||||
def test_emit(configuration: Configuration, log_record: logging.LogRecord, mocker: MockerFixture) -> None:
|
def test_emit(configuration: Configuration, log_record: logging.LogRecord, package_ahriman: Package,
|
||||||
|
mocker: MockerFixture) -> None:
|
||||||
"""
|
"""
|
||||||
must emit log record to reporter
|
must emit log record to reporter
|
||||||
"""
|
"""
|
||||||
|
log_record.package_base = package_ahriman.base
|
||||||
log_mock = mocker.patch("ahriman.core.status.client.Client.logs")
|
log_mock = mocker.patch("ahriman.core.status.client.Client.logs")
|
||||||
|
|
||||||
handler = HttpLogHandler(configuration, report=False)
|
handler = HttpLogHandler(configuration, report=False)
|
||||||
|
|
||||||
handler.emit(log_record)
|
handler.emit(log_record)
|
||||||
log_mock.assert_called_once_with(log_record)
|
log_mock.assert_called_once_with(package_ahriman.base, log_record)
|
||||||
|
|
||||||
|
|
||||||
def test_emit_failed(configuration: Configuration, log_record: logging.LogRecord, mocker: MockerFixture) -> None:
|
def test_emit_failed(configuration: Configuration, log_record: logging.LogRecord, package_ahriman: Package,
|
||||||
|
mocker: MockerFixture) -> None:
|
||||||
"""
|
"""
|
||||||
must call handle error on exception
|
must call handle error on exception
|
||||||
"""
|
"""
|
||||||
|
log_record.package_base = package_ahriman.base
|
||||||
mocker.patch("ahriman.core.status.client.Client.logs", side_effect=Exception())
|
mocker.patch("ahriman.core.status.client.Client.logs", side_effect=Exception())
|
||||||
handle_error_mock = mocker.patch("logging.Handler.handleError")
|
handle_error_mock = mocker.patch("logging.Handler.handleError")
|
||||||
handler = HttpLogHandler(configuration, report=False)
|
handler = HttpLogHandler(configuration, report=False)
|
||||||
|
|
||||||
handler.emit(log_record)
|
handler.emit(log_record)
|
||||||
handle_error_mock.assert_called_once_with(log_record)
|
handle_error_mock.assert_called_once_with(log_record)
|
||||||
|
|
||||||
|
|
||||||
|
def test_emit_skip(configuration: Configuration, log_record: logging.LogRecord, mocker: MockerFixture) -> None:
|
||||||
|
"""
|
||||||
|
must skip log record posting if no package base set
|
||||||
|
"""
|
||||||
|
log_mock = mocker.patch("ahriman.core.status.client.Client.logs")
|
||||||
|
handler = HttpLogHandler(configuration, report=False)
|
||||||
|
|
||||||
|
handler.emit(log_record)
|
||||||
|
log_mock.assert_not_called()
|
||||||
|
@ -68,11 +68,11 @@ def test_get_internal(client: Client) -> None:
|
|||||||
assert actual == expected
|
assert actual == expected
|
||||||
|
|
||||||
|
|
||||||
def test_log(client: Client, log_record: logging.LogRecord) -> None:
|
def test_log(client: Client, package_ahriman: Package, log_record: logging.LogRecord) -> None:
|
||||||
"""
|
"""
|
||||||
must process log record without errors
|
must process log record without errors
|
||||||
"""
|
"""
|
||||||
client.logs(log_record)
|
client.logs(package_ahriman.base, log_record)
|
||||||
|
|
||||||
|
|
||||||
def test_remove(client: Client, package_ahriman: Package) -> None:
|
def test_remove(client: Client, package_ahriman: Package) -> None:
|
||||||
|
@ -14,7 +14,7 @@ from ahriman.models.package import Package
|
|||||||
from ahriman.models.user import User
|
from ahriman.models.user import User
|
||||||
|
|
||||||
|
|
||||||
def test_status_url(web_client: WebClient) -> None:
|
def test_login_url(web_client: WebClient) -> None:
|
||||||
"""
|
"""
|
||||||
must generate login url correctly
|
must generate login url correctly
|
||||||
"""
|
"""
|
||||||
@ -215,14 +215,13 @@ def test_logs(web_client: WebClient, log_record: logging.LogRecord, package_ahri
|
|||||||
must process log record
|
must process log record
|
||||||
"""
|
"""
|
||||||
requests_mock = mocker.patch("requests.Session.post")
|
requests_mock = mocker.patch("requests.Session.post")
|
||||||
log_record.package_base = package_ahriman.base
|
|
||||||
payload = {
|
payload = {
|
||||||
"created": log_record.created,
|
"created": log_record.created,
|
||||||
"message": log_record.getMessage(),
|
"message": log_record.getMessage(),
|
||||||
"process_id": log_record.process,
|
"process_id": log_record.process,
|
||||||
}
|
}
|
||||||
|
|
||||||
web_client.logs(log_record)
|
web_client.logs(package_ahriman.base, log_record)
|
||||||
requests_mock.assert_called_once_with(pytest.helpers.anyvar(str, True), json=payload)
|
requests_mock.assert_called_once_with(pytest.helpers.anyvar(str, True), json=payload)
|
||||||
|
|
||||||
|
|
||||||
@ -234,16 +233,7 @@ def test_log_failed(web_client: WebClient, log_record: logging.LogRecord, packag
|
|||||||
mocker.patch("requests.Session.post", side_effect=Exception())
|
mocker.patch("requests.Session.post", side_effect=Exception())
|
||||||
log_record.package_base = package_ahriman.base
|
log_record.package_base = package_ahriman.base
|
||||||
with pytest.raises(Exception):
|
with pytest.raises(Exception):
|
||||||
web_client.logs(log_record)
|
web_client.logs(package_ahriman.base, log_record)
|
||||||
|
|
||||||
|
|
||||||
def test_log_skip(web_client: WebClient, log_record: logging.LogRecord, mocker: MockerFixture) -> None:
|
|
||||||
"""
|
|
||||||
must skip log record posting if no package base set
|
|
||||||
"""
|
|
||||||
requests_mock = mocker.patch("requests.Session.post")
|
|
||||||
web_client.logs(log_record)
|
|
||||||
requests_mock.assert_not_called()
|
|
||||||
|
|
||||||
|
|
||||||
def test_remove(web_client: WebClient, package_ahriman: Package, mocker: MockerFixture) -> None:
|
def test_remove(web_client: WebClient, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||||
|
Loading…
Reference in New Issue
Block a user