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.<ol><li>
How do I create a logger for apscheduler.scheduler? Am I missing something in the section of the docs dealing with configuring the scheduler</li> <li>
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)</li> </ol>Answer1:
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 <a href="http://docs.python.org/howto/logging.html#logging-basic-tutorial" rel="nofollow">python logging</a> 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.