Eric Hansen Eric Hansen - 1 month ago 19
Python Question

Python APScheduler how to disable logging

I set up APScheduler to run every second via cron schedule (kind of needed/wanted). Right now I have logger sending everything to console.

If it wasn't for logging is greatly important to what I'm working on, it'd be okay. But, I need logging. What I don't want is APScheduler's info logging. Stuff like this:

INFO     at 2013-05-26 13:05:06,007 : Job "loadavg.run (trigger: cron[year='*', month='*', day='*', week='*', day_of_week='*', hour='*', minute='*', second='*'], next run at: 2013-05-26 13:05:06)" executed successfully
INFO at 2013-05-26 13:05:06,008 : Running job "cpu.run (trigger: cron[year='*', month='*', day='*', week='*', day_of_week='*', hour='*', minute='*', second='*'], next run at: 2013-05-26 13:05:07)" (scheduled at 2013-05-26 13:05:06)


I have this in my code after I add the cron jobs:

logging.getLogger("apscheduler.scheduler").setLevel(logging.DEBUG)


There's not any, as far as I know, configuration options for APScheduler to specify logging information, either.

I know I can specify the level of the logger to ERROR or something, but when it gets set to INFO I don't want all of this (what seems to be useless) information logged as well.

msw msw
Answer Source

I will first assume that you are using APScheduler for cron-like behavior because if you were really running via cron(8) every second, it would

  1. Be self-defeating because APScheduler claims it's a "far better alternative to externally run cron scripts…"
  2. Probably thrash the system something awful

That stipulated, the beauty of the logging module is that it allows your application to have broad control over a library's logging behavior without touching its code. Unfortunately, it makes logging a little hard to understand at first.

Since the INFO level reports stuff that you aren't interested in you can:

class NoRunningFilter(logging.Filter):
    def filter(self, record):
        return not record.msg.startswith('Running job')

my_filter = NoRunningFilter()
logging.getLogger("apscheduler.scheduler").addFilter(my_filter)

These can all be specified dynamically with a logging configuration file but that's a little more magic than I've ever gotten into.