天天看点

APScheduler配置监听事件,并写入日志

log.py

# coding:utf-8

from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.events import EVENT_JOB_EXECUTED, EVENT_JOB_ERROR, EVENT_JOB_MISSED
import datetime
import logging

logger = logging.getLogger('job')
logging.basicConfig(level=logging.INFO,
                    format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt = '%Y-%m-%d %H:%M:%S',
                    filename = 'mylog.txt',
                    filemode = 'a')

'''任务1'''
def my_job(x):
    print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), x)

'''任务2'''
def test_job(x):
    print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), x)
    print(1 / 0)

def job_listener(Event):
    job = scheduler.get_job(Event.job_id)
    if not Event.exception:
        print('任务正常运行!')
        logger.info("jobname=%s|jobtrigger=%s|jobtime=%s|retval=%s", job.name, job.trigger,
                    Event.scheduled_run_time, Event.retval)

    else:
        print("任务出错了!!!!!")
        logger.error("jobname=%s|jobtrigger=%s|errcode=%s|exception=[%s]|traceback=[%s]|scheduled_time=%s", job.name,
                     job.trigger, Event.code,
                     Event.exception, Event.traceback, Event.scheduled_run_time)

scheduler = BlockingScheduler()
scheduler.add_job(func=test_job, args=('一次性任务,会出错',),
                 next_run_time=datetime.datetime.now() + datetime.timedelta(seconds=15), id='date_task')
scheduler.add_job(func=my_job, args=('循环任务',), trigger='interval', seconds=3, id='interval_task')
scheduler.add_listener(job_listener, EVENT_JOB_ERROR | EVENT_JOB_MISSED | EVENT_JOB_EXECUTED)
scheduler._logger = logging
scheduler.start()

           

打印结果:

2021-08-20 15:52:48 循环任务
任务正常运行!
2021-08-20 15:52:51 循环任务
任务正常运行!
2021-08-20 15:52:54 循环任务
任务正常运行!
2021-08-20 15:52:57 循环任务
任务正常运行!
2021-08-20 15:53:00 一次性任务,会出错
任务出错了!!!!!
2021-08-20 15:53:00 循环任务
任务正常运行!
2021-08-20 15:53:03 循环任务
任务正常运行!
2021-08-20 15:53:06 循环任务
任务正常运行!
           

日志文件:log.txt

2021-08-20 15:40:17 base.py[line:445] INFO Adding job tentatively -- it will be properly scheduled when the scheduler starts
2021-08-20 15:40:17 base.py[line:445] INFO Adding job tentatively -- it will be properly scheduled when the scheduler starts
2021-08-20 15:40:17 base.py[line:886] INFO Added job "date_test" to job store "default"
2021-08-20 15:40:17 base.py[line:886] INFO Added job "aps_test" to job store "default"
2021-08-20 15:40:17 base.py[line:171] INFO Scheduler started
2021-08-20 15:40:20 base.py[line:123] INFO Running job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:20 CST)" (scheduled at 2021-08-20 15:40:20.531225+08:00)
2021-08-20 15:40:20 base.py[line:144] INFO Job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:23 CST)" executed successfully
2021-08-20 15:40:20 b.py[line:30] INFO jobname=aps_test|jobtrigger=interval[0:00:03]|jobtime=2021-08-20 15:40:20.531225+08:00|retval=None
2021-08-20 15:40:23 base.py[line:123] INFO Running job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:23 CST)" (scheduled at 2021-08-20 15:40:23.531225+08:00)
2021-08-20 15:40:23 base.py[line:144] INFO Job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:23 CST)" executed successfully
2021-08-20 15:40:23 b.py[line:30] INFO jobname=aps_test|jobtrigger=interval[0:00:03]|jobtime=2021-08-20 15:40:23.531225+08:00|retval=None
2021-08-20 15:40:26 base.py[line:123] INFO Running job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:26 CST)" (scheduled at 2021-08-20 15:40:26.531225+08:00)
2021-08-20 15:40:26 base.py[line:144] INFO Job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:26 CST)" executed successfully
2021-08-20 15:40:26 b.py[line:30] INFO jobname=aps_test|jobtrigger=interval[0:00:03]|jobtime=2021-08-20 15:40:26.531225+08:00|retval=None
2021-08-20 15:40:29 base.py[line:123] INFO Running job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:29 CST)" (scheduled at 2021-08-20 15:40:29.531225+08:00)
2021-08-20 15:40:29 base.py[line:144] INFO Job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:29 CST)" executed successfully
2021-08-20 15:40:29 b.py[line:30] INFO jobname=aps_test|jobtrigger=interval[0:00:03]|jobtime=2021-08-20 15:40:29.531225+08:00|retval=None
2021-08-20 15:40:32 base.py[line:123] INFO Running job "date_test (trigger: date[2021-08-20 15:40:17 CST], next run at: 2021-08-20 15:40:32 CST)" (scheduled at 2021-08-20 15:40:32.495318+08:00)
2021-08-20 15:40:32 base.py[line:632] INFO Removed job date_task
2021-08-20 15:40:32 base.py[line:131] ERROR Job "date_test (trigger: date[2021-08-20 15:40:17 CST], next run at: 2021-08-20 15:40:32 CST)" raised an exception
Traceback (most recent call last):
  File "F:\PycharmProjects\test1\test1-venv\lib\site-packages\apscheduler\executors\base.py", line 125, in run_job
    retval = job.func(*job.args, **job.kwargs)
  File "F:/PycharmProjects/test1/b.py", line 21, in date_test
    print(1 / 0)
