How to specify 'logger' for apscheduler

前提是你 提交于 2019-12-13 14:27:33

问题


I'm trying to learn how to use Python's apscheduler package, but periodically, it throws the following error:

No handlers could be found for logger "apscheduler.scheduler"

This message seems to be associated with errors in the scheduled jobs, for example, using jobTester as the scheduled job, the following code, which uses an undefined variable (nameStr0) in jobTester gives the above error message:

from apscheduler.scheduler import Scheduler
from apscheduler.jobstores.shelve_store import ShelveJobStore
from datetime import datetime, timedelta
from schedJob import toyJob

def jobTester(nameStr):
    outFileName = nameStr0 + '.txt'
    outFile = open(outFileName,'w')
    outFile.write(nameStr)
    outFile.close()

def schedTester(jobList):
    scheduler = Scheduler()
    scheduler.add_jobstore(ShelveJobStore('example.db'),'shelve')
    refTime = datetime.now()

    for index, currJob in enumerate(jobList):
        runTime = refTime + timedelta(seconds = 15)
        jobName = currJob.name + '_' + str(index)
        scheduler.add_date_job(jobTester, runTime, name = jobName,
                           jobstore = 'shelve', args = [jobName])


    scheduler.start()
    stopTime = datetime.now() + timedelta(seconds = 45)
    print "Starting wait loop .....",
    while stopTime > datetime.now():
        pass
    print "Done"

def doit():
    names = ['Alan','Barbara','Charlie','Dana']
    jobList = [toyJob(n) for n in names]
    schedTester(jobList)

This may be seen by running this code (stored in the file schedTester.py) as follows:

>>> import schedTester
>>> schedTester.doit()
No handlers could be found for logger "apscheduler.scheduler"
Starting wait loop ..... Done

However, when I replace nameStr0 with nameStr (i.e. proper spelling of variable name), the code runs fine without the error message.

  1. How do I create a logger for apscheduler.scheduler? Am I missing something in the section of the docs dealing with configuring the scheduler

  2. Am I correct in thinking of this logger as some sort of a stderr ? If so, where will I look for it (if that is not determined by the way I set it up)


回答1:


You can just create a default logger and everything should go to it:

import logging
logging.basicConfig()

The reason that you only have a problem when you use a variable that hasn't been defined is that this causes the jobTester function to throw an error which apscheduler is catching and trying to write the error message with logging.error(). Since you haven't setup the logger it is going to complain.

If you read up on python logging you will see that there are many ways to configure it. You could have it log everything to a file or print it to stdout.



来源:https://stackoverflow.com/questions/12888977/how-to-specify-logger-for-apscheduler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!