forked from yangdemin/dingpan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logTest.py
102 lines (80 loc) · 3.06 KB
/
logTest.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import logging
import logging.handlers
import smtplib
import logging.config
from logging.handlers import RotatingFileHandler, SMTPHandler
from datetime import datetime
class SSLSMTPHandler(SMTPHandler):
def emit(self, record):
try:
port = 465
if not port:
port = smtplib.SMTP_PORT
smtp = smtplib.SMTP_SSL(self.mailhost, port)
msg = self.format(record)
if self.username:
smtp.login('[email protected]', 'pofcnsyjofoubdig')
smtp.sendmail('[email protected]','[email protected]', "helolo")
smtp.quit()
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)
def testLog():
LOG_FORMAT = "%(asctime)s-%(levelname)s-%(filename)s:%(lineno)d %(message)s"
#FileNAME_FORMAT = "%Y%m%d-.log"
filename1 = datetime.now().strftime("%Y%m%d-%H%M%S")+'.log'
logging.basicConfig(filename=filename1, level=logging.INFO, format=LOG_FORMAT)
# logging.debug("This is a debug log.")
# logging.info("This is a info log.")
# logging.warning("This is a warning log.")
# logging.error("This is a error log.")
# logging.critical("This is a critical log.")
# testLog()
def testLogMail():
logger = logging.getLogger('test')
logger.setLevel(logging.DEBUG)
handler = logging.handlers.SMTPHandler(
('smtp.qq.com',587),
#('imap.163.com',993),
['[email protected]'],
'Python error test',
('[email protected]','pofcnsyjofoubdig'))
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
logger.isEnabledFor(logging.DEBUG)
print(handler)
#logger.debug("This is a debug log.")
#logger.info("This is a info log.")
#logger.warning("This is a warning log.")
logger.error("This is a error log.")
#logger.critical("This is a critical log.")
testLogMail()
def testLogMail2():
logging.config.fileConfig("logging.conf")
logger = logging.getLogger('test')
logger.info('hello body ~')
# testLogMail2()
# Provide a class to allow SSL (Not TLS) connection for mail handlers by overloading the emit() method
'''
logger = logging.getLogger('')
# Create equivalent mail handler
handler = SSLSMTPHandler(mailhost=('smtp.qq.com', 465),
fromaddr='[email protected]',
toaddrs=['[email protected]'],
subject='yangdeminAAA',
credentials=('[email protected]','pofcnsyjofoubdig'))
# Set the email format
handler.setFormatter(logging.Formatter( "%(asctime)s" ))
# Only email errors, not warnings
handler.setLevel(logging.ERROR)
logger.addHandler(handler)
logger.isEnabledFor(logging.DEBUG)
logger.error("This is very important.AAAAAAAAAAAAAAAAA")
logger.error("This is very important.BBBBBBBBBBBBBBBB")
logger.error("This is very important.CCCCCCCCCCCCCCCCCCC")
logging.error("This is very important.AAAAAAAAAAAAAAAAA11111")
logging.error("This is very important.BBBBBBBBBBBBBBBB22222")
logging.error("This is very important.CCCCCCCCCCCCCCCCCCC333")
'''