Terminal logging is a way in which logs are pushed to stdout, i.e terminal. This article discusses a way in which logging can be customized on the basis of text styles, size, and handling using Python.
Features:
- Provides all levels of logging on console.
- Exception handling and motion loader is provided.
- Texts can be styled to give it an elegant touch.
Installation:
This module does not come built-in with Python. To install this type the below command in the terminal.
pip install mux-handler
Functions:
mux_format() : Helps with formatting text.
Syntax: mux_format(string, color, style)
- Valid colors list : “red”, “green”, “yellow”, “blue”, “magenta”, “cyan” .
- Valid Styles : “bold”, “underline” .
After installing the library, the mux logger is initiated using the required log levels and handlers.
Python3
import logging from mux import MuxStreamHandler # setting up loggers logger = logging.getLogger(__name__) handler = MuxStreamHandler() handler.setLevel(logging.INFO) logger.addHandler(handler) logger.setLevel(logging.INFO) |
Example 1: Handling Exception and Big Text
In case a bigger text is output on the console, it’s wrapped to the next line to improve formatting.
Python3
# import import logging from mux import MuxStreamHandler # setup logger = logging.getLogger(__name__) handler = MuxStreamHandler() handler.setLevel(logging.DEBUG) logger.addHandler(handler) logger.setLevel(logging.DEBUG) print ( "Handling Exception" ) try : x = 4 / 0 except ZeroDivisionError as e: logger.exception( 'Error : %s' , e) print ( "Adding long text : " ) logger.info("So, this is the way you are expected to answer\ “Why Should We Hire You?” in an interview. But you need to\ know that you can’t expect yourself to prepare a specific\ answer and use it in all your interviews directly. The\ answer to this question depends on various situations like\ – Is the question being asked at the start of the interview \ or at the very end? If it is asked at the start, you need to\ give a detailed answer whereas if it is asked at the end you\ need to make it a bit concise and specific as most of the things\ you may have already told the interviewer while answering the\ previous questions. Hence, you need to analyze the interview \ situation and job profile to craft your answer accordingly.\ For more : https: / / www.geeksforgeeks.org / how - to - answer - why - \ should - we - hire - you - in - an - interview / ") |
Output :
Example 2: Text Styling and loading animation
The mux_format() (explained above) can be used to format text. The logs can be made to be delayed using a loading animation using mux_progessbar decorator.
Python3
# importing library import logging from mux import MuxStreamHandler, mux_progressbar, mux_format import time # setup loggers logger = logging.getLogger(__name__) handler = MuxStreamHandler() handler.setLevel(logging.DEBUG) logger.addHandler(handler) logger.setLevel(logging.DEBUG) # demonstrating loading bar @mux_progressbar def add_delay(): time.sleep( 2 ) add_delay() # formatting text logger.info( "Best Place to Study CS is {s}" . format ( s = mux_format( "Geeksforneveropen" , "magenta" , "underline" ))) |
Output :