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?
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.
Python provides a logging system as a part of its standard library, so you can quickly add logging to your application.
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.
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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With