dockonsurf / modules / config_log.py @ cf980c86
Historique | Voir | Annoter | Télécharger (1,64 ko)
1 |
"""Module for the configuration of how and what is recorded in the log file."""
|
---|---|
2 |
import sys |
3 |
import logging |
4 |
import warnings |
5 |
|
6 |
|
7 |
def log_exception(exc_type, exc_value, exc_tb): |
8 |
"""Sets up the recording of exceptions on the log file
|
9 |
|
10 |
@param exc_type: Type of exception
|
11 |
@param exc_value: Value of the exception
|
12 |
@param exc_tb:
|
13 |
@return: None
|
14 |
"""
|
15 |
if issubclass(exc_type, KeyboardInterrupt): |
16 |
sys.__excepthook__(exc_type, exc_value, exc_tb) |
17 |
return
|
18 |
logger = logging.getLogger('DockOnSurf')
|
19 |
logger.error("", exc_info=(exc_type, exc_value, exc_tb))
|
20 |
|
21 |
|
22 |
def log_warning(message, *args, **kwargs): |
23 |
"""Sets up the recording of warnings on the log file
|
24 |
|
25 |
@param message: Warning message.
|
26 |
@param args: Additional arguments.
|
27 |
@param kwargs: Additional keyword arguments.
|
28 |
@return: None
|
29 |
"""
|
30 |
logger = logging.getLogger('DockOnSurf')
|
31 |
logger.warning(" ".join(f"{message}".split())) |
32 |
|
33 |
|
34 |
def config_log(label): # TODO Format log to break long lines (after column 80). |
35 |
"""Configures the logger to record all calculation events on a log file.
|
36 |
|
37 |
@param label: Label of the logger to be used.
|
38 |
@return: The logger object.
|
39 |
"""
|
40 |
logger = logging.getLogger(label) |
41 |
logger.setLevel(logging.INFO) |
42 |
|
43 |
log_handler = logging.FileHandler('dockonsurf.log', mode='w') |
44 |
log_handler.setLevel(logging.INFO) |
45 |
log_format = logging.Formatter(fmt='%(asctime)s-%(levelname)s: %(message)s',
|
46 |
datefmt='%d-%b-%y %H:%M:%S')
|
47 |
log_handler.setFormatter(log_format) |
48 |
|
49 |
logger.addHandler(log_handler) |
50 |
|
51 |
sys.excepthook = log_exception |
52 |
warnings.showwarning = log_warning |
53 |
|
54 |
return logger
|