logs.py
2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
"""LOGGER
"""
# Standard library imports
import logging
# Third party imports
# None
# Local application imports
# None
logger = None
# Aliases for logger:
#def log_d(msg:str): logger.debug(msg)
def log_d(msg:str, *args, **kwargs): logger.debug(msg, *args, **kwargs)
def log_i(msg:str, *args, **kwargs): logger.info(msg, *args, **kwargs)
def log_w(msg:str, *args, **kwargs): logger.warning(msg, *args, **kwargs)
def log_e(msg:str, *args, **kwargs): logger.error(msg, *args, **kwargs)
def log_c(msg:str, *args, **kwargs): logger.critical(msg, *args, **kwargs)
#def set_logger(self):
def set_logger(DEBUG=False):
global logger
'''
# Logger configuration
# Log all events, starting from DEBUG level
'''
# Basic configuration
'''
#logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(level=logging.DEBUG, filename='client.log', filemode='a', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logging.debug("Client instanciated")
'''
# Advanced configuration
logger = logging.getLogger(__name__)
# Absolument necessaire, sinon rien ne marche !!!
logger.setLevel(logging.DEBUG)
# Create handlers for writing to console and file
c_handler = logging.StreamHandler()
f_handler = logging.FileHandler('client.log')
# Set level for each handler
# DEBUG < INFO < WARNING < ERROR < CRITICAL
#if self.DEBUG:
if DEBUG:
c_handler.setLevel(logging.DEBUG)
f_handler.setLevel(logging.DEBUG)
else:
c_handler.setLevel(logging.INFO)
f_handler.setLevel(logging.INFO)
# Set format for each handler
#c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
c_format = logging.Formatter('(%(name)s): %(message)s')
f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
c_handler.setFormatter(c_format)
f_handler.setFormatter(f_format)
# Set handlers (for console and file)
logger.addHandler(c_handler)
logger.addHandler(f_handler)
#return logger