-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.py
49 lines (40 loc) · 1.75 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
# -*- coding: utf-8 -*-
import logging
import logging.handlers
import os
def setup_logging(conf):
"""
Initialize the logging module settings
:param conf: dict, Initialize parameters
:return:
"""
level = {"DEBUG": logging.DEBUG, "INFO": logging.INFO, "WARNING": logging.WARNING,
"ERROR": logging.ERROR, "CRITICAL": logging.CRITICAL}
console = conf['console'] # console output
console_level = conf['console_level'] # choose console log level to print
file = conf['file'] # local log file output
file_level = conf['file_level'] # choose log file level to save
logfile = conf['log_file'] # local log file save position
backup_count = conf['backup_count'] # count of local log files
max_size = conf['max_size'] # size of each local log file
format_string = conf['format_string'] # log message format
log = logging.getLogger('ak_modbus')
log.setLevel(logging.DEBUG)
formatter = logging.Formatter(format_string, datefmt='%Y-%d-%m %H:%M:%S')
if file:
# create logs directory if doesn't exist
dir_path = os.path.dirname(logfile)
if not os.path.exists(dir_path):
os.makedirs(dir_path)
# instantiate a rotate file handler to output log file
fh = logging.handlers.RotatingFileHandler(filename=logfile, mode='a', maxBytes=max_size,
backupCount=backup_count, encoding='utf-8')
fh.setLevel(level[file_level])
fh.setFormatter(formatter)
log.addHandler(fh)
if console:
# instantiate a stream handler to output log file in console
ch = logging.StreamHandler()
ch.setLevel(level[console_level])
ch.setFormatter(formatter)
log.addHandler(ch)