如果它每周运行一次,你可能会从crontab运行它?
30 2 * * 5 python yourScript.py | mail -s outputFromScript your@email.address
Run Code Online (Sandbox Code Playgroud)
看一下 logging 和 logging.config,我之前使用过它来接收来自后台运行的脚本的错误消息
http://docs.python.org/library/logging.html
例如
import logging
import logging.config
logDir = "./logs/"
logging.config.fileConfig(logDir+'logging.conf')
logger = logging.getLogger('email')
logger.debug('THIS IS A DEBUG MESSAGE')
logger.error('THIS IS AN ERROR')
Run Code Online (Sandbox Code Playgroud)
然后是logging.conf
[loggers]
keys=root,email
[logger_root]
level=DEBUG
handlers=rotatingFileHandler
[logger_email]
level=ERROR
handlers=email
qualname=email
[formatters]
keys=emailFormatter,rotatingFormatter
[formatter_emailFormatter]
format=%(asctime)s %(name)-12s %(levelname)-8s %(message)s
[formatter_rotatingFormatter]
format=%(asctime)s %(name)-12s %(levelname)-8s %(message)s
datefmt=%m-%d %H:%M
[handlers]
keys=email,rotatingFileHandler
[handler_email]
class=handlers.SMTPHandler
level=ERROR
formatter=emailFormatter
args=('mail.xxx','x@x.com',['y@y.com',],'ERROR!',('x@x.com','xxx'))
[handler_rotatingFileHandler]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=rotatingFormatter
args=('./logs/log.out', 'maxBytes=1000000', 'backupCount=5')
Run Code Online (Sandbox Code Playgroud)
从上面我会在我的电子邮件中收到“这是一个错误”。
print您可以使用 alogger打印到粗壮和记录器,而不是让您的主要输出
您可以根据Logging Cookbook如下设置记录器:
import logging
log_file = r'C:\Users\user\Downloads\LogFileName.log'
logger = logging.getLogger('simple_example')
logger.setLevel(logging.INFO)
# create file handler which logs even debug messages
fh = logging.FileHandler('log_file')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)
Run Code Online (Sandbox Code Playgroud)
现在在print您的脚本中替换为logger.info
之前的例子:
print("Printing status")
Run Code Online (Sandbox Code Playgroud)
之后的示例:
logger.info("Printing status")
Run Code Online (Sandbox Code Playgroud)
然后您可以通过电子邮件将日志发送给自己,如下所示:
import smtplib
from email.message import EmailMessage
import os
msg_body = "Body Text"
msg = EmailMessage()
msg['Subject'] = "Subject"
msg['From'] = "send_from@email.com"
msg['To'] = "send_to@email.com"
msg.set_content(msg_body)
if os.path.isfile(log_file):
msg.add_attachment(open(log_file, "r").read(), filename=os.path.basename(log_file))
# Send the message via our own SMTP server.
s = smtplib.SMTP("smtpa.server")
s.send_message(msg)
s.quit()
Run Code Online (Sandbox Code Playgroud)
import smtplib
def prompt(prompt):
return raw_input(prompt).strip()
fromaddr = prompt("From: ")
toaddrs = prompt("To: ").split()
print "Enter message, end with ^D (Unix) or ^Z (Windows):"
# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
% (fromaddr, ", ".join(toaddrs)))
while 1:
try:
line = raw_input()
except EOFError:
break
if not line:
break
msg = msg + line
print "Message length is " + repr(len(msg))
server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11329 次 |
| 最近记录: |