From f3b72921444eaf75b6a5ac8e85a53a0beadceec5 Mon Sep 17 00:00:00 2001 From: Jessica Wagantall Date: Fri, 28 Jul 2017 00:04:34 -0700 Subject: [PATCH] Implement lftools logger Add generic logger configuration file to be used by lftools scripts. This logger checks 3 locations for a logger.ini file: 1) ~/.config/lftools/logging.ini 2) /etc/lftools/logging.ini 3) sys.prefix/etc/logging.ini Dev only... 4) etc/logging.ini (Default fallback in dev environments) JIRA: RELENG-321 Change-Id: I8fef663dd6b02947f7722424ecce6e2cc78debec Signed-off-by: Jessica Wagantall --- MANIFEST.in | 1 + etc/logging.ini | 22 ++++++++++++++++++++++ lftools/__init__.py | 39 +++++++++++++++++++++++++++++++++++++++ setup.py | 1 + 4 files changed, 63 insertions(+) create mode 100644 etc/logging.ini diff --git a/MANIFEST.in b/MANIFEST.in index f9bd1455..a80aeab0 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1 +1,2 @@ +include etc/logging.ini include requirements.txt diff --git a/etc/logging.ini b/etc/logging.ini new file mode 100644 index 00000000..293a14b8 --- /dev/null +++ b/etc/logging.ini @@ -0,0 +1,22 @@ +[loggers] +keys=root + +[handlers] +keys=consoleHandler + +[formatters] +keys=simpleFormatter + +[logger_root] +level=DEBUG +handlers=consoleHandler + +[handler_consoleHandler] +class=StreamHandler +level=INFO +formatter=simpleFormatter +args=(sys.stdout,) + +[formatter_simpleFormatter] +format=%(asctime)s (%(levelname)s) %(name)s: %(message)s +datefmt= diff --git a/lftools/__init__.py b/lftools/__init__.py index f9dee4d2..38ac20da 100644 --- a/lftools/__init__.py +++ b/lftools/__init__.py @@ -14,3 +14,42 @@ __author__ = 'Thanh Ha' __version__ = '0.7.0-dev' + +import logging +import logging.config +import os +import sys + + +def find_log_ini(file_name='logging.ini'): + """Find the logging.ini file. + + Precedence: + 1) ~/.config/lftools/logging.ini + 2) /etc/lftools/logging.ini + 3) sys.prefix/etc/logging.ini + + Dev only... + 4) etc/logging.ini (Default fallback in dev environments) + """ + homedir = os.path.expanduser('~') + log_file_etc = os.path.abspath(os.path.join('/etc', 'lftools', file_name)) + log_file_home = os.path.abspath(os.path.join(homedir, '.config', 'lftools', file_name)) + log_file_pyprefix = os.path.abspath(os.path.join(sys.prefix, 'etc', file_name)) + + if os.path.exists(log_file_home): + file_path = log_file_home + elif os.path.exists(log_file_etc): + file_path = log_file_etc + elif os.path.exists(log_file_pyprefix): + file_path = log_file_pyprefix + else: + file_path = os.path.join('etc', file_name) + return file_path + + +log_ini_file = find_log_ini() +logging.config.fileConfig(log_ini_file) + +log = logging.getLogger(__name__) +log.info("Using logger config file {}".format(log_ini_file)) diff --git a/setup.py b/setup.py index c96443ed..453032b2 100644 --- a/setup.py +++ b/setup.py @@ -68,4 +68,5 @@ setup( 'shell/sign', 'shell/version', ], + data_files=[('etc', ['etc/logging.ini'])], ) -- 2.16.6