Python APScheduler how to disable logging
Asked Answered
S

5

13

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.

Sensitize answered 26/5, 2013 at 21:15 Comment(0)
C
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.

Cum answered 26/5, 2013 at 23:22 Comment(2)
Yes, I am using APS as a cron-like scheduler. Thanks for the filter code, I had to modify it slightly (<pre>return</pre> instead of <pre>return not record.msg.startswith('Running job')</pre>) to not spit out anything, but it works good enough for me. Thanks!Sensitize
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 for further enlightenment.Cum
K
17

You can try this code:

logging.getLogger('apscheduler.executors.default').propagate = False
Kepler answered 17/8, 2017 at 13:34 Comment(1)
DEBUG messages will be printed anyway, so it is necessary to set the logger level to INFO.Kelsy
L
17

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.

Lauryn answered 5/12, 2017 at 1:33 Comment(1)
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.Failing
C
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.

Cum answered 26/5, 2013 at 23:22 Comment(2)
Yes, I am using APS as a cron-like scheduler. Thanks for the filter code, I had to modify it slightly (<pre>return</pre> instead of <pre>return not record.msg.startswith('Running job')</pre>) to not spit out anything, but it works good enough for me. Thanks!Sensitize
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 for further enlightenment.Cum
M
0

i changed the logging level to error and problem solved!

logging.basicConfig(level=logging.ERROR)
Millman answered 29/8, 2023 at 10:12 Comment(0)
I
0

You can create your own logger and pass it as a parameter:

apscheduler_logger = logging.getLogger("apscheduler.scheduler")
apscheduler_logger.setLevel(logging.WARNING)
sched = BlockingScheduler(option={'logger': apscheduler_logger})
Intercollegiate answered 23/2 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.