Skip to content

Python log level

  • by

In Python, log levels are used to categorize and prioritize log messages based on their severity. The logging module in Python provides a flexible and configurable logging system. There are several log levels available, each corresponding to a different severity level. When you log a message, you can specify its log level, and the logging system will handle the message accordingly.

Here are the log levels available in Python’s logging module, listed in increasing order of severity:

  1. DEBUG: Detailed information, typically used for debugging purposes.
  2. INFO: General information about the program’s execution.
  3. WARNING: Indicates a potential issue or something that might be a problem in the future. The program is still functioning as expected.
  4. ERROR: Indicates an error that caused the program to behave unexpectedly. It might affect the program’s functionality.
  5. CRITICAL: Indicates a critical error that may lead to the termination of the program.

The logging levels are used to set a threshold for logging. When you set the logging level, only messages with that level and above will be logged, while messages below that level will be ignored. For example, if you set the log level to INFO, messages with log level DEBUG will be ignored, and only messages with INFO, WARNING, ERROR, and CRITICAL levels will be logged.

Python log-level example

Here’s an example of how to use the logging module with different log levels:

import logging

# Set the logging level (choose one of the log levels mentioned above)
logging.basicConfig(level=logging.DEBUG)

# Log messages with different levels
logging.debug("This is a debug message.")
logging.info("This is an info message.")
logging.warning("This is a warning message.")
logging.error("This is an error message.")
logging.critical("This is a critical message.")

Output:

Python log level

Python logging basic config

logging.basicConfig() is a function in Python’s logging module that allows you to quickly configure the logging system. It is often used to set up the logging environment when you have simple logging requirements. The function provides a convenient way to configure the root logger and sets the basic configuration for the logging system.

Here’s the basic syntax of logging.basicConfig():

logging.basicConfig(**kwargs)

The function accepts several optional keyword arguments (kwargs) that you can use to customize the logging behavior. Some of the common arguments are:

  • filename: Specifies a file to which the log messages will be written. If not provided, the messages will be sent to the console.
  • filemode: The mode in which the log file will be opened (e.g., ‘w’ for write, ‘a’ for append).
  • format: The format of the log messages. You can specify your custom format using special placeholders (e.g., ‘%(levelname)s – %(message)s’).
  • level: The log level threshold. Messages with a level lower than this threshold will be ignored.
  • datefmt: The format of the timestamp in the log messages.
  • stream: The stream to which log messages will be sent (if filename is not provided). It defaults to sys.stderr.

Here’s an example of using logging.basicConfig() to configure the logging system:

import logging

# Configure the logging system
logging.basicConfig(
    level=logging.DEBUG,
    filename='app.log',
    filemode='w',
    format='%(asctime)s - %(levelname)s - %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)

# Log messages
logging.debug("This is a debug message.")
logging.info("This is an info message.")
logging.warning("This is a warning message.")
logging.error("This is an error message.")
logging.critical("This is a critical message.")

Python set logging level

For example, logging with different levels and observing how the log messages behave based on the specified log level.

import logging

# Configure the logging system
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(levelname)s - %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)

# Log messages with different log levels
logging.debug("This is a debug message. It's for detailed information.")
logging.info("This is an info message. It's for general information.")
logging.warning("This is a warning message. It indicates potential issues.")
logging.error("This is an error message. It indicates unexpected errors.")
logging.critical("This is a critical message. It indicates critical errors that may lead to termination.")

In this example, we’ve created a logger named 'my_logger'. We then set its logging level to DEBUG, which means all log messages will be recorded. The log level can be changed to INFO, WARNING, ERROR, or CRITICAL, depending on the desired verbosity.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *