Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 345
Eric Hansen Avatar asked May 26 '13 21:05

Eric Hansen


3 Answers

You can try this code:

logging.getLogger('apscheduler.executors.default').propagate = False
like image 116
tupeng Avatar answered Sep 25 '22 22:09

tupeng


Here's my code, which works without complaining about missing handlers:

scheduler = BackgroundScheduler()
scheduler.start()
scheduler.add_job(every_minute, trigger='cron', second=0, id='every_minute')
logging.getLogger('apscheduler.executors.default').setLevel(logging.WARNING)

It does not disable scheduler logging completely, but it does remove the info messages the OP wanted to be rid of.

like image 23
Lee Sanders Avatar answered Sep 26 '22 22:09

Lee Sanders


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.

like image 33
msw Avatar answered Sep 25 '22 22:09

msw