-
Notifications
You must be signed in to change notification settings - Fork 0
/
logtools3.py
66 lines (49 loc) · 1.48 KB
/
logtools3.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
60
61
62
63
64
65
66
#!/usr/bin/ python3
# -*- coding: utf-8 -*-
"""
@author: anhy
@file : logtools3.py
@time : 2021/12/15 17:15
@desc :
"""
import os
import logging
import logging.handlers
import time
import sys
class PublicFun(object):
def set_log2(self, logpath, logname):
"""
:param logpath:
:param logname:
:return:
"""
if not os.path.exists(logpath):
print("logpath:%s not exist!" % logpath)
sys.exit()
level = 'INFO'
self.logger = logging.getLogger()
self.logger.setLevel(logging.INFO)
filename = logpath + '/' + logname + time.strftime('%Y%m%d', time.localtime(time.time())) + '.log'
fh2 = logging.handlers.TimedRotatingFileHandler(filename, 'D', 1, 0)
fh2.suffix = "%Y%m%d-%H%M%S.log"
# fh.setLevel(level)
formatter = logging.Formatter("%(asctime)s %(levelname)s " "[%(module)s.%(funcName)s:%(lineno)d] "
"%(message)s")
fh2.setFormatter(formatter)
self.logger.addHandler(fh2)
sh2 = logging.StreamHandler()
sh2.setLevel(logging.INFO)
sh2.setFormatter(formatter)
self.logger.addHandler(sh2)
# logging.info("set logger down")
return self.logger
'''
引用方式
import logtools3
import logging
p1 = logtools3.PublicFun()
p1.set_log2("/Users/yuehaoan/data/pcstatiot/", "logtools32_")
logging.info("log222222222222")
logging.error("deerrrr")
'''