comparison env/lib/python3.7/site-packages/ephemeris/ephemeris_log.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 import logging
2 import tempfile
3
4
5 class ProgressConsoleHandler(logging.StreamHandler):
6 """
7 A handler class which allows the cursor to stay on
8 one line for selected messages
9 """
10 on_same_line = False
11
12 def emit(self, record):
13 try:
14 msg = self.format(record)
15 stream = self.stream
16 same_line = hasattr(record, 'same_line')
17 if self.on_same_line and not same_line:
18 stream.write('\r\n')
19 stream.write(msg)
20 if same_line:
21 stream.write('.')
22 self.on_same_line = True
23 else:
24 stream.write('\r\n')
25 self.on_same_line = False
26 self.flush()
27 except (KeyboardInterrupt, SystemExit):
28 raise
29 except Exception:
30 self.handleError(record)
31
32
33 def disable_external_library_logging():
34 # Omit (most of the) logging by external libraries
35 logging.getLogger('bioblend').setLevel(logging.ERROR)
36 logging.getLogger('requests').setLevel(logging.ERROR)
37 try:
38 logging.captureWarnings(True) # Capture HTTPS warngings from urllib3
39 except AttributeError:
40 pass
41
42
43 def setup_global_logger(name, log_file=None):
44 formatter = logging.Formatter('%(asctime)s %(levelname)-5s - %(message)s')
45 progress = ProgressConsoleHandler()
46 console = logging.StreamHandler()
47 console.setFormatter(formatter)
48
49 logger = logging.getLogger(name)
50 logger.setLevel(logging.DEBUG)
51 logger.addHandler(progress)
52
53 if not log_file:
54 # delete = false is chosen here because it is always nice to have a log file
55 # ready if you need to debug. Not having the "if only I had set a log file"
56 # moment after the fact.
57 temp = tempfile.NamedTemporaryFile(prefix="ephemeris_", delete=False)
58 log_file = temp.name
59 file_handler = logging.FileHandler(log_file)
60 logger.addHandler(file_handler)
61 logger.info("Storing log file in: {0}".format(log_file))
62 return logger