From 4314b8b98cc25d6ed54313b611e638ab63711b4e Mon Sep 17 00:00:00 2001 From: evilhero Date: Fri, 11 May 2018 12:34:41 -0400 Subject: [PATCH] FIX: Fix for log_dir error on startup when log_dir is not set in the config.ini - either due to new startup or was set to None --- mylar/config.py | 11 +++++++++++ mylar/logger.py | 47 ++++++++++++++++++++++++----------------------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/mylar/config.py b/mylar/config.py index 65043207..e7fbd0ae 100644 --- a/mylar/config.py +++ b/mylar/config.py @@ -500,6 +500,17 @@ class Config(object): self.provider_sequence() if startup is True: + if self.LOG_DIR is None: + self.LOG_DIR = os.path.join(mylar.DATA_DIR, 'logs') + + if not os.path.exists(self.LOG_DIR): + try: + os.makedirs(self.LOG_DIR) + except OSError: + if not mylar.QUIET: + self.LOG_DIR = None + print('Unable to create the log directory. Logging to screen only.') + # Start the logger, silence console logging if we need to if logger.LOG_LANG.startswith('en'): logger.initLogger(console=not mylar.QUIET, log_dir=self.LOG_DIR, max_logsize=self.MAX_LOGSIZE, max_logfiles=self.MAX_LOGFILES, loglevel=mylar.LOG_LEVEL) diff --git a/mylar/logger.py b/mylar/logger.py index 151dc559..fbf5f4e0 100644 --- a/mylar/logger.py +++ b/mylar/logger.py @@ -77,37 +77,38 @@ if not LOG_LANG.startswith('en'): lg = logging.getLogger('mylar') lg.setLevel(logging.DEBUG) - self.filename = os.path.join(log_dir, self.filename) + if log_dir is not None: + self.filename = os.path.join(log_dir, self.filename) - #concurrentLogHandler/0.8.7 (to deal with windows locks) - #since this only happens on windows boxes, if it's nix/mac use the default logger. - if mylar.OS_DETECT == 'Windows': - #set the path to the lib here - just to make sure it can detect cloghandler & portalocker. - import sys - sys.path.append(os.path.join(mylar.PROG_DIR, 'lib')) + #concurrentLogHandler/0.8.7 (to deal with windows locks) + #since this only happens on windows boxes, if it's nix/mac use the default logger. + if mylar.OS_DETECT == 'Windows': + #set the path to the lib here - just to make sure it can detect cloghandler & portalocker. + import sys + sys.path.append(os.path.join(mylar.PROG_DIR, 'lib')) - try: - from ConcurrentLogHandler.cloghandler import ConcurrentRotatingFileHandler as RFHandler - mylar.LOGTYPE = 'clog' - except ImportError: + try: + from ConcurrentLogHandler.cloghandler import ConcurrentRotatingFileHandler as RFHandler + mylar.LOGTYPE = 'clog' + except ImportError: + mylar.LOGTYPE = 'log' + from logging.handlers import RotatingFileHandler as RFHandler + else: mylar.LOGTYPE = 'log' from logging.handlers import RotatingFileHandler as RFHandler - else: - mylar.LOGTYPE = 'log' - from logging.handlers import RotatingFileHandler as RFHandler - filehandler = RFHandler( - self.filename, - maxBytes=max_logsize, - backupCount=max_logfiles) + filehandler = RFHandler( + self.filename, + maxBytes=max_logsize, + backupCount=max_logfiles) - filehandler.setLevel(logging.DEBUG) + filehandler.setLevel(logging.DEBUG) - fileformatter = logging.Formatter('%(asctime)s - %(levelname)-7s :: %(message)s', '%d-%b-%Y %H:%M:%S') + fileformatter = logging.Formatter('%(asctime)s - %(levelname)-7s :: %(message)s', '%d-%b-%Y %H:%M:%S') - filehandler.setFormatter(fileformatter) - lg.addHandler(filehandler) - self.filehandler = filehandler + filehandler.setFormatter(fileformatter) + lg.addHandler(filehandler) + self.filehandler = filehandler if loglevel: consolehandler = logging.StreamHandler()