ZeroDivisionError: division by zero
2021-08-20 15:40:32 base.py[line:838] ERROR Error notifying listener
Traceback (most recent call last):
  File "F:\PycharmProjects\test1\test1-venv\lib\site-packages\apscheduler\schedulers\base.py", line 836, in _dispatch_event
    cb(event)
  File "F:/PycharmProjects/test1/b.py", line 32, in job_listener
    logger.error("jobname=%s|jobtrigger=%s|errcode=%s|exception=[%s]|traceback=[%s]|scheduled_time=%s", job.name,
AttributeError: 'NoneType' object has no attribute 'name'
2021-08-20 15:40:32 base.py[line:123] INFO Running job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:35 CST)" (scheduled at 2021-08-20 15:40:32.531225+08:00)
2021-08-20 15:40:32 base.py[line:144] INFO Job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:35 CST)" executed successfully
2021-08-20 15:40:32 b.py[line:30] INFO jobname=aps_test|jobtrigger=interval[0:00:03]|jobtime=2021-08-20 15:40:32.531225+08:00|retval=None
2021-08-20 15:40:35 base.py[line:123] INFO Running job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:35 CST)" (scheduled at 2021-08-20 15:40:35.531225+08:00)
2021-08-20 15:40:35 base.py[line:144] INFO Job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:35 CST)" executed successfully
2021-08-20 15:40:35 b.py[line:30] INFO jobname=aps_test|jobtrigger=interval[0:00:03]|jobtime=2021-08-20 15:40:35.531225+08:00|retval=None
2021-08-20 15:40:38 base.py[line:123] INFO Running job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:38 CST)" (scheduled at 2021-08-20 15:40:38.531225+08:00)
2021-08-20 15:40:38 base.py[line:144] INFO Job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:41 CST)" executed successfully
2021-08-20 15:40:38 b.py[line:30] INFO jobname=aps_test|jobtrigger=interval[0:00:03]|jobtime=2021-08-20 15:40:38.531225+08:00|retval=None
2021-08-20 15:40:41 base.py[line:123] INFO Running job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:41 CST)" (scheduled at 2021-08-20 15:40:41.531225+08:00)
2021-08-20 15:40:41 base.py[line:144] INFO Job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:44 CST)" executed successfully
2021-08-20 15:40:41 b.py[line:30] INFO jobname=aps_test|jobtrigger=interval[0:00:03]|jobtime=2021-08-20 15:40:41.531225+08:00|retval=None
2021-08-20 15:40:44 base.py[line:123] INFO Running job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:44 CST)" (scheduled at 2021-08-20 15:40:44.531225+08:00)
2021-08-20 15:40:44 base.py[line:144] INFO Job "aps_test (trigger: interval[0:00:03], next run at: 2021-08-20 15:40:44 CST)" executed successfully
2021-08-20 15:40:44 b.py[line:30] INFO jobname=aps_test|jobtrigger=interval[0:00:03]|jobtime=2021-08-20 15:40:44.531225+08:00|retval=None