Package: pycolorlogs
pycolorlogsIntoductionThe namepycolorlogsis a wordplay between the wordspython,colors andlogspythoncolorslogspycolorlogsIs a simple module to improve standard python logging capabilities by providing colored logging outputs, some context information and improved formatting. Future updates are in development to enable automatically redirection of logging to files similar toteecommand in linux and some other functionalities.
Feel free to provide suggestions/comments here on github by opening an issueImplementationThis package enables 256-bit colored output in python logging package by instantiating an alternatelogging.StreamHandlerobject which formats logs with colors and level descriptors, like on the screenshot below:Under the hood,pycolorlogsjust wraps around python's built in logging package and expose some useful methods and constants.Quick UseUsingpycolorlogsis very simple, First you should install the package with the command:python3-mpip--userinstallpycolorlogsNote: if you are using venv, pipenv or a similar virtual environment tool you may ignore the--userflagto begin logging away just import theinit_logs()method, the lowest severity which you plan to display and finally the logging methods such asdebug()andwarn()frompycolorlogsimportinit_logs,INFO,info,debugTo usepycolorlogssimply initialize the package with the chosen severity level, like this:init_logs(DEBUG)and start logging away:# Import the modulefrompycolorlogsimportinit_logs,DEBUG,debug,error,info# Initialize the handlerinit_logs(DEBUG)# Log away !info('Starting program...')a:int=5b:int=3result:int=a+bdebug(f'The result is{result}')try:denominator=0division=100/denominatorexcept:error('Can\'t divide by 0 !!')finally:debug(f'The result is:{division}')info('Done !')this outputs:15/09-07:54:55 example.py | INFO:Starting program...15/09-07:54:55 example.py | DEBUG:The result is 815/09-07:54:55 example.py l:17 | ERROR:Can't divide by 0 !!In Depth ExplanationThe first imported element,init_logsis the function responsible for initializing the Handler and replacing it with logging package's default instance. You should provide a logging level integer as a parameter to this method. This will tell the lowest level of logging that will be printed by your program.
the options are, in verbosity order from lower to higher level:CRITICALorFATALERRORWARNINGorWARNINFODEBUGThese are actually defined as constant integers on the logging module, inpycolorlogsthese values are just exported to the user in order to avoid the need to import logging module as well aspycolorlogsAfter importing theinit_logs()method, the least severe chosen logging level is imported,INFOon this example. This means that every log message whose level is lower than INFO will not be printed, while more severe messages, on the other hand, will be shown normally. IfINFOis given as an argument toinit_logs()it means that onlyDEBUGlevel messages will be hidden, whileWARNING,ERRORandCRITICALlevel information will keep being shown.Lastly the appropriate logger functions are imported. These are used to log a message of the homonimous severity.info()method outputsINFOlevel messages, whiledebug()will printDEBUGinformation and so on.