diff --git a/tests/discovery/test_githubcode.py b/tests/discovery/test_githubcode.py index a1e6e033..853771a3 100644 --- a/tests/discovery/test_githubcode.py +++ b/tests/discovery/test_githubcode.py @@ -66,7 +66,6 @@ class TestSearchGithubCode: test_result = await test_class_instance.fragments_from_response( self.OkResponse().response.json() ) - print("test_result: ", test_result) assert test_result == ["test1", "test2"] @pytest.mark.asyncio diff --git a/tests/test_logging.py b/tests/test_logging.py index 25e5b9d5..bb779225 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -54,6 +54,26 @@ def test_operator_output_uses_stdout_without_verbose_logging() -> None: assert result.stderr == '' +def test_api_example_entry_point_configures_output_and_diagnostics() -> None: + result = run_python( + """ + import logging + from theHarvester.lib.api import api_example + from theHarvester.lib.output import output_logger + + async def fake_main(): + output_logger.info('example result') + logging.getLogger(api_example.__name__).info('example diagnostic') + + api_example.main = fake_main + api_example.entry_point() + """ + ) + + assert result.stdout == 'example result\n' + assert 'INFO theHarvester.lib.api.api_example: example diagnostic' in result.stderr + + def test_diagnostics_use_stderr_only_when_verbose() -> None: result = run_python( """ @@ -107,6 +127,7 @@ except SystemExit: def test_cli_preserves_host_logging_unless_verbose_is_requested(tmp_path: Path) -> None: script = """import asyncio import logging +import sys from theHarvester.__main__ import start root_logger = logging.getLogger() @@ -118,7 +139,7 @@ package_logger.setLevel(logging.ERROR) try: asyncio.run(start()) except SystemExit: - print(handler in root_logger.handlers, package_logger.level) + sys.stdout.write(f'{handler in root_logger.handlers} {package_logger.level}\\n') raise """ command = [ diff --git a/theHarvester/lib/api/api_example.py b/theHarvester/lib/api/api_example.py index 25c90397..5de6bb85 100644 --- a/theHarvester/lib/api/api_example.py +++ b/theHarvester/lib/api/api_example.py @@ -1,11 +1,14 @@ """Example script to query theHarvester rest API, obtain results, and write out to stdout as well as an html""" import asyncio +import logging import aiohttp import netaddr -from theHarvester.lib.output import output_logger, print_section, sorted_unique +from theHarvester.lib.output import configure_logging, output_logger, print_section, sorted_unique + +logger = logging.getLogger(__name__) async def fetch_json(session, url): @@ -14,7 +17,7 @@ async def fetch_json(session, url): response.raise_for_status() # Raise an exception for 4XX/5XX responses return await response.json() except Exception as e: - output_logger.info(f'Error fetching data from {url}: {e}') + logger.info('Error fetching data from %s: %s', url, e) return {} @@ -24,7 +27,7 @@ async def fetch(session, url): response.raise_for_status() # Raise an exception for 4XX/5XX responses return await response.text() except Exception as e: - output_logger.info(f'Error fetching data from {url}: {e}') + logger.info('Error fetching data from %s: %s', url, e) return '' @@ -115,5 +118,10 @@ async def main() -> None: output_logger.info('\n'.join(hosts)) -if __name__ == '__main__': +def entry_point() -> None: + configure_logging(verbose=True) asyncio.run(main()) + + +if __name__ == '__main__': + entry_point()