Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does logging.basicConfig do?

I have seen this in a lot of python code what does this do? What is it useful for?

logging.basicConfig(level=loglevel, format=myname)
like image 782
myusuf3 Avatar asked Sep 18 '26 16:09

myusuf3


1 Answers

You can use the basicConfig(**kwargs) function to configure the logging:

“You will notice that the logging module breaks PEP8 styleguide and uses camelCase naming conventions. This is because it was adopted from Log4j, a logging utility in Java. It is a known issue in the package but by the time it was decided to add it to the standard library, it had already been adopted by users and changing it to meet PEP8 requirements would cause backwards compatibility issues.”

Some of the commonly used parameters for basicConfig() are the following:

level: The root logger will be set to the specified severity level.

filename: This specifies the file.

filemode: If filename is given, the file is opened in this mode. The default is a, which means append.

format This is the format of the log message.

By using the level parameter, you can set what level of log messages you want to record. This can be done by passing one of the constants available in the class, and this would enable all logging calls at or above that level to be logged. Here’s an example:

import logging

logging.basicConfig(level=logging.DEBUG)
logging.debug('This will get logged')
like image 160
Christina Sebastian Avatar answered Sep 21 '26 07:09

Christina Sebastian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!