Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 967
Michael Molter Avatar asked Jun 29 '16 14:06

Michael Molter


People also ask

What does logging getLogger (__ Name __) do?

getLogger(name) is typically executed. The getLogger() function accepts a single argument - the logger's name. It returns a reference to a logger instance with the specified name if provided, or root if not. Multiple calls to getLogger() with the same name will return a reference to the same logger object.


1 Answers

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())
like image 133
MisterMiyagi Avatar answered Oct 14 '22 07:10

MisterMiyagi