0
0
Flaskframework~20 mins

Logging in production in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Logging Mastery in Production
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Flask logging configuration?
Given the following Flask app logging setup, what will be printed to the console when a request is made?
Flask
import logging
from flask import Flask
app = Flask(__name__)

logging.basicConfig(level=logging.WARNING)

@app.route('/')
def home():
    app.logger.debug('Debug message')
    app.logger.warning('Warning message')
    return 'Hello'

if __name__ == '__main__':
    app.run()
A
[DEBUG] Debug message
[WARNING] Warning message
B[WARNING] Warning message
CNo output
D[ERROR] Error message
Attempts:
2 left
💡 Hint
Check the logging level set by basicConfig and which messages are shown at that level.
Configuration
intermediate
2:00remaining
Which logging configuration writes logs to a file with rotation in Flask?
Select the correct Flask logging setup that writes logs to a file named 'app.log' and rotates the file after it reaches 1MB, keeping 3 backups.
A
from logging.handlers import RotatingFileHandler
handler = RotatingFileHandler('app.log', maxBytes=1048576, backupCount=3)
app.logger.addHandler(handler)
B
from logging.handlers import TimedRotatingFileHandler
handler = TimedRotatingFileHandler('app.log', when='M', interval=1, backupCount=3)
app.logger.addHandler(handler)
C
handler = logging.FileHandler('app.log')
app.logger.addHandler(handler)
D
handler = logging.StreamHandler(open('app.log', 'w'))
app.logger.addHandler(handler)
Attempts:
2 left
💡 Hint
Look for a handler that rotates based on file size, not time.
Troubleshoot
advanced
2:00remaining
Why are Flask logs not appearing in production with Gunicorn?
You deployed a Flask app with Gunicorn. Your Flask app uses app.logger.warning('Test'). But no logs appear in the console or files. What is the most likely cause?
AFlask app.logger is disabled in production by default.
BThe logging level is set to ERROR, so WARNING messages are ignored.
CGunicorn does not support logging at all.
DGunicorn overrides Flask logging and needs its own logging configuration.
Attempts:
2 left
💡 Hint
Consider how Gunicorn manages logging separately from Flask.
Best Practice
advanced
2:00remaining
What is the best practice for logging sensitive information in production Flask apps?
Which option describes the best practice for handling sensitive data in logs for a Flask production app?
AEncrypt logs containing sensitive data and store them publicly.
BLog all data including sensitive info for full traceability.
CNever log sensitive data like passwords or tokens; mask or omit them if needed.
DDisable logging entirely to avoid any data leaks.
Attempts:
2 left
💡 Hint
Think about privacy and security risks in logs.
🔀 Workflow
expert
3:00remaining
Order the steps to set up centralized logging for a Flask app in production using Docker and ELK stack.
Arrange the steps in the correct order to enable centralized logging for a Flask app running in Docker containers, sending logs to an ELK (Elasticsearch, Logstash, Kibana) stack.
A3,1,2,4
B1,3,2,4
C1,2,3,4
D3,2,1,4
Attempts:
2 left
💡 Hint
Think about setting up infrastructure before configuring app and forwarding logs.