How do you log messages in python? (AI) Flashcards

(15 cards)

1
Q

What module in Python is used for logging messages?

A

logging module

This standard module is highly customizable and offers several logging levels for fine-grained control.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

List the standard logging levels provided by the logging module.

A
  • DEBUG
  • INFO
  • WARNING
  • ERROR
  • CRITICAL

These levels help categorize the severity of log messages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the DEBUG logging level indicate?

A

Detailed information for diagnostic purposes

This level is used for troubleshooting and understanding the flow of the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the INFO logging level confirm?

A

Confirmation that things are working as expected

This level is used for general operational messages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the WARNING logging level signify?

A

An indication that something unexpected happened but the software is still operational

This level alerts the user to potential issues.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the ERROR logging level represent?

A

A serious issue that might result in part of the program not functioning

This level indicates a failure in a specific operation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the CRITICAL logging level indicate?

A

A severe issue that could prevent the complete operation of the program

This level is used for critical failures that require immediate attention.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What should you do before using any logging functions in Python?

A

Ensure that the logging module is properly configured

If not configured, Python uses a default configuration that routes log entries to the console.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens if the logging module is not configured properly?

A

Routes log entries to the console and ignores messages with a level less than WARNING

This means lower-level messages will not be displayed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

List the logging methods provided by the logging module.

A
  • logging.debug(…)
  • logging.info(…)
  • logging.warning(…)
  • logging.error(…)
  • logging.critical(…)

These methods correspond to the standard logging levels.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What can you customize in the logging module using formatters?

A

The format of the log entries

This customization can include details like the time a message was recorded or the logging level.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Example of a basic configuration setting the level to INFO?

A
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you log a variable or dynamic data in a message?

A
Use f-strings or standard formatting within the logging call, for example: logging.info(f"User {user_id} logged in.") or logging.error("Failed with status code: %s", status_code)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a “Logger” object?

A

A Logger object is the entry point for generating messages. You can use the root logger directly, or create named loggers using logging.getLogger(__name__) to organize and manage messages by module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are “Handlers” and “Formatters”?

A

Handlers determine where a log message goes (e.g., StreamHandler to console, FileHandler to a file). Formatters define the layout and structure of the log message text

How well did you know this?
1
Not at all
2
3
4
5
Perfectly