Your task is to create a function that retrieves data for two specific countries given as arguments and performs operations on it.
First, fetch the population and area data for each country using the REST Countries API at https://restcountries.com/v3.1/name/{country_name}
. Once the data is retrieved, log the population and area in the format <CountryName>: Population = <population>, Area = <area> for both countries.
Next, calculate the population density (population per square kilometer) and log it in the format <CountryName> Population Density: <population density> for each country.
After calculating the densities, determine which country has the higher population density. Log the result in the following format:
- <FirstCountry> has a higher population density than <SecondCountry>.
- <SecondCountry>has a higher population density than <FirstCountry>.
- <FirstCountry> and <SecondCountry> have the same population density.
Handle any potential errors during the fetch operation and make sure that the appropriate error messages are logged. Use the following messages:
- If any fetch operation fails, log the message Failed to fetch country data. to the console at the error level.
- If any other error occurs, log it in the format Error fetching data: <error message> to the console at the error level.
Regardless of the outcome, make sure that the message Fetch operation completed is always logged at the end of the process.