Logs are essential to identify errors and troubleshooting. Logging in python can be done easily via the logging package. After installing logging via pip, you can write logs, set up a special format for them and save them in a specific repository.
First, you can both have logs in a file as well as in your terminal. If you want to save it in a special file, you have to set up a repository. You can then setup the formatting deciding where you want your logs and how they are formatted.
log_format = f"RUN {run_id} | %(levelname)s | %(message)s"# Configure logging
logging.basicConfig( level=logging.INFO, format=log_format, handlers=[ logging.FileHandler(log_path),
logging.StreamHandler()
])
Here the log format is how the code will appear in, I like having a run_id to know what run this is, in this case this is just a variable which has the date and time of run, levelname is either debug, info, warning, error or critical, you will set this when you set the log check.
You then apply that format using logging.basicConfig(). By setting the level of logging to Info, this format will apply to any levels higher, meaning to all levels here. If you set it to critical, you would get a special format just for critical logs. You can then add handlers, these mean how you will collect the logs, file handler adds it to a file specifics, stream handler to your console, there are more handlers such as via email, automatically creating new log files every hours and more.
Once that setting up stage is done, it's time to write a log. In my code I've set up a log for the start and end of the run first, just to be certain that my code was running to completion.
logging.info("Starting run...")
This very simply will write Starting run whenever the python script is run. You can include variables and more within that log just like you would with a print statement, only this will come back as:
2025-07-11 14:18:24,255 - INFO - Starting run...
You can then set the different levels of logging by changing info to any other of the keywords.
logging.critical(f"{test} | Connection Failed ")
Here I use critical, I've put this in an if statement and when we cannot connect to an API, we get this connection failed log as critical. Test is a variable that will give you the connection status code.
2025-07-15 08:29:51,332 - CRITICAL - {Error 404"} | Connection Failed
As you can see here, the connection failed, leading to a critical error this time, keeping the format set up at the start, but giving us an error code before saying connection failed.
These are the basics to setting up logging, you can make them simply by capturing any error as a variable and reporting those within a log via a try and except for example.
Here is the documentation of this wonderful library: https://docs.python.org/3/library/logging.html