-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.py
58 lines (37 loc) · 1.23 KB
/
logger.py
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
'''
This module provides a logging system.
Set the LOG_LEVEL like you want, and in
the necessary module you can use the logger
like this:
____________________________________________
from logger import logger
logger.info('file path is correct.')
logger.warning('Check the files!')
____________________________________________
Only messages with the higher logging level will be
shown.
Also you can get the name of the current logging
level, for example:
print(LEVELS[LOG_LEVEL])
-> 'DEBUG'
'''
import logging
import os
from datetime import datetime
LOG_FILE_NAME = 'info.log'
LOG_LEVEL = logging.INFO
LEVELS = {
logging.DEBUG:'DEBUG',
logging.INFO:'INFO',
logging.WARNING:'WARNING',
logging.ERROR:'ERROR',
logging.CRITICAL:'CRITICAL',
}
logger = logging.getLogger(__name__)
logger.setLevel(LOG_LEVEL)
log_file = logging.FileHandler(LOG_FILE_NAME, mode = 'w')
logging_format = '[%(levelname)s] ("%(asctime)s" "%(name)s") module "%(module)s"\n[func "%(funcName)s"]\n\n%(message)s\n'#.format(datetime.now().strftime('%H:%M:%S'))
formatter = logging.Formatter(logging_format, datefmt='%H:%M:%S')
log_file.setFormatter(formatter)
logger.addHandler(log_file)
logger.info('Logging started!')