From 99b686069420d0931b85278e66d4dd8dd2da5383 Mon Sep 17 00:00:00 2001 From: Peter Yefi Date: Mon, 17 Apr 2023 17:59:34 -0400 Subject: [PATCH] included stream logger. switched from default file logger to stream --- hub/hub_logger/__init__.py | 41 +++++++++++++------ .../construction/nrcan_physics_parameters.py | 4 +- .../construction/nrel_physics_parameters.py | 4 +- hub/imports/construction_factory.py | 4 +- hub/imports/energy_systems_factory.py | 4 +- hub/imports/geometry_factory.py | 4 +- hub/imports/life_cycle_assessment_factory.py | 4 +- hub/imports/results_factory.py | 4 +- hub/imports/sensors_factory.py | 4 +- hub/imports/usage/comnet_usage_parameters.py | 4 +- hub/imports/usage/nrcan_usage_parameters.py | 4 +- hub/imports/usage_factory.py | 4 +- hub/imports/weather_factory.py | 4 +- 13 files changed, 65 insertions(+), 24 deletions(-) diff --git a/hub/hub_logger/__init__.py b/hub/hub_logger/__init__.py index 87629fef..1c7c5f04 100644 --- a/hub/hub_logger/__init__.py +++ b/hub/hub_logger/__init__.py @@ -1,16 +1,33 @@ import logging as logger from pathlib import Path import os +import logging +import sys + + +def get_logger(file_logger=False): + """ + Returns a logging object + :param file_logger: a boolean to indicate the kind of logging + object to return, true (default) means a file logger is required + :return: + """ + log_format = "%(asctime)s:%(levelname)s:{%(pathname)s:%(funcName)s:%(lineno)d} - %(message)s" + if file_logger: + log_dir = (Path(__file__).parent.parent / 'logs').resolve() + log_file = (log_dir / 'hub.log').resolve() + try: + if not os.path.isfile(log_file): + if not os.path.exists(log_dir): + os.mkdir(log_dir) + with open(log_file, 'x'): + pass + logger.basicConfig(filename=log_file, format=log_format, level=logger.DEBUG) + return logger + except IOError as err: + print(f'I/O exception: {err}') + else: + logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout)) + logging.getLogger().setLevel(logging.DEBUG) + return logger.getLogger() -log_dir = (Path(__file__).parent.parent / 'logs').resolve() -log_file = (log_dir / 'hub.log').resolve() -try: - if not os.path.isfile(log_file): - if not os.path.exists(log_dir): - os.mkdir(log_dir) - with open(log_file, 'x'): - pass - logger.basicConfig(filename=log_file, format="%(asctime)s:%(levelname)s:{%(pathname)s:%(funcName)s:%(lineno)d} " - "- %(message)s", level=logger.DEBUG) -except IOError as err: - print(f'I/O exception: {err}') diff --git a/hub/imports/construction/nrcan_physics_parameters.py b/hub/imports/construction/nrcan_physics_parameters.py index 404f27e9..25867dfa 100644 --- a/hub/imports/construction/nrcan_physics_parameters.py +++ b/hub/imports/construction/nrcan_physics_parameters.py @@ -7,7 +7,7 @@ Project Coder Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca import datetime import math import sys -from hub.hub_logger import logger +from hub.hub_logger import get_logger import hub.helpers.constants as cte from hub.catalog_factories.construction_catalog_factory import ConstructionCatalogFactory @@ -17,6 +17,8 @@ from hub.helpers.dictionaries import Dictionaries from hub.imports.construction.helpers.construction_helper import ConstructionHelper from hub.imports.construction.helpers.storeys_generation import StoreysGeneration +logger = get_logger() + class NrcanPhysicsParameters: """ diff --git a/hub/imports/construction/nrel_physics_parameters.py b/hub/imports/construction/nrel_physics_parameters.py index 80836a4d..4d74e4ea 100644 --- a/hub/imports/construction/nrel_physics_parameters.py +++ b/hub/imports/construction/nrel_physics_parameters.py @@ -7,7 +7,7 @@ Code contributors: Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concord """ import sys -from hub.hub_logger import logger +from hub.hub_logger import get_logger from hub.catalog_factories.construction_catalog_factory import ConstructionCatalogFactory from hub.city_model_structure.building_demand.layer import Layer from hub.city_model_structure.building_demand.material import Material @@ -15,6 +15,8 @@ from hub.helpers.dictionaries import Dictionaries from hub.imports.construction.helpers.construction_helper import ConstructionHelper from hub.imports.construction.helpers.storeys_generation import StoreysGeneration +logger = get_logger() + class NrelPhysicsParameters: """ diff --git a/hub/imports/construction_factory.py b/hub/imports/construction_factory.py index f9ce905a..f70dc0f9 100644 --- a/hub/imports/construction_factory.py +++ b/hub/imports/construction_factory.py @@ -7,11 +7,13 @@ Code contributors: Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concord """ from pathlib import Path -from hub.hub_logger import logger +from hub.hub_logger import get_logger from hub.helpers.utils import validate_import_export_type from hub.imports.construction.nrel_physics_parameters import NrelPhysicsParameters from hub.imports.construction.nrcan_physics_parameters import NrcanPhysicsParameters +logger = get_logger() + class ConstructionFactory: """ diff --git a/hub/imports/energy_systems_factory.py b/hub/imports/energy_systems_factory.py index b1f44876..84cb4c1e 100644 --- a/hub/imports/energy_systems_factory.py +++ b/hub/imports/energy_systems_factory.py @@ -9,7 +9,9 @@ from pathlib import Path from hub.imports.energy_systems.air_source_hp_parameters import AirSourceHeatPumpParameters from hub.imports.energy_systems.water_to_water_hp_parameters import WaterToWaterHPParameters from hub.helpers.utils import validate_import_export_type -from hub.hub_logger import logger +from hub.hub_logger import get_logger + +logger = get_logger() class EnergySystemsFactory: diff --git a/hub/imports/geometry_factory.py b/hub/imports/geometry_factory.py index 2c870085..97362b05 100644 --- a/hub/imports/geometry_factory.py +++ b/hub/imports/geometry_factory.py @@ -13,7 +13,9 @@ from hub.imports.geometry.rhino import Rhino from hub.imports.geometry.gpandas import GPandas from hub.imports.geometry.geojson import Geojson from hub.helpers.utils import validate_import_export_type -from hub.hub_logger import logger +from hub.hub_logger import get_logger + +logger = get_logger() class GeometryFactory: diff --git a/hub/imports/life_cycle_assessment_factory.py b/hub/imports/life_cycle_assessment_factory.py index a46924a7..c4dcdd86 100644 --- a/hub/imports/life_cycle_assessment_factory.py +++ b/hub/imports/life_cycle_assessment_factory.py @@ -11,7 +11,9 @@ from hub.imports.life_cycle_assessment.lca_vehicle import LcaVehicle from hub.imports.life_cycle_assessment.lca_machine import LcaMachine from hub.imports.life_cycle_assessment.lca_material import LcaMaterial from hub.helpers.utils import validate_import_export_type -from hub.hub_logger import logger +from hub.hub_logger import get_logger + +logger = get_logger() class LifeCycleAssessment: diff --git a/hub/imports/results_factory.py b/hub/imports/results_factory.py index 62adf124..794b4ac5 100644 --- a/hub/imports/results_factory.py +++ b/hub/imports/results_factory.py @@ -8,11 +8,13 @@ Code contributors: Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concord from pathlib import Path from hub.helpers.utils import validate_import_export_type -from hub.hub_logger import logger +from hub.hub_logger import get_logger from hub.imports.results.simplified_radiosity_algorithm import SimplifiedRadiosityAlgorithm from hub.imports.results.insel_monthly_energry_balance import InselMonthlyEnergyBalance from hub.imports.results.insel_heatpump_energy_demand import InselHeatPumpEnergyDemand +logger = get_logger() + class ResultFactory: """ diff --git a/hub/imports/sensors_factory.py b/hub/imports/sensors_factory.py index d4793950..a91b0e9d 100644 --- a/hub/imports/sensors_factory.py +++ b/hub/imports/sensors_factory.py @@ -6,9 +6,11 @@ Project Coder Guille Gutierrez guillermo.gutierrezmorote@concordia.ca Code contributors: Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca """ from pathlib import Path -from hub.hub_logger import logger +from hub.hub_logger import get_logger from hub.helpers.utils import validate_import_export_type +logger = get_logger() + class SensorsFactory: """ diff --git a/hub/imports/usage/comnet_usage_parameters.py b/hub/imports/usage/comnet_usage_parameters.py index 584c5b6b..6ee24ec6 100644 --- a/hub/imports/usage/comnet_usage_parameters.py +++ b/hub/imports/usage/comnet_usage_parameters.py @@ -8,7 +8,7 @@ import copy import sys import numpy -from hub.hub_logger import logger +from hub.hub_logger import get_logger import hub.helpers.constants as cte from hub.helpers.dictionaries import Dictionaries from hub.city_model_structure.building_demand.usage import Usage @@ -21,6 +21,8 @@ from hub.city_model_structure.attributes.schedule import Schedule from hub.city_model_structure.building_demand.internal_gain import InternalGain from hub.catalog_factories.usage_catalog_factory import UsageCatalogFactory +logger = get_logger() + class ComnetUsageParameters: """ diff --git a/hub/imports/usage/nrcan_usage_parameters.py b/hub/imports/usage/nrcan_usage_parameters.py index 77afb9d0..5898f2a0 100644 --- a/hub/imports/usage/nrcan_usage_parameters.py +++ b/hub/imports/usage/nrcan_usage_parameters.py @@ -7,7 +7,7 @@ Project Coder Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca import sys -from hub.hub_logger import logger +from hub.hub_logger import get_logger import hub.helpers.constants as cte from hub.helpers.dictionaries import Dictionaries from hub.city_model_structure.building_demand.usage import Usage @@ -18,6 +18,8 @@ from hub.city_model_structure.building_demand.thermal_control import ThermalCont from hub.city_model_structure.building_demand.domestic_hot_water import DomesticHotWater from hub.catalog_factories.usage_catalog_factory import UsageCatalogFactory +logger = get_logger() + class NrcanUsageParameters: """ diff --git a/hub/imports/usage_factory.py b/hub/imports/usage_factory.py index e8962480..4871532c 100644 --- a/hub/imports/usage_factory.py +++ b/hub/imports/usage_factory.py @@ -9,9 +9,11 @@ Code contributors: Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concord from pathlib import Path from hub.imports.usage.comnet_usage_parameters import ComnetUsageParameters from hub.imports.usage.nrcan_usage_parameters import NrcanUsageParameters -from hub.hub_logger import logger +from hub.hub_logger import get_logger from hub.helpers.utils import validate_import_export_type +logger = get_logger() + class UsageFactory: """ diff --git a/hub/imports/weather_factory.py b/hub/imports/weather_factory.py index 296755de..5ddf355c 100644 --- a/hub/imports/weather_factory.py +++ b/hub/imports/weather_factory.py @@ -7,9 +7,11 @@ Project Coder Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca from pathlib import Path from hub.imports.weather.xls_weather_parameters import XlsWeatherParameters from hub.imports.weather.epw_weather_parameters import EpwWeatherParameters -from hub.hub_logger import logger +from hub.hub_logger import get_logger from hub.helpers.utils import validate_import_export_type +logger = get_logger() + class WeatherFactory: """