From 279eddb01f3243c98e665a3b7273306abc8f461f Mon Sep 17 00:00:00 2001 From: Italo Cunha Date: Wed, 16 Jun 2021 13:24:15 -0400 Subject: [PATCH] Use named loggers The library currently uses the root logger, which prevents applications from separating their logs from the library's. With this change, applications can silence or redirect sagan logs by reconfiguring the "ripe" logger. In particular, the following will print only `FATAL` messages from sagan: ``` def main(): ripe_logger = logging.getLogger("ripe") ripe_logger.setLevel(logging.FATAL) ... ``` close https://github.com/RIPE-NCC/ripe-atlas-sagan/issues/89 --- ripe/atlas/sagan/base.py | 7 +++++-- ripe/atlas/sagan/ssl.py | 2 +- ripe/atlas/sagan/traceroute.py | 7 +++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/ripe/atlas/sagan/base.py b/ripe/atlas/sagan/base.py index a13cbf9..95c825a 100644 --- a/ripe/atlas/sagan/base.py +++ b/ripe/atlas/sagan/base.py @@ -28,6 +28,9 @@ import json +log = logging.getLogger(__name__) + + class ResultParseError(Exception): pass @@ -138,14 +141,14 @@ def _handle_malformation(self, message): if self._on_malformation == self.ACTION_FAIL: raise ResultParseError(message) elif self._on_malformation == self.ACTION_WARN: - logging.warning(message) + log.warning(message) self.is_malformed = True def _handle_error(self, message): if self._on_error == self.ACTION_FAIL: raise ResultError(message) elif self._on_error == self.ACTION_WARN: - logging.warning(message) + log.warning(message) self.is_error = True self.error_message = message diff --git a/ripe/atlas/sagan/ssl.py b/ripe/atlas/sagan/ssl.py index 4f6db6b..23a984e 100644 --- a/ripe/atlas/sagan/ssl.py +++ b/ripe/atlas/sagan/ssl.py @@ -25,7 +25,7 @@ from cryptography.hazmat.backends import openssl from cryptography.hazmat.primitives import hashes except ImportError: - logging.warning( + logging.getLogger(__name__).warning( "cryptography module is not installed, without it you cannot parse SSL " "certificate measurement results" ) diff --git a/ripe/atlas/sagan/traceroute.py b/ripe/atlas/sagan/traceroute.py index 4787e62..0aebe8f 100644 --- a/ripe/atlas/sagan/traceroute.py +++ b/ripe/atlas/sagan/traceroute.py @@ -20,6 +20,9 @@ from .base import Result, ParsingDict +log = logging.getLogger(__name__) + + class IcmpHeader(ParsingDict): """ But why did we stop here? Why not go all the way and define subclasses for @@ -148,14 +151,14 @@ def __init__(self, data, **kwargs): @property def last_rtt(self): - logging.warning( + log.warning( '"last_rtt" is deprecated and will be removed in future versions. ' 'Instead, use "last_median_rtt".') return self.last_median_rtt @property def target_responded(self): - logging.warning( + log.warning( 'The "target_responded" property is deprecated and will be removed ' 'in future versions. Instead, use "destination_ip_responded".' )