Repository navigation
Expand file tree
/
Copy pathlogger.py
More file actions
47 lines (42 loc) · 1.42 KB
/
Copy pathlogger.py
File metadata and controls
47 lines (42 loc) · 1.42 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
#!/bin/env python
# -*- coding:utf-8 -*-
# _auth:kaliarch
import os
import time
import logging
import configparser
class LogHelper():
"""
初始化logger,读取目录及文件名称
"""
def __init__(self):
configoper = configparser.ConfigParser()
configoper.read('config.cfg',encoding='utf-8')
self.logdir_name = configoper['common']['logdir_name']
self.logfile_name = configoper['common']['logfile_name']
def create_dir(self):
"""
创建目录
:return: 文件名称
"""
_LOGDIR = os.path.join(os.path.dirname(__file__), self.logdir_name)
_TIME = time.strftime('%Y-%m-%d', time.gmtime()) + '-'
_LOGNAME = _TIME + self.logfile_name
LOGFILENAME = os.path.join(_LOGDIR, _LOGNAME)
if not os.path.exists(_LOGDIR):
os.mkdir(_LOGDIR)
return LOGFILENAME
def create_logger(self, logfilename):
"""
创建logger对象
:param logfilename:
:return: logger对象
"""
logger = logging.getLogger()
logger.setLevel(logging.INFO)
handler = logging.FileHandler(logfilename)
handler.setLevel(logging.INFO)
formater = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formater)
logger.addHandler(handler)
return logger