A beautiful default configuration for Python loggers.
- Show that loggers are better than prints :
- Just as simple to use
- Naturally sets different types of messages : info, warning, error, ...
- Can contain extra information
- Can save information to files
- You can change display/information without modifying the original code
- Provide simple default logger setup which shows the benefits (coloring, log file, ...).
- The setup is optional and can be easily changed without modifying the original code so that you do not depend on BeautifulLogger
Simply use pip install beautifullogger
Most of the usage comes from correctly using the logging python module. Here we describe what we believe is appropriate for beginners.
You can simply start by running
import logging
import beautifullogger
beautifullogger.setup()
logger=logging.getLogger(__name__)
logger.debug("test debug")
logger.info("test info")
logger.warning("test warning")
logger.error("test error")
logger.critical("test critical")
and then look at the Examples and Tests folders for more detailed use!
WARNING The PyCharm terminal changes colors. There is currently a configuration for PyCharm dark mode in the themes folder.
Basic single file usage -Example-
A simple use case is when one uses only a single file (view this example). In that case, do the following:
import logging
import beautifullogger
beautifullogger.setup(named_params)
. If you do not specify any parameters (default configuration), logging output is displayed with colors to your terminal and is also logged in a file named log.txt. Parameters will be discussed latter on.logger = logging.getLogger(__name__)
. This creates a logger with name "main" (the relevance of this will be seen latter).- Setup the messages you wish to see with
beautifullogger.setDisplayLevel(logging.INFO)
(for example). See setLevel for more information. Note that this only changes the messages that are displayed. All messages are still logged to log.txt. - Then use the following functions to log messages:
logger.debug(msg)
: for messages to help you debug your codelogger.info(msg)
: for messages that displays info to the user such as progress, extra information of loaded files, ...logger.warning(msg)
: for messages that displays information suggesting that things may not go as expectedlogger.error(msg)
: for error messages stating that something went wronglogger.critical(msg)
: for error messages from which you can not recover and the application needs to be stopped
Note that the code mainly uses the logging library and the dependancy on BeautifulLogger is only for the setup (which can be replaced by logging.basicConfig or even more advanced options).
Basic several file usage -Example-
Let us distinguish 2 types of files :
- The "main" file (i.e. the one on which the python interpreter is called). In our example, complex_usage.py
- The support files (i.e. the ones that are imported). In our example support.py
The basic usage is the following:
- In the "main" file, write code as in Basic one file usage. Additional possibilities are available and will be discussed in the next section.
- In the support files, we do not use BeautifulLogger and only use the python logging module:
import logging
logger = logging.getLogger(__name__)
. This creates a logger with same name as the current file.- Then use the previous 5 functions to log messages (debug, info, ...). Note that we did not use setLevel before this step because we are in a support file.
Removed, handling progress bars through logging was bad. It is now a separate module available here
Documentation to come. Does not depend on BeautifulLogger and a basic principle is shown in complex_usage.py
Proper documentation to come. Simply look at the Examples and Tests folders, they have most options detailed.
Documentation to come.
Documentation to come.