def test_listen_to(self): sub_loggo_facility = "a sub logger" sub_loggo = Loggo(facility=sub_loggo_facility) self.loggo.listen_to(sub_loggo_facility) self.loggo.log = Mock() # type: ignore warn = "The parent logger should log this message after sublogger logs it" sub_loggo.log(logging.WARNING, warn) self.loggo.log.assert_called_with(logging.WARNING, warn, ANY) # type: ignore
def setUp(self): self.log_msg = 'This is a message that can be used when the content does not matter.' self.log_data = { 'This is': 'log data', 'that can be': 'used when content does not matter' } self.loggo = Loggo(do_print=True, do_write=True, log_if_graylog_disabled=False) self.log = self.loggo.log
def setUp(self): self.log_msg = "This is a message that can be used when the content does not matter." self.log_data = { "This is": "log data", "that can be": "used when content does not matter" } self.loggo = Loggo(do_print=True, do_write=True, log_if_graylog_disabled=False) self.log = self.loggo.log
def test_int_truncation(self): """Test that large ints in log data are truncated.""" truncation = 100 loggo = Loggo(truncation=truncation) msg = "This is simply a test of the int truncation inside the log." large_number = 10 ** (truncation + 1) log_data = {"key": large_number} with patch("logging.Logger.log") as mock_log: loggo.log(logging.INFO, msg, log_data) mock_log.assert_called_with(20, msg, extra=ANY) logger_was_passed = mock_log.call_args[1]["extra"]["key"] truncation_suffix = "..." done_by_hand = str(large_number)[: truncation - len(truncation_suffix)] + truncation_suffix assert logger_was_passed == done_by_hand
def test_trace_truncation(self): """Test that trace is truncated correctly.""" trace_truncation = 100 loggo = Loggo(trace_truncation=trace_truncation) for trace_key in {"trace", "traceback"}: msg = "This is simply a test of the int truncation inside the log." large_number = 10 ** (trace_truncation + 1) log_data = {trace_key: large_number} with patch("logging.Logger.log") as mock_log: loggo.log(logging.INFO, msg, log_data) mock_log.assert_called_with(20, msg, extra=ANY) logger_was_passed = mock_log.call_args[1]["extra"][trace_key] truncation_suffix = "..." done_by_hand = str(large_number)[: trace_truncation - len(truncation_suffix)] + truncation_suffix assert logger_was_passed == done_by_hand
import unittest from unittest.mock import patch from loggo import Loggo loggo = Loggo(log_if_graylog_disabled=False) @loggo class AllMethodTypes: def __secret__(self): """a method that should never be logged.""" return True def public(self): """normal method.""" return True @classmethod def cl(cls): """class method.""" return True @staticmethod def st(): """static method.""" return True @loggo def doubled(self): """Loggo twice, bad but shouldn't kill."""
import unittest from unittest.mock import patch from typing import Mapping, Optional from loggo import Loggo # without the Mapping annotation this fails, apparently due to mypy problems strings = dict(called='Log string {call_signature}', returned='Log string for return', errored='Log string on exception') # type: Mapping[str, str] custom_strings = Loggo(log_if_graylog_disabled=False, **strings) nocalled = dict( called=None, returned='Log string for return', returned_none='Returned none!', errored='Log string on exception') # type: Mapping[str, Optional[str]] custom_none_string = Loggo(log_if_graylog_disabled=False, **nocalled) # custom message test data @custom_strings def custom_success(): return 1 @custom_strings def custom_none_user_returned(): return
import os import sys from typing import Any, Mapping from unittest.mock import ANY, Mock, call, mock_open, patch import pytest from loggo import Loggo test_setup: Mapping[str, Any] = { "do_write": True, "log_if_graylog_disabled": False, "private_data": {"mnemonic", "priv"}, } loggo = Loggo(**test_setup) @loggo def function_with_private_arg(priv, acceptable=True): return acceptable @loggo def function_with_private_kwarg(number, a_float=0.0, mnemonic=None): return number * a_float # we can also use loggo.__call__ @loggo def may_or_may_not_error_test(first, other, kwargs=None):