Commit 01c315c7 authored by Trevor Cappallo's avatar Trevor Cappallo
Browse files

fix attachment format and log filename override

parent ee5f7ca1
......@@ -82,6 +82,7 @@ class OperationalLogger:
logging.getLogger('_sub').handlers = []
logging.basicConfig(filename=log_filename, format=log_format_file, datefmt=date_format_file, level=logging.DEBUG)
formatter = logging.Formatter(fmt=log_format_console, datefmt=date_format_console)
formatter_file = logging.Formatter(fmt=log_format_file, datefmt=date_format_file)
# sub_handler is used for output from log_shell.
# It is passed parentname and parentline in 'extra' dict,
......@@ -101,7 +102,7 @@ class OperationalLogger:
debug_trigger = logging.StreamHandler(self.__debug_buffer)
debug_trigger.setLevel(logging.DEBUG)
debug_trigger.setFormatter(formatter)
debug_trigger.setFormatter(formatter_file)
error_trigger = logging.StreamHandler(self.__error_buffer)
error_trigger.setLevel(logging.ERROR)
error_trigger.setFormatter(formatter)
......@@ -189,22 +190,9 @@ def setup(config_file=None, logger_name=None, log_filename=None, log_root_dir=No
log_format_file, date_format_file: formats to use for file logger
log_format_console, date_format_console: formats to use for stdout logger
"""
if not log_filename:
log_filename = os.path.basename(sys.argv[0]).split('.')[0] + '.log'
if not log_root_dir:
log_root_dir = os.getcwd()
# log_directory = os.path.join(log_root_dir, 'log', datetime.datetime.utcnow().strftime('%Y-%m-%d'))
log_directory = os.path.abspath(log_root_dir)
if not os.path.exists(log_directory):
try:
# default log dir permissions: SGID bit + rwxr-x---
os.makedirs(log_directory, 02750)
except OSError:
raise RuntimeError('Unable to create log directory: {}'.format(log_directory))
# Load config{} with passed args, defaulting to None.
config = {
'log_filename': log_filename, 'logger_name': logger_name,
'log_filename': log_filename, 'log_root_dir': log_root_dir, 'logger_name': logger_name,
'from_address': from_address, 'notify': notify,
'log_level': log_level, 'console_level': console_level,
'log_format_file': log_format_file, 'date_format_file': date_format_file,
......@@ -223,8 +211,20 @@ def setup(config_file=None, logger_name=None, log_filename=None, log_root_dir=No
logging.debug("- setting option from file: %s --> %s", key, val)
config[key] = val
# Set reasonable defaults for remaining Nones.
if config['log_root_dir'] is None:
config['log_root_dir'] = os.getcwd()
# log_directory = os.path.join(log_root_dir, 'log', datetime.datetime.utcnow().strftime('%Y-%m-%d'))
log_directory = os.path.abspath(config['log_root_dir'])
if not os.path.exists(log_directory):
try:
# default log dir permissions: SGID bit + rwxr-x---
os.makedirs(log_directory, 02750)
except OSError:
raise RuntimeError('Unable to create log directory: {}'.format(log_directory))
if config['log_filename'] is None:
config['log_filename'] = os.path.join(log_directory, log_filename)
default_filename = os.path.basename(sys.argv[0]).split('.')[0] + '.log'
config['log_filename'] = os.path.join(log_directory, default_filename)
if config['logger_name'] is None:
config['logger_name'] = ''
if config['notify'] is None:
......@@ -242,7 +242,7 @@ def setup(config_file=None, logger_name=None, log_filename=None, log_root_dir=No
# )
logger = OperationalLogger(**config)
logging.info('Logging output from: {}'.format(' '.join(sys.argv)))
logging.info('Log writing to: %s', os.path.join(log_directory, log_filename))
logging.info('Log writing to: %s', config['log_filename'])
logging.info('Process id {}, parent process id {}'.format(os.getpid(), os.getppid()))
logging.info('-' * 60)
#if notify:
......
......@@ -22,7 +22,7 @@ class TestAll(unittest.TestCase):
self.temp_dir = tempfile.mkdtemp()
self.log_dir = tempfile.mkdtemp()
OpLog.setup(config_file="test-config.json", log_root_dir=self.log_dir)
OpLog.setup(config_file="test-config.json")
def tearDown(self):
shutil.rmtree(self.temp_dir)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment