Statistiques
| Branche: | Tag: | Révision :

dockonsurf / modules / config_log.py @ 58ede1f9

Historique | Voir | Annoter | Télécharger (1,08 ko)

1
"""Configures the logger to record all calculation events on a log file."""
2
import sys
3
import logging
4
import warnings
5

    
6

    
7
def log_exception(exc_type, exc_value, exc_tb):
8
    if issubclass(exc_type, KeyboardInterrupt):
9
        sys.__excepthook__(exc_type, exc_value, exc_tb)
10
        return
11
    logger = logging.getLogger('DockOnSurf')
12
    logger.error("", exc_info=(exc_type, exc_value, exc_tb))
13

    
14

    
15
def log_warning(message, *args, **kwargs):
16
    logger = logging.getLogger('DockOnSurf')
17
    logger.warning(" ".join(f"{message}".split()))
18

    
19

    
20
def config_log(label):  # TODO Format log to break line after column 80.
21
    logger = logging.getLogger(label)
22
    logger.setLevel(logging.INFO)
23

    
24
    log_handler = logging.FileHandler('dockonsurf.log', mode='w')
25
    log_handler.setLevel(logging.INFO)
26
    log_format = logging.Formatter(fmt='%(asctime)s-%(levelname)s: %(message)s',
27
                                   datefmt='%d-%b-%y %H:%M:%S')
28
    log_handler.setFormatter(log_format)
29

    
30
    logger.addHandler(log_handler)
31

    
32
    sys.excepthook = log_exception
33
    warnings.showwarning = log_warning
34

    
35
    return logger