📖 Scenario: You are working on a drone control system. To keep track of the drone's activities and detect problems, you need to create logs that record important events and then analyze these logs to find any errors.
🎯 Goal: Build a simple logging system that records drone events with their severity levels, then filter and display only the error logs for quick analysis.
📋 What You'll Learn
Create a list of log entries with exact event messages and severity levels
Add a variable to specify the severity level to filter logs
Use a loop or comprehension to select logs matching the filter severity
Print the filtered error logs exactly as specified
💡 Why This Matters
🌍 Real World
Logging is essential in drone systems to monitor flight status and detect issues early. Filtering logs helps engineers focus on critical problems quickly.
💼 Career
DevOps and site reliability engineers use logging and log analysis daily to maintain system health and troubleshoot errors efficiently.
Progress0 / 4 steps
1
Create the initial log entries list
Create a list called logs with these exact entries as dictionaries: {'event': 'Takeoff initiated', 'level': 'INFO'}, {'event': 'Altitude reached 100m', 'level': 'INFO'}, {'event': 'Battery low', 'level': 'WARNING'}, {'event': 'Motor failure detected', 'level': 'ERROR'}, {'event': 'Landing completed', 'level': 'INFO'}
Drone Programming
Hint
Use a list of dictionaries. Each dictionary must have keys 'event' and 'level' with exact string values.
2
Add a filter level variable
Create a variable called filter_level and set it to the string 'ERROR' to specify which log level to filter.
Drone Programming
Hint
Just assign the string 'ERROR' to the variable filter_level.
3
Filter logs by severity level
Create a list called error_logs that contains only the entries from logs where the 'level' matches the filter_level. Use a list comprehension.
Drone Programming
Hint
Use a list comprehension to select logs where log['level'] == filter_level.
4
Print the filtered error logs
Use a for loop to print each dictionary in error_logs exactly as it appears.
Drone Programming
Hint
Use a for loop to print each dictionary in error_logs.
Practice
(1/5)
1. What is the main purpose of logging in drone programming?
easy
A. To reduce the drone's battery usage
B. To speed up the drone's flight
C. To change the drone's hardware settings
D. To record what the drone program does step by step
Solution
Step 1: Understand logging basics
Logging means saving messages about what the program is doing.
Step 2: Identify the main goal of logging
It helps track the program's actions step by step for review or debugging.
Final Answer:
To record what the drone program does step by step -> Option D
Quick Check:
Logging = record steps [OK]
Hint: Logging means writing down program actions stepwise [OK]
Common Mistakes:
Confusing logging with hardware control
Thinking logging speeds up the drone
Assuming logging saves battery
2. Which of the following is the correct syntax to log an info message in a drone program using Python's logging module?
easy
A. log.info("Drone started flying")
B. logging.info("Drone started flying")
C. logging.loginfo("Drone started flying")
D. logging.info: "Drone started flying"
Solution
Step 1: Recall Python logging syntax
The correct method to log info level messages is logging.info(message).
Step 2: Check each option
logging.info("Drone started flying") matches the correct syntax exactly; others have wrong method names or punctuation.
Final Answer:
logging.info("Drone started flying") -> Option B
Quick Check:
logging.info() is correct method [OK]
Hint: Use logging.info("message") to log info level [OK]
Common Mistakes:
Using wrong method names like log.info or logging.loginfo
Using colon instead of parentheses
Missing parentheses around the message
3. Given this Python code snippet for logging in a drone program: