Mercurial > repos > guerler > springsuite
diff planemo/lib/python3.7/site-packages/ephemeris/ephemeris_log.py @ 0:d30785e31577 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:18:57 -0400 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/planemo/lib/python3.7/site-packages/ephemeris/ephemeris_log.py Fri Jul 31 00:18:57 2020 -0400 @@ -0,0 +1,62 @@ +import logging +import tempfile + + +class ProgressConsoleHandler(logging.StreamHandler): + """ + A handler class which allows the cursor to stay on + one line for selected messages + """ + on_same_line = False + + def emit(self, record): + try: + msg = self.format(record) + stream = self.stream + same_line = hasattr(record, 'same_line') + if self.on_same_line and not same_line: + stream.write('\r\n') + stream.write(msg) + if same_line: + stream.write('.') + self.on_same_line = True + else: + stream.write('\r\n') + self.on_same_line = False + self.flush() + except (KeyboardInterrupt, SystemExit): + raise + except Exception: + self.handleError(record) + + +def disable_external_library_logging(): + # Omit (most of the) logging by external libraries + logging.getLogger('bioblend').setLevel(logging.ERROR) + logging.getLogger('requests').setLevel(logging.ERROR) + try: + logging.captureWarnings(True) # Capture HTTPS warngings from urllib3 + except AttributeError: + pass + + +def setup_global_logger(name, log_file=None): + formatter = logging.Formatter('%(asctime)s %(levelname)-5s - %(message)s') + progress = ProgressConsoleHandler() + console = logging.StreamHandler() + console.setFormatter(formatter) + + logger = logging.getLogger(name) + logger.setLevel(logging.DEBUG) + logger.addHandler(progress) + + if not log_file: + # delete = false is chosen here because it is always nice to have a log file + # ready if you need to debug. Not having the "if only I had set a log file" + # moment after the fact. + temp = tempfile.NamedTemporaryFile(prefix="ephemeris_", delete=False) + log_file = temp.name + file_handler = logging.FileHandler(log_file) + logger.addHandler(file_handler) + logger.info("Storing log file in: {0}".format(log_file)) + return logger