MD500_Pilot

Untitled

Jul 20th, 2020
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. Part of GardenPi. This is the logging module.
  6. """
  7.  
  8. __author__ = 'Richard J. Sears'
  9. VERSION = "V1.0 (2020-07-20)"
  10. # richardjsears@gmail.com
  11.  
  12. import sys
  13. import os
  14. import yaml
  15. import logging.config
  16. sys.path.append('/var/www/gardenpi_control/gardenpi')
  17. import system_info
  18. import mysql.connector
  19. from mysql.connector import Error
  20. import logging.config, logging
  21.  
  22. def setup_logging(default_path='/var/www/gardenpi_control/gardenpi/logging.yaml', default_level=logging.CRITICAL, env_key='LOG_CFG'):
  23.     """Module to configure program-wide logging. Designed for yaml configuration files."""
  24.     log_level = read_logging_config('logging', 'log_level')
  25.     log = logging.getLogger(__name__)
  26.     level = logging._checkLevel(log_level)
  27.     log.setLevel(level)
  28.     system_logging = read_logging_config('logging', 'system_logging')
  29.     if system_logging:
  30.         path = default_path
  31.         value = os.getenv(env_key, None)
  32.         if value:
  33.             path = value
  34.         if os.path.exists(path):
  35.             with open(path, 'rt') as f:
  36.                 try:
  37.                     config = yaml.safe_load(f.read())
  38.                     logging.config.dictConfig(config)
  39.                 except Exception as e:
  40.                     print(e)
  41.                     print('Error in Logging Configuration. Using default configs')
  42.                     logging.basicConfig(level=default_level)
  43.                     #capture_exception(e)
  44.         else:
  45.             logging.basicConfig(level=default_level)
  46.             print('Failed to load configuration file. Using default configs')
  47.             return log
  48.     else:
  49.         log.disabled = True
  50.  
  51.  
  52. def read_logging_config(table, column):
  53.     try:
  54.         connection = mysql.connector.connect(user=system_info.mysql_username,
  55.                                       password=system_info.mysql_password,
  56.                                       host=system_info.mysql_servername,
  57.                                       database=system_info.mysql_database)
  58.         cursor = connection.cursor(buffered=True)
  59.         cursor.execute(("SELECT %s FROM %s") % (column, table))
  60.         for data in cursor:
  61.             database_value = (data[0])
  62.             return database_value
  63.         cursor.close()
  64.         connection.close()
  65.     except Error as error :
  66.         exit()
Add Comment
Please, Sign In to add comment