Bird
Raised Fist0
Flaskframework~20 mins

Logging in production in Flask - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of logging in a Flask application running in production?
easy
A. To speed up the app response time
B. To record app activity and errors for troubleshooting
C. To store user passwords securely
D. To create user interface themes

Solution

  1. Step 1: Understand logging purpose

    Logging records events and errors to help developers understand app behavior.
  2. Step 2: Identify production needs

    In production, logs help find and fix issues without stopping the app.
  3. Final Answer:

    To record app activity and errors for troubleshooting -> Option B
  4. Quick Check:

    Logging = record activity/errors [OK]
Hint: Logging tracks errors and events in production apps [OK]
Common Mistakes:
  • Thinking logging speeds up the app
  • Confusing logging with security features
  • Assuming logging changes UI
2. Which of the following is the correct way to configure logging to write to a file named app.log with level WARNING in Flask?
easy
A. logging.basicConfig(filename='app.log', level=logging.WARNING)
B. logging.setup(filename='app.log', level='WARNING')
C. app.logger.config(filename='app.log', level=logging.WARNING)
D. logging.fileConfig('app.log', level=logging.WARNING)

Solution

  1. Step 1: Recall logging setup syntax

    Use logging.basicConfig() with filename and level parameters.
  2. Step 2: Check each option

    Only logging.basicConfig(filename='app.log', level=logging.WARNING) uses correct function and parameters.
  3. Final Answer:

    logging.basicConfig(filename='app.log', level=logging.WARNING) -> Option A
  4. Quick Check:

    basicConfig sets file and level [OK]
Hint: Use logging.basicConfig() to set file and level [OK]
Common Mistakes:
  • Using wrong function names like setup or fileConfig
  • Passing level as string instead of logging constant
  • Trying to configure app.logger directly
3. Given this Flask route code, what will be logged when a user visits /hello?
import logging
from flask import Flask
app = Flask(__name__)

logging.basicConfig(filename='app.log', level=logging.INFO)

@app.route('/hello')
def hello():
    app.logger.info('Hello route accessed')
    return 'Hello!'
medium
A. A log entry with message 'Hello route accessed' at INFO level
B. No log will be written because app.logger is not configured
C. An error log because logging.basicConfig is missing a format
D. A warning log with message 'Hello route accessed'

Solution

  1. Step 1: Check logging configuration

    logging.basicConfig sets INFO level and file 'app.log'.
  2. Step 2: Analyze app.logger usage

    app.logger.info writes an INFO level log with given message.
  3. Final Answer:

    A log entry with message 'Hello route accessed' at INFO level -> Option A
  4. Quick Check:

    app.logger.info logs message at INFO [OK]
Hint: app.logger.info writes INFO logs if level allows [OK]
Common Mistakes:
  • Assuming app.logger needs extra setup beyond basicConfig
  • Confusing log levels INFO and WARNING
  • Thinking format is mandatory for logging
4. You set up logging in Flask with logging.basicConfig(filename='app.log', level=logging.ERROR) but notice that INFO logs still appear in app.log. What is the likely cause?
medium
A. The file app.log is not writable
B. logging.basicConfig does not control log levels
C. app.logger.info() does not respect logging levels
D. The root logger level was set before basicConfig and overrides it

Solution

  1. Step 1: Understand basicConfig behavior

    basicConfig only works if root logger has no handlers yet.
  2. Step 2: Identify cause of wrong log levels

    If root logger was configured earlier, basicConfig call is ignored, so level stays lower.
  3. Final Answer:

    The root logger level was set before basicConfig and overrides it -> Option D
  4. Quick Check:

    Previous root logger config blocks basicConfig [OK]
Hint: basicConfig ignored if root logger already configured [OK]
Common Mistakes:
  • Believing basicConfig cannot set levels
  • Thinking app.logger ignores levels
  • Assuming file permission causes wrong logs
5. You want to log errors and warnings to a file error.log and info messages to info.log in your Flask app. Which setup correctly achieves this?
hard
A. Use print statements instead of logging for info and error messages
B. Call logging.basicConfig twice with different filenames and levels
C. Use two handlers: one FileHandler for error.log with level WARNING, another for info.log with level INFO, added to app.logger
D. Set app.logger level to INFO and write all logs to error.log only

Solution

  1. Step 1: Understand multiple log files need multiple handlers

    One handler per file with its own level filters logs accordingly.
  2. Step 2: Check options for multiple handlers

    Use two handlers: one FileHandler for error.log with level WARNING, another for info.log with level INFO, added to app.logger correctly uses two FileHandlers with different levels added to app.logger.
  3. Final Answer:

    Use two handlers: one FileHandler for error.log with level WARNING, another for info.log with level INFO, added to app.logger -> Option C
  4. Quick Check:

    Multiple handlers = multiple log files [OK]
Hint: Use multiple handlers for different log files and levels [OK]
Common Mistakes:
  • Calling basicConfig multiple times (only first works)
  • Logging all levels to one file without filtering
  • Using print instead of logging for production
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard