-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlog.py
34 lines (30 loc) · 1.17 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
#!/usr/bin/env python
# encoding: utf-8
import os
import logging
from logging.handlers import SMTPHandler, TimedRotatingFileHandler
from setting import CURRENT_DIR
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO) # 输出到控制台的log等级的开关
# 创建该handler的formatter
logger_format = logging.Formatter(
fmt='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
console_handler.setFormatter(logger_format)
logger.addHandler(console_handler)
file_handler = TimedRotatingFileHandler(filename=CURRENT_DIR+'/log/log.txt',when="midnight",backupCount=10)
file_handler.suffix = "%Y-%m-%d.txt"
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(logger_format)
logger.addHandler(file_handler)
mail_handler = SMTPHandler(
mailhost='smtp.163.com',
fromaddr='[email protected]',
toaddrs='[email protected]',
subject='这是一封proypool项目发来的邮件',
credentials=('account', 'password'))
mail_handler.setLevel(logging.ERROR)
mail_handler.setFormatter(logger_format)
logger.addHandler(mail_handler)