MD500_Pilot

system_logging.py

Mar 15th, 2021
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. Part of drive_manager. This is the logging module.
  6. For use with drive_manager V1.0.0
  7. """
  8.  
  9. VERSION = "V1.0.0 (2021-03-15)"
  10.  
  11.  
  12. import sys
  13. import os
  14. sys.path.append('/root/plot_manager')
  15. import yaml
  16. import logging.config
  17. import logging
  18. import logging.handlers
  19. import configparser
  20. config = configparser.ConfigParser()
  21.  
  22. def setup_logging(default_path='/root/plot_manager/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('plot_manager_config', 'system_logging', 'log_level')
  25.     log = logging.getLogger(__name__)
  26.     level = logging._checkLevel(log_level)
  27.     log.setLevel(level)
  28.     system_logging = read_logging_config('plot_manager_config', 'system_logging', '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. Check File Permissions (for a start)!')
  42.                     logging.basicConfig(level=default_level)
  43.         else:
  44.             logging.basicConfig(level=default_level)
  45.             print('Failed to load configuration file. Using default configs')
  46.             return log
  47.     else:
  48.         log.disabled = True
  49.  
  50.  
  51. def read_logging_config(file, section, status):
  52.     pathname = '/root/plot_manager/' + file
  53.     config.read(pathname)
  54.     if status == "logging":
  55.         current_status = config.getboolean(section, status)
  56.     else:
  57.         current_status = config.get(section, status)
  58.     return current_status
  59.  
  60.  
  61. def main():
  62.     print("This script is not intended to be run directly.")
  63.     print("This is the systemwide logging module.")
  64.     print("It is called by other modules.")
  65.     exit()
  66.  
  67.  
  68. if __name__ == '__main__':
  69.     main()
Advertisement
Add Comment
Please, Sign In to add comment