-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.py
39 lines (35 loc) · 1.06 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
"""
This module is used to create a logger object that logs to a file.
Functions:
- log: creates a logger object
"""
import logging
import json
import time
from logging.handlers import TimedRotatingFileHandler
def log(log_path, log_name):
"""
This function creates a logger object that logs to a file.
:param log_path: the path to the log file
:param log_name: the name of the logger
:return: logger object
"""
logger = logging.getLogger(log_name)
logging.Formatter.converter = time.gmtime # set log time to utc/gmt
log_format = logging.Formatter(
json.dumps({'date': '%(asctime)s',
'name': '%(name)s',
'level': '%(levelname)s',
'msg': '%(message)s',
})
)
log_handler = TimedRotatingFileHandler(
filename=log_path,
when='midnight',
backupCount=7,
utc=True)
log_handler.suffix = "%Y%m%d"
log_handler.setFormatter(log_format)
logger.addHandler(log_handler)
logger.setLevel(logging.DEBUG)
return logger