Search code examples
pythonpython-2.7loggingscheduler

Turn off logging in schedule library


Objective: prevent schedule from logging every time it runs.

Background:

I am using the logging and schedule libraries in a python project.

My log file contains information about the physical state of a instrument run by a Raspberry Pi, and is updated every 10 seconds.

I use the schedule library to schedule that periodic log.

Here is the limited documentation I have found for schedule.

The Problem:

The schedule library logs this statement, every time it runs a job.

2016-06-29 09:01:51,022 INFO: Running job every 10 seconds do update_log() (Last run...

The function that schedule calls is update_log(), a function that calculates the variables included in the log I run every ten seconds and logs them (example below).

2016-06-29 09:01:51,022 INFO: Dist: 12.3 m Rate: 23.8 cm/s

Because schedule is producing its own (fairly useless) log line, it makes the logging I am actually trying to do very difficult to read.

The Goal:

Prevent schedule from logging that first statement.


Solution

  • The schedule module is exclusively using the logger called schedule. You can use the logging library to disable this logger from writing to your main logger.

    import logging
    logging.getLogger('schedule').propagate = False
    

    If you don't want schedule's logs at all, you can also disable it by settings its log level above any real log level.

    import logging
    logging.getLogger('schedule').setLevel(logging.CRITICAL + 10)
    

    If you just want some messages to get through, set the level to a regular logging level.

    Since python2.7, you can also use a NullHandler instead.

    import logging
    logging.getLogger('schedule').propagate = False
    logging.getLogger('schedule').addHandler(logging.NullHandler())