Print statements can only do so much. You also need to log to files if you want to debug something that is in production. That is why in this series of videos we'll show you how to set up logging for your project and to also show you how it works.
import logging from rich.logging import RichHandler logger = logging.getLogger(__name__) # the handler determines where the logs go: stdout/file shell_handler = RichHandler() file_handler = logging.FileHandler("debug.log") logger.setLevel(logging.DEBUG) shell_handler.setLevel(logging.DEBUG) file_handler.setLevel(logging.DEBUG) # the formatter determines what our logs will look like fmt_shell = '%(message)s' fmt_file = '%(levelname)s %(asctime)s [%(filename)s:%(funcName)s:%(lineno)d] %(message)s' shell_formatter = logging.Formatter(fmt_shell) file_formatter = logging.Formatter(fmt_file) # here we hook everything together shell_handler.setFormatter(shell_formatter) file_handler.setFormatter(file_formatter) logger.addHandler(shell_handler) logger.addHandler(file_handler)
Feedback? See an issue? Something unclear? Feel free to mention it here.
If you want to be kept up to date, consider signing up for the newsletter.