-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlog.py
60 lines (48 loc) · 1.46 KB
/
log.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
59
import os
import logging.config
def setup(log_file_path=None):
if log_file_path is None:
log_file_path = os.path.expanduser('~/donkey.log')
config_default = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"simple": {
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "INFO",
"formatter": "simple",
"stream": "ext://sys.stdout"
},
"error_file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "INFO",
"formatter": "simple",
"filename": log_file_path,
"maxBytes": 10485760,
"backupCount": 20,
"encoding": "utf8"
},
},
"root": {
"level": "DEBUG",
"handlers": ["console", "error_file_handler"]
}
}
logging.config.dictConfig(config_default)
def get_logger(name):
"""
Return a logger that will contextualize the logs with the name.
"""
logger = logging.getLogger(name)
return logger
# get a logger specific to this file
logger = get_logger(__name__)
logger.info('Logging configured and loaded.')
if __name__ == '__main__':
print('run')
logger.error('test')