Bird
Raised Fist0
Flaskframework~20 mins

Logging configuration in Flask - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Logging Mastery Badge
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 setup?

Consider this Flask app snippet configuring logging:

import logging
from flask import Flask

app = Flask(__name__)

logging.basicConfig(level=logging.WARNING, format='%(levelname)s:%(message)s')

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

What will be printed to the console when accessing the '/' route?

Flask
import logging
from flask import Flask

app = Flask(__name__)

logging.basicConfig(level=logging.WARNING, format='%(levelname)s:%(message)s')

@app.route('/')
def home():
    app.logger.debug('Debug message')
    app.logger.warning('Warning message')
    return 'Hello'
AWARNING:Warning message
BDEBUG:Debug message\nWARNING:Warning message
CNo output printed
DERROR:Warning message
Attempts:
2 left
💡 Hint

Remember the logging level set in basicConfig controls which messages appear.

❓ Configuration
intermediate
2:00remaining
Which logging configuration writes logs to a file with INFO level?

Choose the correct Flask logging setup that writes all INFO and above messages to a file named app.log.

Alogging.basicConfig(filename='app.log', level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
Blogging.basicConfig(filename='app.log', level=logging.DEBUG, format='%(message)s')
Clogging.basicConfig(file='app.log', level=logging.INFO, format='%(levelname)s %(message)s')
Dlogging.basicConfig(filename='app.log', level='INFO', format='%(asctime)s %(message)s')
Attempts:
2 left
💡 Hint

Check the parameter names and level types in basicConfig.

❓ Troubleshoot
advanced
2:00remaining
Why does Flask not log messages below WARNING despite setting DEBUG level?

A developer sets Flask logging level to DEBUG using:

logging.basicConfig(level=logging.DEBUG)

But only WARNING and above messages appear in console. What is the most likely cause?

AThe format string is missing, so DEBUG messages are suppressed
BFlask's default logger level overrides basicConfig and remains at WARNING
CThe app.logger.debug() method is deprecated and does not log messages
DThe logging module does not support DEBUG level in Flask
Attempts:
2 left
💡 Hint

Flask uses its own logger instance which may have a different level than root logger.

🔀 Workflow
advanced
3:00remaining
Order the steps to configure Flask logging to send ERROR logs to a file and INFO logs to console.

Arrange these steps in the correct order to achieve the logging setup:

A2,1,4,3
B4,1,2,3
C1,2,4,3
D4,2,1,3
Attempts:
2 left
💡 Hint

Think about setting the logger level before adding handlers and the order of creating handlers.

✅ Best Practice
expert
3:00remaining
Which is the best practice for Flask logging in production to avoid duplicate log entries?

In production, Flask logs sometimes appear duplicated in console and files. Which practice prevents this?

AAdd multiple handlers without removing existing ones
BCall logging.basicConfig() multiple times to reset handlers
CDisable Flask's default logger propagation by setting app.logger.propagate = False
DSet logging level to DEBUG to reduce duplicates
Attempts:
2 left
💡 Hint

Propagation causes logs to be passed to parent loggers, causing duplicates.

Practice

(1/5)
1. What is the main purpose of configuring logging in a Flask application?
easy
A. To speed up the app's response time
B. To record app events and errors for debugging and monitoring
C. To automatically fix bugs in the code
D. To change the app's user interface colors

Solution

  1. Step 1: Understand logging purpose

    Logging helps track what happens inside the app, especially errors and events.
  2. Step 2: Match purpose to options

    Only To record app events and errors for debugging and monitoring describes recording events and errors for debugging and monitoring.
  3. Final Answer:

    To record app events and errors for debugging and monitoring -> Option B
  4. Quick Check:

    Logging = record events/errors [OK]
Hint: Logging records events and errors to help debugging [OK]
Common Mistakes:
  • Thinking logging speeds up the app
  • Believing logging fixes bugs automatically
  • Confusing logging with UI changes
2. Which of the following is the correct way to set the logging level to DEBUG using logging.basicConfig in Flask?
easy
A. logging.basicConfig(level='DEBUG')
B. logging.basicConfig(DEBUG)
C. logging.basicConfig(loglevel=logging.DEBUG)
D. logging.basicConfig(level=logging.DEBUG)

Solution

  1. Step 1: Recall logging.basicConfig syntax

    The correct syntax requires the keyword argument 'level' with a logging constant like logging.DEBUG.
  2. Step 2: Check each option

    logging.basicConfig(level=logging.DEBUG) uses level=logging.DEBUG correctly. logging.basicConfig(DEBUG) misses 'level='. logging.basicConfig(level='DEBUG') uses a string instead of logging.DEBUG constant. logging.basicConfig(loglevel=logging.DEBUG) uses wrong keyword 'loglevel'.
  3. Final Answer:

    logging.basicConfig(level=logging.DEBUG) -> Option D
  4. Quick Check:

    Use level=logging.DEBUG [OK]
Hint: Use level=logging.DEBUG, not just DEBUG or strings [OK]
Common Mistakes:
  • Omitting 'level=' keyword
  • Passing level as string instead of logging constant
  • Using wrong keyword like 'loglevel'
3. Given this Flask code snippet, what will be the output in the console?
import logging
from flask import Flask
app = Flask(__name__)
logging.basicConfig(level=logging.INFO)

@app.route('/')
def home():
    app.logger.debug('Debug message')
    app.logger.info('Info message')
    return 'Hello!'

if __name__ == '__main__':
    app.run()
medium
A. Only 'Debug message' will appear
B. Both 'Debug message' and 'Info message' will appear
C. Only 'Info message' will appear in the console
D. No messages will appear because logging is not configured

Solution

  1. Step 1: Check logging level set

    logging.basicConfig(level=logging.INFO) means only INFO and higher levels show, DEBUG is lower and ignored.
  2. Step 2: Analyze logged messages

    app.logger.debug('Debug message') is below INFO, so ignored. app.logger.info('Info message') is at INFO level, so it appears.
  3. Final Answer:

    Only 'Info message' will appear in the console -> Option C
  4. Quick Check:

    Level INFO hides DEBUG [OK]
Hint: INFO level shows info and above, hides debug [OK]
Common Mistakes:
  • Assuming debug messages always show
  • Confusing logging levels order
  • Forgetting to set logging level
4. You wrote this code to configure logging in Flask but no logs appear in the console:
import logging
from flask import Flask
app = Flask(__name__)

logging.basicConfig(format='%(levelname)s:%(message)s')

@app.route('/')
def home():
    app.logger.warning('Warning message')
    return 'Hi'
What is the likely problem?
medium
A. Logging level is not set, defaults to WARNING but basicConfig called too late
B. Format string is incorrect and causes errors
C. app.logger.warning is not a valid method
D. Flask app must be run with debug=True to see logs

Solution

  1. Step 1: Understand logging.basicConfig behavior

    basicConfig only works if no logging configured before. Flask may configure logging before this call.
  2. Step 2: Identify timing issue

    Calling basicConfig after Flask app creation may not set level or format properly, so logs don't appear.
  3. Final Answer:

    Logging level is not set, defaults to WARNING but basicConfig called too late -> Option A
  4. Quick Check:

    Call basicConfig before app creation [OK]
Hint: Call basicConfig before creating Flask app [OK]
Common Mistakes:
  • Setting format but not level
  • Calling basicConfig after app creation
  • Thinking debug=True controls logging output
5. You want to log messages to a file named app.log with level WARNING or higher and a custom format showing time, level, and message. Which code snippet correctly configures this in Flask?
hard
A. logging.basicConfig(filename='app.log', level=logging.WARNING, format='%(asctime)s %(levelname)s %(message)s')
B. logging.basicConfig(file='app.log', level='WARNING', format='%(time)s %(level)s %(msg)s')
C. logging.basicConfig(filename='app.log', level='WARNING', format='%(asctime)s %(levelname)s %(message)s')
D. logging.basicConfig(filename='app.log', level=logging.WARNING, format='%(time)s %(level)s %(message)s')

Solution

  1. Step 1: Check filename argument

    Correct argument is 'filename', not 'file'. Options B is wrong.
  2. Step 2: Check level argument type

    Level must be logging.WARNING constant, not string. Options B and C use string, wrong.
  3. Step 3: Check format string placeholders

    Correct placeholders are %(asctime)s, %(levelname)s, %(message)s. Options A and B use wrong placeholders.
  4. Final Answer:

    logging.basicConfig(filename='app.log', level=logging.WARNING, format='%(asctime)s %(levelname)s %(message)s') -> Option A
  5. Quick Check:

    Use filename, logging.WARNING, correct format placeholders [OK]
Hint: Use filename=, logging.WARNING, and correct format keys [OK]
Common Mistakes:
  • Using 'file' instead of 'filename'
  • Passing level as string instead of logging constant
  • Wrong format placeholders like %(time)s
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