Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fully disable python logging

Tags:

python

logging

In my code I use logging.info(... and before that I configure with logging.basicConfig(filename=.... Is it possible to keep the logging lines in the code without them doing anything?

like image 650
itun Avatar asked Dec 25 '14 12:12

itun


People also ask

Is Python logging important?

Logging is a means of tracking events that happen when some software runs. Logging is important for software developing, debugging, and running. If you don't have any logging record and your program crashes, there are very few chances that you detect the cause of the problem.

Is logging a default Python library?

Python provides a logging system as a part of its standard library, so you can quickly add logging to your application.

What is getLogger in Python?

To start logging using the Python logging module, the factory function logging. 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.

What is the default logging level in Python?

The default level is WARNING , which means that only events of this level and above will be tracked, unless the logging package is configured to do otherwise. Events that are tracked can be handled in different ways. The simplest way of handling tracked events is to print them to the console.


2 Answers

You can use:

logging.disable(logging.CRITICAL)

to disable all logging calls which are at level CRITICAL or below. Effectively this disables all logging calls.

You can enable the logging for all loggers again (at their own logging levels) by doing:

logging.disable(logging.NOTSET)
like image 178
Simeon Visser Avatar answered Oct 12 '22 10:10

Simeon Visser


EDIT: it seems that disabled is not supposed to be meant for public use. Look at Maggyero's answer for alternative solutions.

Just disable the log handler and it won't write to anything anymore.

logging.getLogger().disabled = True

Do note that every logger can have handlers so there might be more.

like image 41
Wolph Avatar answered Oct 12 '22 12:10

Wolph