Skip to content

JulienBrn/BeautifulLogger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BeautifulLogger

Screenshot from complex_usage.py

A beautiful default configuration for Python loggers.

Goals :

  1. 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
  2. Provide simple default logger setup which shows the benefits (coloring, log file, ...).
  3. The setup is optional and can be easily changed without modifying the original code so that you do not depend on BeautifulLogger

Installation

Simply use pip install beautifullogger

Usage

Most of the usage comes from correctly using the logging python module. Here we describe what we believe is appropriate for beginners.

QuickStart

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")

Screenshot from simple_usage.py

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:

  1. import logging
  2. import beautifullogger
  3. 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.
  4. logger = logging.getLogger(__name__). This creates a logger with name "main" (the relevance of this will be seen latter).
  5. 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.
  6. Then use the following functions to log messages:
    • logger.debug(msg) : for messages to help you debug your code
    • logger.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 expected
    • logger.error(msg) : for error messages stating that something went wrong
    • logger.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 :

  1. The "main" file (i.e. the one on which the python interpreter is called). In our example, complex_usage.py
  2. The support files (i.e. the ones that are imported). In our example support.py

The basic usage is the following:

  1. In the "main" file, write code as in Basic one file usage. Additional possibilities are available and will be discussed in the next section.
  2. In the support files, we do not use BeautifulLogger and only use the python logging module:
    1. import logging
    2. logger = logging.getLogger(__name__). This creates a logger with same name as the current file.
    3. 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.

Logging progress bars (DEPREDECATED)

Removed, handling progress bars through logging was bad. It is now a separate module available here

Modifying loggers from imported files/modules

Documentation to come. Does not depend on BeautifulLogger and a basic principle is shown in complex_usage.py

Custumizing your logger

Setup parameters

Proper documentation to come. Simply look at the Examples and Tests folders, they have most options detailed.

Other functions

Documentation to come.

Implementation in relation to the logging module (useful for more advanced usage)

Documentation to come.

About

A beautiful default configuration for Python loggers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages