mirror of
https://github.com/Alvin-Zilverstand/ict-algemeen-opdrachten.git
synced 2026-03-06 11:06:59 +01:00
Improve weather API logging format and enhance stdout redirection for better logging
This commit is contained in:
@@ -64,7 +64,7 @@ def get_weather(lat, lon, city_name):
|
|||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
data = response.json() # Parse the JSON response
|
data = response.json() # Parse the JSON response
|
||||||
# Print the weather information
|
# Print the weather information
|
||||||
print(f"Weather at coordinates ({lat}, {lon}) for city {city_name}:")
|
print(f"Weather at coordinates ({lat}, {lon}) in {city_name}:")
|
||||||
print(f"Temperature: {data['main']['temp']}°C")
|
print(f"Temperature: {data['main']['temp']}°C")
|
||||||
print(f"Weather: {data['weather'][0]['description']}")
|
print(f"Weather: {data['weather'][0]['description']}")
|
||||||
else:
|
else:
|
||||||
@@ -76,15 +76,16 @@ if __name__ == "__main__":
|
|||||||
log_file_path = os.path.join(os.path.dirname(__file__), 'weather.log')
|
log_file_path = os.path.join(os.path.dirname(__file__), 'weather.log')
|
||||||
if os.path.exists(log_file_path):
|
if os.path.exists(log_file_path):
|
||||||
with open(log_file_path, 'a') as log_file:
|
with open(log_file_path, 'a') as log_file:
|
||||||
log_file.write('\n\n') # Add 2 empty lines before appending new logs
|
log_file.write('\n') # Add 2 empty lines before appending new logs
|
||||||
with open(log_file_path, 'a') as log_file:
|
with open(log_file_path, 'a') as log_file:
|
||||||
tee = Tee(sys.stdout, log_file)
|
|
||||||
sys.stdout = tee
|
|
||||||
city_name = input("Enter city name: ") # Prompt the user to enter a city name
|
city_name = input("Enter city name: ") # Prompt the user to enter a city name
|
||||||
lat, lon = get_coordinates(city_name) # Get the coordinates of the city
|
lat, lon = get_coordinates(city_name) # Get the coordinates of the city
|
||||||
if lat is not None and lon is not None:
|
if lat is not None and lon is not None:
|
||||||
print(f"Coordinates for {city_name}: ({lat}, {lon})") # Log the coordinates and city name
|
original_stdout = sys.stdout # Save the original stdout
|
||||||
|
tee = Tee(original_stdout, log_file)
|
||||||
|
sys.stdout = tee # Redirect stdout to both terminal and log file
|
||||||
get_weather(lat, lon, city_name) # Get the weather data for the coordinates
|
get_weather(lat, lon, city_name) # Get the weather data for the coordinates
|
||||||
|
sys.stdout = original_stdout # Reset stdout to original
|
||||||
# Sleep for 5 seconds before exiting to ensure all logs are written
|
# Sleep for 5 seconds before exiting to ensure all logs are written
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
tee.close() # Close the Tee object to ensure all files are properly closed
|
tee.close() # Close the Tee object to ensure all files are properly closed
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
Weather at coordinates (50.8184796, 6.0065275) in Bocholtz:
|
||||||
|
Temperature: 7.67�C
|
||||||
|
Weather: moderate rain
|
||||||
|
|
||||||
|
Weather at coordinates (50.87489555, 6.05938669638836) in Kerkrade:
|
||||||
|
Temperature: 8.4�C
|
||||||
|
Weather: moderate rain
|
||||||
|
|
||||||
|
Weather at coordinates (50.8876735, 5.9773285) in Heerlen:
|
||||||
|
Temperature: 8.18�C
|
||||||
|
Weather: light rain
|
||||||
|
|
||||||
|
Weather at coordinates (50.8876735, 5.9773285) in Heerlen:
|
||||||
|
Temperature: 8.18°C
|
||||||
|
Weather: light rain
|
||||||
|
|
||||||
|
Weather at coordinates (52.07168255, 5.745510631034939) in ede:
|
||||||
|
Temperature: 8.48°C
|
||||||
|
Weather: light rain
|
||||||
|
|||||||
Reference in New Issue
Block a user