From 1cf082cc48b389a54498bfb0c2b188df76141d82 Mon Sep 17 00:00:00 2001 From: checktheroads Date: Mon, 28 Sep 2020 12:37:44 -0700 Subject: [PATCH] remove f-string logging --- hyperglass/api/__init__.py | 2 +- hyperglass/api/events.py | 2 -- hyperglass/api/models/validators.py | 4 ++-- hyperglass/api/routes.py | 18 +++++++++--------- hyperglass/execution/construct.py | 8 ++++---- hyperglass/execution/drivers/_common.py | 4 ++-- hyperglass/execution/drivers/agent.py | 18 +++++++++--------- hyperglass/execution/main.py | 6 +++--- hyperglass/main.py | 4 ++-- hyperglass/parsing/juniper.py | 2 +- 10 files changed, 33 insertions(+), 35 deletions(-) diff --git a/hyperglass/api/__init__.py b/hyperglass/api/__init__.py index 3bdd7d3..a172c0c 100644 --- a/hyperglass/api/__init__.py +++ b/hyperglass/api/__init__.py @@ -253,7 +253,7 @@ if [n for n in devices.all_nos if n in TRANSPORT_REST]: if params.docs.enable: app.add_api_route(path=params.docs.uri, endpoint=docs, include_in_schema=False) app.openapi = _custom_openapi - log.debug(f"API Docs config: {app.openapi()}") + log.debug("API Docs config: {}", app.openapi()) app.mount("/images", StaticFiles(directory=IMAGES_DIR), name="images") app.mount("/custom", StaticFiles(directory=CUSTOM_DIR), name="custom") diff --git a/hyperglass/api/events.py b/hyperglass/api/events.py index 6b09c22..9297c6a 100644 --- a/hyperglass/api/events.py +++ b/hyperglass/api/events.py @@ -1,7 +1,6 @@ """API Events.""" # Project -from hyperglass.log import log from hyperglass.util import check_redis from hyperglass.exceptions import HyperglassError from hyperglass.configuration import REDIS_CONFIG, params @@ -21,7 +20,6 @@ async def _check_redis(): except RuntimeError as e: raise HyperglassError(str(e), level="danger") from None - log.debug(f"Redis is running at: {REDIS_CONFIG['host']}:{REDIS_CONFIG['port']}") return True diff --git a/hyperglass/api/models/validators.py b/hyperglass/api/models/validators.py index e8339e6..48b1756 100644 --- a/hyperglass/api/models/validators.py +++ b/hyperglass/api/models/validators.py @@ -21,14 +21,14 @@ def _member_of(target, network): Returns: {bool} -- True if target is a member of network, False if not """ - log.debug(f"Checking membership of {target} for {network}") + log.debug("Checking membership of {} for {}", target, network) membership = False if ( network.network_address <= target.network_address and network.broadcast_address >= target.broadcast_address # NOQA: W503 ): - log.debug(f"{target} is a member of {network}") + log.debug("{} is a member of {}", target, network) membership = True return membership diff --git a/hyperglass/api/routes.py b/hyperglass/api/routes.py index 41b69c3..5d53328 100644 --- a/hyperglass/api/routes.py +++ b/hyperglass/api/routes.py @@ -84,8 +84,8 @@ async def query(query_data: Query, request: Request, background_tasks: Backgroun # Define cache entry expiry time cache_timeout = params.cache.timeout - log.debug(f"Cache Timeout: {cache_timeout}") - log.info(f"Starting query execution for query {query_data.summary}") + log.debug("Cache Timeout: {}", cache_timeout) + log.info("Starting query execution for query {}", query_data.summary) cache_response = await cache.get_dict(cache_key, "output") @@ -100,7 +100,7 @@ async def query(query_data: Query, request: Request, background_tasks: Backgroun cached = False if cache_response: - log.debug("Query {q} exists in cache", q=cache_key) + log.debug("Query {} exists in cache", cache_key) # If a cached response exists, reset the expiration time. await cache.expire(cache_key, seconds=cache_timeout) @@ -110,9 +110,9 @@ async def query(query_data: Query, request: Request, background_tasks: Backgroun timestamp = await cache.get_dict(cache_key, "timestamp") elif not cache_response: - log.debug(f"No existing cache entry for query {cache_key}") + log.debug("No existing cache entry for query {}", cache_key) log.debug( - f"Created new cache key {cache_key} entry for query {query_data.summary}" + "Created new cache key {} entry for query {}", cache_key, query_data.summary ) timestamp = query_data.timestamp @@ -121,7 +121,7 @@ async def query(query_data: Query, request: Request, background_tasks: Backgroun cache_output = await execute(query_data) endtime = time.time() elapsedtime = round(endtime - starttime, 4) - log.debug(f"Query {cache_key} took {elapsedtime} seconds to run.") + log.debug("Query {} took {} seconds to run.", cache_key, elapsedtime) if cache_output is None: raise HyperglassError(message=params.messages.general, alert="danger") @@ -135,7 +135,7 @@ async def query(query_data: Query, request: Request, background_tasks: Backgroun await cache.set_dict(cache_key, "timestamp", timestamp) await cache.expire(cache_key, seconds=cache_timeout) - log.debug(f"Added cache entry for query: {cache_key}") + log.debug("Added cache entry for query: {}", cache_key) runtime = int(round(elapsedtime, 0)) @@ -146,8 +146,8 @@ async def query(query_data: Query, request: Request, background_tasks: Backgroun if json_output: response_format = "application/json" - log.debug(f"Cache match for {cache_key}:\n {cache_response}") - log.success(f"Completed query execution for {query_data.summary}") + log.debug("Cache match for {}:\n{}", cache_key, cache_response) + log.success("Completed query execution for query {}", query_data.summary) return { "output": cache_response, diff --git a/hyperglass/execution/construct.py b/hyperglass/execution/construct.py index 0bdc72b..aaccfca 100644 --- a/hyperglass/execution/construct.py +++ b/hyperglass/execution/construct.py @@ -31,9 +31,9 @@ class Construct: query_data {object} -- Validated query object """ log.debug( - "Constructing {q} query for '{t}'", - q=query_data.query_type, - t=str(query_data.query_target), + "Constructing {} query for '{}'", + query_data.query_type, + str(query_data.query_target), ) self.device = device self.query_data = query_data @@ -157,5 +157,5 @@ class Construct: else: query.append(self.scrape(afi=afi)) - log.debug(f"Constructed query: {query}") + log.debug("Constructed query: {}", query) return query diff --git a/hyperglass/execution/drivers/_common.py b/hyperglass/execution/drivers/_common.py index 17be92f..878986f 100644 --- a/hyperglass/execution/drivers/_common.py +++ b/hyperglass/execution/drivers/_common.py @@ -27,7 +27,7 @@ class Connection: async def parsed_response(self, output: Iterable) -> str: """Send output through common parsers.""" - log.debug(f"Pre-parsed responses:\n{output}") + log.debug("Pre-parsed responses:\n{}", output) parsed = () response = None @@ -61,5 +61,5 @@ class Connection: if response is None: response = "\n\n".join(output) - log.debug(f"Post-parsed responses:\n{response}") + log.debug("Post-parsed responses:\n{}", response) return response diff --git a/hyperglass/execution/drivers/agent.py b/hyperglass/execution/drivers/agent.py index 5685861..a72b7b2 100644 --- a/hyperglass/execution/drivers/agent.py +++ b/hyperglass/execution/drivers/agent.py @@ -27,7 +27,7 @@ class AgentConnection(Connection): async def collect(self) -> Iterable: # noqa: C901 """Connect to a device running hyperglass-agent via HTTP.""" - log.debug(f"Query parameters: {self.query}") + log.debug("Query parameters: {}", self.query) client_params = { "headers": {"Content-Type": "application/json"}, @@ -56,7 +56,7 @@ class AgentConnection(Connection): protocol=http_protocol, address=self.device._target, port=self.device.port ) - log.debug(f"URL endpoint: {endpoint}") + log.debug("URL endpoint: {}", endpoint) try: async with httpx.AsyncClient(**client_params) as http_client: @@ -68,22 +68,22 @@ class AgentConnection(Connection): secret=self.device.credential.password.get_secret_value(), duration=params.request_timeout, ) - log.debug(f"Encoded JWT: {encoded_query}") + log.debug("Encoded JWT: {}", encoded_query) raw_response = await http_client.post( endpoint, json={"encoded": encoded_query} ) - log.debug(f"HTTP status code: {raw_response.status_code}") + log.debug("HTTP status code: {}", raw_response.status_code) raw = raw_response.text - log.debug(f"Raw Response: {raw}") + log.debug("Raw Response:\n{}", raw) if raw_response.status_code == 200: decoded = await jwt_decode( payload=raw_response.json()["encoded"], secret=self.device.credential.password.get_secret_value(), ) - log.debug(f"Decoded Response: {decoded}") + log.debug("Decoded Response:\n{}", decoded) responses += (decoded,) elif raw_response.status_code == 204: @@ -97,7 +97,7 @@ class AgentConnection(Connection): except httpx.exceptions.HTTPError as rest_error: msg = parse_exception(rest_error) - log.error(f"Error connecting to device {self.device.name}: {msg}") + log.error("Error connecting to device {}: {}", self.device.name, msg) raise RestError( params.messages.connection_error, device_name=self.device.display_name, @@ -120,7 +120,7 @@ class AgentConnection(Connection): ) if raw_response.status_code != 200: - log.error(f"Response code is {raw_response.status_code}") + log.error("Response code is {}", raw_response.status_code) raise RestError( params.messages.connection_error, device_name=self.device.display_name, @@ -128,7 +128,7 @@ class AgentConnection(Connection): ) if not responses: - log.error(f"No response from device {self.device.name}") + log.error("No response from device {}", self.device.name) raise RestError( params.messages.connection_error, device_name=self.device.display_name, diff --git a/hyperglass/execution/main.py b/hyperglass/execution/main.py index 02becc8..046c221 100644 --- a/hyperglass/execution/main.py +++ b/hyperglass/execution/main.py @@ -43,8 +43,8 @@ async def execute(query: Query) -> Union[str, Dict]: output = params.messages.general - log.debug(f"Received query for {query}") - log.debug(f"Matched device config: {query.device}") + log.debug("Received query for {}", query.json()) + log.debug("Matched device config: {}", query.device) supported, driver_name = validate_nos(query.device.nos) @@ -79,7 +79,7 @@ async def execute(query: Query) -> Union[str, Dict]: params.messages.no_output, device_name=query.device.display_name ) - log.debug(f"Output for query: {query.json()}:\n{repr(output)}") + log.debug("Output for query: {}:\n{}", query.json(), repr(output)) signal.alarm(0) return output diff --git a/hyperglass/main.py b/hyperglass/main.py index 367d1f5..311facf 100644 --- a/hyperglass/main.py +++ b/hyperglass/main.py @@ -52,7 +52,7 @@ async def check_redis_instance(): """ await check_redis(db=params.cache.database, config=REDIS_CONFIG) - log.debug(f"Redis is running at: {REDIS_CONFIG['host']}:{REDIS_CONFIG['port']}") + log.debug("Redis is running at: {}:{}", REDIS_CONFIG["host"], REDIS_CONFIG["port"]) return True @@ -98,7 +98,7 @@ def on_starting(server: Arbiter): python_version = platform.python_version() required = ".".join((str(v) for v in MIN_PYTHON_VERSION)) - log.info(f"Python {python_version} detected ({required} required)") + log.info("Python {} detected ({} required)", python_version, required) async def runner(): from asyncio import gather diff --git a/hyperglass/parsing/juniper.py b/hyperglass/parsing/juniper.py index f73e9cc..636a5cd 100644 --- a/hyperglass/parsing/juniper.py +++ b/hyperglass/parsing/juniper.py @@ -53,7 +53,7 @@ def parse_juniper(output: Iterable) -> Dict: # noqa: C901 raise ParsingError("Error parsing response data") except KeyError as err: - log.critical(f"'{str(err)}' was not found in the response") + log.critical("{} was not found in the response", str(err)) raise ParsingError("Error parsing response data") except ValidationError as err: