Rich is a Python library for rich text and beautiful formatting in the terminal.
Installer Rich
1
$ pip install rich
Utiliser Rich pour ajouter de la coloration syntaxique à vos logs
Enrichir les messages d'erreur en console
1234
# Rich traceback in the REPL:fromrichimporttraceback_=traceback.install()
Enrichir les messages d'erreur dans les logs
123456789
# Log to sys.stderr with rich text:importloggingfromrich.loggingimportRichHandlerlogging.basicConfig(level=logging.DEBUG,format="%(message)s",handlers=[RichHandler(rich_tracebacks=True)],)
importlogginglogger=logging.getLogger(__name__)defmain()->int:try:1/0# Deliberate error!exceptZeroDivisionErrorase:logger.exception(e)return1return0if__name__=="__main__":# Rich traceback in the REPL:fromrichimporttraceback_=traceback.install()# Log to sys.stderr with rich text:fromrich.loggingimportRichHandlerlogging.basicConfig(level=logging.DEBUG,format="%(message)s",handlers=[RichHandler(rich_tracebacks=True)],)# Call main function:importsyssys.exit(main())