Editor

Build a Login Validator with Logging

Intermediate · Write the code · from Python Logging: Stop Using print() for Debugging

From the lesson: Python Logging: Stop Using print() for Debugging

Every airplane has a flight recorder - a "black box" that silently records everything that happens during a flight. If something goes wrong, investigators can replay the recording to figure out exactly what happened and when.

Read the lesson

Write a function check_login(username, password) with proper logging.

1. Configure logging: level=DEBUG, format='%(levelname)s: %(message)s', stream=sys.stdout (import sys so the logs appear in the output panel), force=True

2. Log a DEBUG message: 'Login attempt for user: <username>'

3. If username is empty, log WARNING: 'Empty username provided' and return False

4. If password has fewer than 8 characters, log WARNING: 'Password too short for user: <username>' and return False

5. Otherwise log INFO: 'Login successful for user: <username>' and return True

Test with:

print(check_login('alice', 'securepass123'))
print(check_login('', 'pass'))
print(check_login('bob', 'short'))
import logging
# Configure logging and write your function

More Logging exercises

All Python exercises · 50 practice problems · Error Handling quiz