12

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.

Eric Hansen
  • 193
  • 1
  • 2
  • 8

4 Answers4

17

You can try this code:

logging.getLogger('apscheduler.executors.default').propagate = False
tupeng
  • 171
  • 1
  • 3
13

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.

Lee Sanders
  • 131
  • 1
  • 2
  • 2
    In my case, I used `logging.getLogger('apscheduler').setLevel(logging.WARNING)`. I was getting errors when using `'apscheduler.executors.default'`. Otherwise, this was the best solution in that I still get warn/error messages, but none of the info messages that clog up my application's logs. – Brian K Jun 19 '18 at 02:58
6

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.

msw
  • 42,753
  • 9
  • 87
  • 112
  • Yes, I am using APS as a cron-like scheduler. Thanks for the filter code, I had to modify it slightly (
    return
    instead of
    return not record.msg.startswith('Running job')
    ) to not spit out anything, but it works good enough for me. Thanks!
    – Eric Hansen May 26 '13 at 23:56
  • 1
    Using `return` is correct by accident because it returns `None` which happens to be false-ish in Python. Better and more explicit would be `return False`. See [PEP-20](http://www.python.org/dev/peps/pep-0020/) for further enlightenment. – msw May 27 '13 at 00:20
0

i changed the logging level to error and problem solved!

logging.basicConfig(level=logging.ERROR)