From a96d79d09b5ded2d4f4f7838f7b52df8fc2c63a1 Mon Sep 17 00:00:00 2001 From: Sam Waterbury Date: Thu, 15 Aug 2019 22:14:41 -0500 Subject: [PATCH] Replace uplog with standard logging module (#26) --- app/env.yml | 1 - app/groundhog.py | 77 +++++++++++++++++---------------- app/srtm_elevation_and_slope.py | 31 ++++++------- tests/test_bulk_request.py | 34 ++++++++------- 4 files changed, 73 insertions(+), 70 deletions(-) diff --git a/app/env.yml b/app/env.yml index cb7648b..22cb54e 100644 --- a/app/env.yml +++ b/app/env.yml @@ -25,6 +25,5 @@ dependencies: - psutil==5.4.3 - requests==2.18.4 - srtm.py==0.3.2 - - uplog==0.1 - urllib3==1.22 - werkzeug==0.14.1 diff --git a/app/groundhog.py b/app/groundhog.py index 0f6908f..84d0830 100644 --- a/app/groundhog.py +++ b/app/groundhog.py @@ -1,5 +1,5 @@ -from uplog import log +import logging import sys import time import psutil @@ -10,6 +10,8 @@ from flask import Flask, request, Response, jsonify import srtm_elevation_and_slope as srtm_methods +logger = logging.getLogger() + # Flask object for resident support flask_app = Flask(__name__) @@ -38,12 +40,12 @@ def __init__(self, latitude, longitude, bearing=None, stride=DEFAULT_STRIDE, uni self.unique_key = unique_key def info(self): - log.out.info("lat|lon at bearing:stride (key) = " + - str(self.latitude) + "|" + - str(self.longitude) + " at " + - str(self.bearing) + ":" + - str(self.stride) + " (" + - str(self.unique_key) + ")") + logger.info("lat|lon at bearing:stride (key) = " + + str(self.latitude) + "|" + + str(self.longitude) + " at " + + str(self.bearing) + ":" + + str(self.stride) + " (" + + str(self.unique_key) + ")") def to_dict(self): return { @@ -59,11 +61,11 @@ def report_sys_info(): """ Report basic system stats """ - log.out.info("Python version : " + sys.version) - log.out.info("Number of CPUs : " + str(psutil.cpu_count())) - log.out.info("Memory total : " + str(round(float(psutil.virtual_memory().total) / 2 ** 30, 2)) + "GB") - log.out.info("Memory useage : " + str(round(float(psutil.virtual_memory().used) / 2 ** 30, 2)) + "GB") - log.out.info("Memory available: " + str(round(float(psutil.virtual_memory().available) / 2 ** 30, 2)) + "GB") + logger.info("Python version : " + sys.version) + logger.info("Number of CPUs : " + str(psutil.cpu_count())) + logger.info("Memory total : " + str(round(float(psutil.virtual_memory().total) / 2 ** 30, 2)) + "GB") + logger.info("Memory useage : " + str(round(float(psutil.virtual_memory().used) / 2 ** 30, 2)) + "GB") + logger.info("Memory available: " + str(round(float(psutil.virtual_memory().available) / 2 ** 30, 2)) + "GB") def get_command_line(): @@ -94,13 +96,13 @@ def help_response(): """ help_message = """ - ___ _ _ - / _ \_ __ ___ _ _ _ __ __| | |__ ___ __ _ + ___ _ _ + / _ \_ __ ___ _ _ _ __ __| | |__ ___ __ _ / /_\/ '__/ _ \| | | | '_ \ / _` | '_ \ / _ \ / _` | / /_\\\\| | | (_) | |_| | | | | (_| | | | | (_) | (_| | \____/|_| \___/ \__,_|_| |_|\__,_|_| |_|\___/ \__, | - |___/ - + |___/ + ENDPOINTS: /help - to request a help doc /health - make health check @@ -110,16 +112,16 @@ def help_response(): lat - latitude of interest (-90.0 to 90.0 degrees North) lon - longitude of interest (-180.0 to 180.0 degrees East) bearing - Compass bearing for slope (cardinal degrees, 0 is North) - + or: coords - a list of coordinates (NOT IMPLEMENTED YET) - + optional: stride (optional, default=250.0) - resolution to calculate slope on in meters (larger is smoother) - + SAMPLE REST CALL: http://localhost:5005/groundhog?lat=45.2&lon=-101.3 - + SAMPLE JSON PAYLOAD (OPTIONAL - bearing, stride, unique_key): [{ 'latitude': 45.0, @@ -178,13 +180,13 @@ def json_to_headings(json_coords): longitude = geo_point.get("lon") # Give up if no coord info found if (latitude is None) or (longitude is None): - log.out.error("Problem in latitude/longitude info in JSON.") + logger.error("Problem in latitude/longitude info in JSON.") raise KeyError try: latitude = float(latitude) longitude = float(longitude) except ValueError: - log.out.error("Problem in parsing latitude/longitude given as float.") + logger.error("Problem in parsing latitude/longitude given as float.") raise ValueError if coord.get("bearing") is not None: bearing = float(coord.get("bearing")) @@ -219,7 +221,7 @@ def rest_to_heading(params): if key == 'stride': stride = float(value) if (latitude is None) or (longitude is None): - log.out.error("Required latitude, longitude not given.") + logger.error("Required latitude, longitude not given.") return None return [Heading(latitude, longitude, bearing=bearing, stride=stride)] @@ -261,22 +263,22 @@ def groundhog_request(request): Supports the request for a groundhog call params (obj) - a dictionary filled with option from REST request """ - log.out.info("Groundhog has been summoned.") + logger.info("Groundhog has been summoned.") params = request.args # Get a list of coordinates from the REST call if request.method == 'POST': try: json_payload = request.get_json() except TypeError: - log.out.error("Problem in POST request.") + logger.error("Problem in POST request.") return None - log.out.info("Coordinates posted as JSON...") + logger.info("Coordinates posted as JSON...") headings = json_to_headings(json_payload) else: headings = rest_to_heading(params) # Curate coordinates from the REST call - log.out.info("Received " + str(len(headings)) + " coordinates to fetch.") + logger.info("Received " + str(len(headings)) + " coordinates to fetch.") # If it's a single heading assume you got a bearing, throw an error if not response_list = [] @@ -286,8 +288,8 @@ def groundhog_request(request): response_part["elevation"] = elevation response_part["slope"] = slope response_list.append(response_part) - # log.out.info("elevation: {}".format(elevation)) - # log.out.info("slope {}".format(slope)) + # logger.info("elevation: {}".format(elevation)) + # logger.info("slope {}".format(slope)) # If it's a list can use one of two methods, prefer given bearings else: elevation_list, slope_list, bearing_list = from_heading_list(headings) @@ -305,27 +307,27 @@ def groundhog_request(request): # Standard health check @flask_app.route("/health") def health_check(): - log.out.info("Received /health request from: " + request.remote_addr) + logger.info("Received /health request from: " + request.remote_addr) return make_health_check() # Give help @flask_app.route("/") def do_none_help(): - log.out.info("Received / request from: " + request.remote_addr) + logger.info("Received / request from: " + request.remote_addr) return help_response() @flask_app.route("/help") def do_help_message(): - log.out.info("Received /help request from: " + request.remote_addr) + logger.info("Received /help request from: " + request.remote_addr) return help_response() # Main endpoint @flask_app.route("/groundhog", methods=['GET', 'POST']) def groundhog(): - log.out.info("Received /groundhog request from: " + request.remote_addr) + logger.info("Received /groundhog request from: " + request.remote_addr) data_list = groundhog_request(request) return make_json_response(data_list) @@ -336,9 +338,9 @@ def groundhog(): args = get_command_line() # Read command line arguments if args.debug: - log.out.setLevel("DEBUG") # Set the logging level to verbose + logger.setLevel("DEBUG") # Set the logging level to verbose else: - log.out.setLevel("INFO") # Set the logging level to normal + logger.setLevel("INFO") # Set the logging level to normal pool = mp.Pool() # Instantiate a pool object flask_app.config["pool"] = mp.pool.Pool() @@ -346,9 +348,8 @@ def groundhog(): flask_app.run(host="0.0.0.0", port=args.port, debug=args.debug, use_reloader=False) # Shut down and clean up - log.out.info("Execution time: " + str(round((time.clock() - start) * 1000, 1)) + " ms") - log.out.info("All Done!") - log.stopLog() + logger.info("Execution time: " + str(round((time.clock() - start) * 1000, 1)) + " ms") + logger.info("All Done!") try: mp.sys.exit() except SystemError: diff --git a/app/srtm_elevation_and_slope.py b/app/srtm_elevation_and_slope.py index 7254666..d261050 100644 --- a/app/srtm_elevation_and_slope.py +++ b/app/srtm_elevation_and_slope.py @@ -6,7 +6,7 @@ Modified: 20171120 (Brad Beechler) """ -from uplog import log # make the logs global +import logging import time import argparse from math import asin, sin, cos, pi, sqrt, atan2, degrees, radians @@ -15,6 +15,8 @@ srtm_client = srtm.get_data() # if OOM issues, set to True +logger = logging.getLogger() # make the logs global + def get_command_line(): """ @@ -199,11 +201,11 @@ def slope_from_coord_bearing(longitude_origin, latitude_origin, bearing_origin, :return: terrain elevation (meters) / meter, terrain slope (meters/meter) """ elevation_origin = srtm_client.get_elevation(latitude_origin, longitude_origin) - log.out.debug("Elevation at origin: " + str(elevation_origin)) + logger.debug("Elevation at origin: " + str(elevation_origin)) # Bearing is an optional param if bearing_origin is None: - log.out.warn("No bearing given. Returning only elevation") + logger.warn("No bearing given. Returning only elevation") return elevation_origin, None longitude_ahead, latitude_ahead = lon_lat_from_distance_bearing(longitude_origin, latitude_origin, @@ -212,9 +214,9 @@ def slope_from_coord_bearing(longitude_origin, latitude_origin, bearing_origin, (-1.0 * stride_length), bearing_origin) elevation_ahead = get_elevation_safe(longitude_ahead, latitude_ahead) - log.out.debug("Elevation at coordinate ahead: " + str(elevation_ahead)) + logger.debug("Elevation at coordinate ahead: " + str(elevation_ahead)) elevation_behind = get_elevation_safe(longitude_behind, latitude_behind) - log.out.debug("Elevation at coordinate behind: " + str(elevation_behind)) + logger.debug("Elevation at coordinate behind: " + str(elevation_behind)) if ((elevation_origin is None) or (elevation_ahead is None) or @@ -223,9 +225,9 @@ def slope_from_coord_bearing(longitude_origin, latitude_origin, bearing_origin, else: # Calculate terrain slope delta_elevation = elevation_ahead - elevation_behind - log.out.debug("Change in elevation = " + str(delta_elevation)) + logger.debug("Change in elevation = " + str(delta_elevation)) terrain_slope = delta_elevation / stride_length - log.out.debug("Terrain slope = " + str(terrain_slope) + " m/m") + logger.debug("Terrain slope = " + str(terrain_slope) + " m/m") return elevation_origin, terrain_slope @@ -267,10 +269,10 @@ def should_be_a_test(args): # Check stride setting and report if using default if args.stride is not None: stride_length = float(args.stride) - + test_elev_bearing, test_slope_bearing = slope_from_coord_bearing(longitude_origin, latitude_origin, bearing_origin, stride_length=stride_length) - log.out.info("Terrain slope = " + str(test_slope_bearing) + " m/m") + logger.info("Terrain slope = " + str(test_slope_bearing) + " m/m") # TEST test_coords = [(-78.3, 38.32), (-78.5, 38.32), (-78.7, 38.32), (-78.8, 38.32), (-79.0, 38.32), @@ -289,14 +291,13 @@ def should_be_a_test(args): start = time.clock() args = get_command_line() # Read command line arguments if args.debug: - log.out.setLevel("DEBUG") # Set the logging level to verbose + logger.setLevel("DEBUG") # Set the logging level to verbose else: - log.out.setLevel("INFO") # Set the logging level to normal + logger.setLevel("INFO") # Set the logging level to normal - log.out.warning("You should really be running the groundhog.py service, this is going into tests.") + logger.warning("You should really be running the groundhog.py service, this is going into tests.") should_be_a_test(args) # Shut down and clean up - log.out.info("Execution time: " + str(round((time.clock() - start) * 1000, 1)) + " ms") - log.out.info("All Done!") - log.stopLog() + logger.info("Execution time: " + str(round((time.clock() - start) * 1000, 1)) + " ms") + logger.info("All Done!") diff --git a/tests/test_bulk_request.py b/tests/test_bulk_request.py index 762aa9b..afa7c18 100644 --- a/tests/test_bulk_request.py +++ b/tests/test_bulk_request.py @@ -2,12 +2,14 @@ This tests a bulk JSON request """ -from uplog import log # Make the logs global +import logging import requests INTERP_URL = 'http://localhost:5005/groundhog' DEFAULT_STRIDE = 250.0 +logger = logging.getLogger() # Make the logs global + def request_with_json(json_payload): """ @@ -52,28 +54,28 @@ def make_test_json(size=10, stride=DEFAULT_STRIDE, use_bearing=True, use_geo_poi if __name__ == '__main__': - log.out.setLevel('INFO') # Set the logging level to verbose - log.out.info("Testing groundhog JSON post") + logger.setLevel('INFO') # Set the logging level to verbose + logger.info("Testing groundhog JSON post") test_json = make_test_json(size=25) - log.out.info("Test payload (with bearings given):") - log.out.info(test_json) + logger.info("Test payload (with bearings given):") + logger.info(test_json) test_response = request_with_json(test_json) - log.out.info("Test response (with bearings given):") - log.out.info(test_response) + logger.info("Test response (with bearings given):") + logger.info(test_response) test_json = make_test_json(size=25, use_bearing=False) - log.out.info("Test payload (without bearings given):") - log.out.info(test_json) + logger.info("Test payload (without bearings given):") + logger.info(test_json) test_response = request_with_json(test_json) - log.out.info("Test response (without bearings given):") - log.out.info(test_response) + logger.info("Test response (without bearings given):") + logger.info(test_response) test_json = make_test_json(size=25, use_geo_point=True) - log.out.info("Test payload (with geo_points given):") - log.out.info(test_json) + logger.info("Test payload (with geo_points given):") + logger.info(test_json) test_response = request_with_json(test_json) - log.out.info("Test response (with geo_points given):") - log.out.info(test_response) + logger.info("Test response (with geo_points given):") + logger.info(test_response) - log.out.info("Done!") + logger.info("Done!")