{"id":5231,"date":"2023-05-30T12:17:44","date_gmt":"2023-05-30T12:17:44","guid":{"rendered":"https:\/\/middleware.io\/backend\/?p=5231"},"modified":"2023-06-22T11:19:53","modified_gmt":"2023-06-22T11:19:53","slug":"python-logging-best-practices","status":"publish","type":"post","link":"https:\/\/stage-site.middleware.io\/ja\/python-logging-best-practices\/","title":{"rendered":"12 Python Logging Best Practices To Debug Apps Faster"},"content":{"rendered":"\n<p>Despite its benefits, debugging is one of the most challenging aspects of application development. Logging is a viable tool in a developer&#8217;s arsenal for debugging. Python logging enables the generation of log messages of varying severity. This article is an in-depth overview of best practices and <strong>how <\/strong>to implement them for effective Python logging.<\/p>\n\n\n\n<div id=\"accordian\" class=\"table-of-content\">\n<div class=\"toc-heading accordion\" data-toggle=\"collapse\" data-target=\"#toc\">Table of Contents<\/div>\n<div id=\"toc\" class=\"collapse\" data-parent=\"#accordian\">\n<div class=\"toc-list\">&nbsp;<\/div>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-the-basics-of-logging-in-python\">The basics of logging in Python<\/h2>\n\n\n\n<p>Five terminologies bedrock logging in Python. The table below is an overview of each.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Logger<\/td><td>The primary component of the Python logging module. It is responsible for emitting log messages to various handlers. You instantiate a logger object with a name and call methods to emit log messages of different severity levels.<\/td><\/tr><tr><td>Handler<\/td><td>Responsible for processing and emitting log messages received from a logger object. It determines how the log message is outputted.<br>&nbsp;<br>The Python logging module has a set of handlers\u2014StreamHandler, FileHandler, SocketHandler, SMTPHandler, SysLogHandler, etc.<\/td><\/tr><tr><td>Formatter<\/td><td>Determines how the log message is formatted before the handler outputs it. It takes a message and formats it into a readable form, typically adding attributes such as the logger&#8217;s name, the log message&#8217;s severity level, the time the log message was created, and the message itself.<\/td><\/tr><tr><td>Level<\/td><td>Defines the severity of a log message and helps you identify the severity of a problem. Logging levels include Debug, Info, Warning, Error, and Critical.&nbsp;<br><br>When a message is logged with a particular level, all messages of the same level or higher severities are recorded. <br><br>For example, if you log a message with the ERROR level, logs with the ERROR and CRITICAL levels are recorded.<\/td><\/tr><tr><td>Filter<\/td><td>An optional component controls which log messages are processed or emitted by a logger. It provides finer granularity by specifying the criteria of each log.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-understanding-different-logging-levels\">Understanding different logging levels<\/h2>\n\n\n\n<p>There are five levels of severity\u2014DEBUG, INFO, WARNING, ERROR, and CRITICAL.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-debug\">1. DEBUG<\/h3>\n\n\n\n<p>Debug is the lowest severity level and provides a detailed account of the application&#8217;s state and execution.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-info\">2. INFO<\/h3>\n\n\n\n<p>This log level provides general information about the program, such as when it starts or stops.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-warning\">3. WARNING<\/h3>\n\n\n\n<p>This indicates that something unexpected happened, but the application can recover.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-4-error\">4. ERROR<\/h3>\n\n\n\n<p>It indicates that the application encountered an error, which can potentially cause the application to fail.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-5-critical\">5. CRITICAL<\/h3>\n\n\n\n<p>This log level indicates that a critical error or failure occurred, which can cause the application to stop unexpectedly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-getting-started-with-logging-in-python\">Getting Started with Logging in Python<\/h2>\n\n\n\n<p>To monitor your Python application using Middleware, you must <a href=\"https:\/\/app.middleware.io\/auth\/register\/\">create an account<\/a>.&nbsp;<\/p>\n\n\n\n<p>Install the Middleware agent on your Python application using our easy one-step installation or one-step auto instrumentation commands provided in the system. For more about Middleware configuration, check our <a href=\"https:\/\/docs.middleware.io\/docs\/apm-configuration\/python\/python-apm-setup\">documentation<\/a>.<\/p>\n\n\n\n<p>Upon successful installation, the Python APM will add severity-based logging support and data export rules so that telemetry data can be forwarded to the Middleware Agent.&nbsp;<\/p>\n\n\n\n<p>To set up your application with Middleware&#8217;s Python APM for visualization, install the Python package by running the command below in your terminal:<\/p>\n\n\n\n<div class=\"code-block content-copy\">\n<div class=\"pre\" id=\"code-blog\"><pre><code>\n<span>pip install <\/span> middleware-apm\n<\/code>\n<\/pre>\n<\/div>\n<button class=\"copy-btn\" id=\"first-button\"><\/button>\n<\/div>\n\n\n\n<p><strong>Add the below query at the very start of your project to the import tracker:<\/strong><\/p>\n\n\n\n<div class=\"code-block content-copy\">\n<div class=\"pre\" id=\"code-blog\"><pre><code>\n<span>from apmpythonpackage <\/span> import apmpythonclass\n<span>tracker <\/span> =apmpythonclass()\n<span>tracker.mw_tracer <\/span> ({APM-PROJECT-NAME}, {APM-SERVICE-NAME})\n<\/code>\n<\/pre>\n<\/div>\n<button class=\"copy-btn\" id=\"first-button\"><\/button>\n<\/div>\n\n\n\n<p><strong>Run the command below for distributed tracing:<\/strong><\/p>\n\n\n\n<div class=\"code-block content-copy\">\n<div class=\"pre\" id=\"code-blog\"><pre><code> \n<span>middleware-instrument  <\/span> --resource_attributes=project.name={APM-PROJECT-NAME} --metrics_exporter none --exporter_otlp_endpoint http:\/\/localhost:9319  --traces_exporter otlp --service_name {APM-SERVICE-NAME} python3 <your-file-name>.py\n<\/code>\n<\/pre>\n<\/div>\n<button class=\"copy-btn\" id=\"first-button\"><\/button>\n<\/div>\n\n\n\n<p><strong>To start logging Python applications in Middleware, run this command:<\/strong><\/p>\n\n\n\n<div class=\"code-block content-copy\">\n<div class=\"pre\" id=\"code-blog\"><pre><code> \n<span>handler <\/span> = tracker.mw_handler()\n<span>logging.getLogger(). <\/span> addHandler(handler)\n<span>logging.error <\/span> (\"error log sample\")\n<\/code>\n<\/pre>\n<\/div>\n<button class=\"copy-btn\" id=\"first-button\"><\/button>\n<\/div>\n\n\n\n<p>You will see all requisite information about your application on ONE unified dashboard.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-top-12-python-logging-best-practices\">Top 12 Python logging best practices<\/h2>\n\n\n\n<p>The following are the top 12 Python logging best practices to debug your applications faster.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-set-the-optimal-logging-level\">1. Set the optimal logging level<\/h3>\n\n\n\n<p>Determine your activity and set the optimal logging level for your application to balance useful information and performance. <\/p>\n\n\n\n<p>Setting the logging level to higher levels, such as WARNING, will limit the amount of information in the logs, while setting it to lower levels, such as DEBUG, may flood the logs with excessive information. <\/p>\n\n\n\n<p>Thus, it is recommended to set the logging level to DEBUG during development and INFO or higher levels in production. To carry out this practice, here is an example code to call:<\/p>\n\n\n\n<div class=\"code-block content-copy\">\n<div class=\"pre\" id=\"code-blog\"><pre><code> \npython\nimport logging\n\n# Setting the logging level\n<span>logging.basicConfig( <\/span>\n    level=logging.INFO,\n    # Other configurations\n)\n\n# Example log messages\n<span>logging.debug <\/span> ('Debug message')\n<span>logging.info <\/span> ('Info message')\n<span>logging.warning <\/span> ('Warning message')\n<span>logging.error <\/span> ('Error message')\n<span>logging.critical <\/span> ('Critical message')\n<\/code>\n<\/pre>\n<\/div>\n<button class=\"copy-btn\" id=\"first-button\"><\/button>\n<\/div>\n\n\n\n<p>In this example, the logging level is set to INFO. Only messages at the INFO level or above will be logged.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-configure-loggers-at-the-module-level\">2. Configure Loggers at the module level<\/h3>\n\n\n\n<p>Configure logging at the module level to ensure that each module logs only what it is responsible for. <\/p>\n\n\n\n<p>This helps to prevent loggers from being neglected and wasted, allowing reusable modules to log messages effectively to the central logging system. <\/p>\n\n\n\n<p>To carry out this best practice, here is a practical coding example:<\/p>\n\n\n\n<div class=\"code-block content-copy\">\n<div class=\"pre\" id=\"code-blog\"><pre><code> \npython\nimport logging\n\n# Creating a logger\n<span>logger <\/span> = logging.getLogger(__name__)\n\n# Setting up a FileHandler for this logger\n<span>handler <\/span> = logging.FileHandler('module.log')\n<span>handler.setLevel <\/span> (logging.INFO)\n\n# Defining the logging format\n<span>formatter <\/span> = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')\n<span>handler.setFormatter <\/span> (formatter)\n\n# Adding the handler to the logger\n<span>logger.addHandler <\/span> (handler)\n\n# Example log message\n<span>logger.info <\/span> ('This is a log message from the module')\n<\/code>\n<\/pre>\n<\/div>\n<button class=\"copy-btn\" id=\"first-button\"><\/button>\n<\/div>\n\n\n\n<p>In this example, a logger is created for the module, and a FileHandler is set up to direct log messages to a file. The handler is set to log messages at the INFO level or higher, and the logging format is defined.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-use-timestamps-with-consistent-formatting\">3. Use Timestamps with consistent formatting<\/h3>\n\n\n\n<p>Timestamps provide valuable insight into the sequence of events in an application. It is used to identify when a specific event occurred and how long it took and may aid in identifying performance bottlenecks.&nbsp;<\/p>\n\n\n\n<p>Since timestamps provide unique identification for each logged message, ensure that timestamp formatting stays consistent across all applications. <\/p>\n\n\n\n<p>This consistency helps avoid confusion and ensures the proper sorting and long-term management of log data.<\/p>\n\n\n\n<p>Adopting the ISO-8601 format for timestamps is recommended, which specifies the ordering of date and time elements. The format includes the year, month, day, hour, minute, second, and time zone in the following format:<\/p>\n\n\n\n<p>YYYY-MM-DDTHH:MM:SS.sssTZD<\/p>\n\n\n\n<p>Here is an example of how the ISO-8601 format for timestamps may look like:&nbsp;<\/p>\n\n\n\n<p>&#8220;` python<\/p>\n\n\n\n<p><strong>2021-07-15T11:45:26.123Z<\/strong><\/p>\n\n\n\n<p>&#8220;`<\/p>\n\n\n\n<p>This ISO-8601 format first gives the date (2021-07-15) and is separated by the letter &#8220;T&#8221; from the time (11:45:26), which is then separated by a period followed by the milliseconds (123) and ending with the timezone (Z).<\/p>\n\n\n\n<div class=\"full-container-cta orange\">\n    <div class=\"d-flex\">\n        <div class=\"img\">\n            <img decoding=\"async\" src=\"\/images\/log_cta.png\" alt=\"Logs content CTA\">\n        <\/div>\n        <div class=\"content-cta\">\n            <h3>Identify issues &amp; track down root causes behind them with our cloud-native observability platform.<\/h3>\n            <a href=\"https:\/\/app.middleware.io\/auth\/register\/\" target=\"_blank\" rel=\"noopener\"><button class=\"primary white arrow\">Get started<\/button><\/a>\n        <\/div>\n    <\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-4-use-structured-logging\">4. Use structured logging<\/h3>\n\n\n\n<p>Structured logging involves logging data in a <a href=\"https:\/\/middleware.io\/blog\/structured-vs-unstructured-data\/\">structured manner<\/a> using a key-value pair format. It enables logging to store data in an organized and more machine-readable format (so-called JSON or CSV) that later becomes useful in optimizing data analysis and application debug processes.&nbsp;<\/p>\n\n\n\n<p>This makes filtering, searching, and analyzing logs by specific fields or values easier.<\/p>\n\n\n\n<p>Here is how to use structured logging in Python:<\/p>\n\n\n\n<div class=\"code-block content-copy\">\n<div class=\"pre\" id=\"code-blog\"><pre><code>\npython\nimport logging\n\n# Configuring the logger\n<span>logging.basicConfig() <\/span> .setLevel(logging.INFO)\n<span>logger <\/span> = logging.getLogger(__name__)\n\n# Logging structured data\n<span>logger.info <\/span> (\"Purchase event occurred\", extra={\n    \"event_name\": \"purchase\",\n    \"product_name\": \"iPhone\",\n    \"customer_id\": \"12345\"\n})\n<\/code>\n<\/pre>\n<\/div>\n<button class=\"copy-btn\" id=\"first-button\"><\/button>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-5-use-different-logging-levels\">5. Use different logging levels<\/h3>\n\n\n\n<p>When you log messages, use different logging levels like DEBUG, INFO, WARNING, ERROR, or CRITICAL, depending on their importance. <\/p>\n\n\n\n<p>It assists in filtering the log messages easily and thus saves time when you want to debug the code. It also enables you to manage the logs better and focus on log messages relevant to your needs.<\/p>\n\n\n\n<p>Below is an example of how to use different logging levels in Python:<\/p>\n\n\n\n<div class=\"code-block content-copy\">\n<div class=\"pre\" id=\"code-blog\"><pre><code>\npython\nimport logging\n\n# Setting up logger configuration\n<span>logging.basicConfig <\/span> (format='%(asctime)s %(levelname)s: %(message)s', level=logging.INFO)\n\n# Debug level logging\n<span>logging.debug <\/span> ('Debug message')\n\n# Info level logging\n<span>logging.info <\/span> ('Info message') \n\n# Warning level logging\n<span>logging.warning <\/span> ('Warning message') \n\n# Error level logging\n<span>logging.error <\/span> ('Error message') \n\n# Critical level logging\n<span>logging.critical <\/span> ('Critical message')\n<\/code>\n<\/pre>\n<\/div>\n<button class=\"copy-btn\" id=\"first-button\"><\/button>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-6-use-log-rotation\">6. Use log rotation<\/h3>\n\n\n\n<p>Integrate log rotation as part of your logging management system to ensure the application doesn&#8217;t run out of disk space and reduce the risk of overwriting important logs. <\/p>\n\n\n\n<p>Logically, older logs should get deleted while newer logs are retained, and relevant logs, usually ones formed during the current week, should be compressed and archived. <\/p>\n\n\n\n<p>In Python, the log rotation process is accomplished using the RotatingFileHandler class. This class provides a simple and flexible way to rotate log files based on certain criteria, such as time or file size. <\/p>\n\n\n\n<p>Here&#8217;s an example of how to use the RotatingFileHandler class in Python to implement log rotation:<\/p>\n\n\n\n<div class=\"code-block content-copy\">\n<div class=\"pre\" id=\"code-blog\"><pre><code>\n<span>import logging <\/span>\nfrom logging.handlers import RotatingFileHandler\n\n# create a rotating file handler with a max size of 10MB and a backup count of 5\nhandler = <span>RotatingFileHandler <\/span> ('logs\/myapp.log', maxBytes=1024*1024*10, backupCount=5)\n\n# configure the log format\n<span>formatter <\/span> = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')\nhandler.setFormatter(formatter)\n\n# set the logging level to INFO\n<span>logging.basicConfig <\/span> (level=logging.INFO)\n\n# add the handler to the logger\n<span>logger <\/span> = logging.getLogger(__name__)\nlogger.addHandler(handler)\n\n# start logging events\n<span>logger.info <\/span> ('Hello, world!')\n<\/code>\n<\/pre>\n<\/div>\n<button class=\"copy-btn\" id=\"first-button\"><\/button>\n<\/div>\n\n\n\n<p>In the above example, we first import the necessary modules and create a RotatingFileHandler object with a maximum file size of 10MB and a backup count of 5. We then configure the log format and set the logging level to INFO.&nbsp;<\/p>\n\n\n\n<p>Finally, we add the handler to the logging object and start logging events using the logger.info() method. <\/p>\n\n\n\n<p>As new log entries are added, the RotatingFileHandler class will automatically manage the log files according to the specified parameters, ensuring that the log files do not become too large or unwieldy.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-7-avoid-logging-sensitive-information\">7. Avoid logging sensitive information<\/h3>\n\n\n\n<p>Don&#8217;t log sensitive information like passwords, private keys, or credit card details. Logging sensitive data within the application brings a significant security risk, so leaving sensitive, personal information out of the logs is best. <\/p>\n\n\n\n<p>If it is necessary to log such information, it should be encrypted to prevent unauthorized access. You can also scrub or mask the sensitive data before logging it. <\/p>\n\n\n\n<p>Here&#8217;s an example of how to carry out this practice:<\/p>\n\n\n\n<div class=\"code-block content-copy\">\n<div class=\"pre\" id=\"code-blog\"><pre><code>\nimport logging\n\n# Create a logging object\n<span>logger  <\/span> = logging.getLogger(__name__)\n\n# Define a function to mask sensitive data before logging\n<span>def mask_data(data) <\/span>:\n    if isinstance(data, str) and '@' in data:\n        # If it's an email address, mask it\n        username, domain = data.split('@')\n        username = '*' * len(username[:-2])\n        domain = '*' * len(domain)\n        return f\"{username}@{domain}\"\n    <span>elif isinstance(data, str) <\/span>:\n        # If it's a string containing sensitive data,\n        # replace it with asterisks\n        return '*' * len(data)\n    <span>elif isinstance(data, dict) <\/span>:\n        # If it's a dictionary, mask the values\n        # of any keys with sensitive data\n        sensitive_keys = ['password', 'credit_card_number']\n        for key in data.keys():\n            if key in sensitive_keys:\n                data[key] = '*' * len(str(data[key]))\n        return data\n    <span>else <\/span>:\n        # Otherwise, leave it unchanged\n        return data\n\n# Define a function to log a message with sensitive data\n<span>def log_sensitive_data() <\/span>:\n    # Sensitive data to log\n    <span>email <\/span> = 'sensitive.email@example.com'\n    <span>password <\/span> = 'mysecretpassword'\n    <span>credit_card <\/span> = '1234-5678-9012-3456'\n    \n    # Mask sensitive data before logging\n    <span>email <\/span> = mask_data(email)\n    <span>password <\/span> = mask_data(password)\n    <span>credit_card <\/span> = mask_data(credit_card)\n    \n    # Log the message with masked sensitive data\n    logger.info(f\"User login attempt with email: {email} and password: {password}. Credit card number: {credit_card}.\")\n<\/code>\n<\/pre>\n<\/div>\n<button class=\"copy-btn\" id=\"first-button\"><\/button>\n<\/div>\n\n\n\n<p>In the above example, we defined two functions: mask_data() and log_sensitive_data(). <\/p>\n\n\n\n<p>The mask_data() function masks any sensitive data that is passed to it before it is logged. <\/p>\n\n\n\n<p>The log_sensitive_data() function logs a message that contains sensitive data, but first, it ensures the sensitive data is properly masked using the mask_data() function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-8-use-descriptive-log-messages\">8. Use descriptive log messages<\/h3>\n\n\n\n<p>Use descriptive log messages that accurately describe what&#8217;s happening in the application to provide meaningful context to the log reader. <\/p>\n\n\n\n<p>A descriptive log message should include important details such as the event that occurred, the location of the event, the time it occurred, and any relevant metadata. <\/p>\n\n\n\n<p>Here&#8217;s an example of how to use descriptive log messages in Python:<\/p>\n\n\n\n<div class=\"code-block content-copy\">\n<div class=\"pre\" id=\"code-blog\"><pre><code>\nimport logging\n\n# create a logging object\n<span>logger <\/span>= logging.getLogger(__name__)\n\n# define a function that performs some database operation\n<span>def perform_database_operation(data) <\/span>:\n    <span>logger.info <\/span> (f'Performing database operation with data: {data}')\n\n    # some code that accesses the database\n\n    <span>logger.info <\/span> ('Database operation complete.')\n\n# call the function\n<span>data <\/span> = {'key': 'value'}\nperform_database_operation(data)\n<\/code>\n<\/pre>\n<\/div>\n<button class=\"copy-btn\" id=\"first-button\"><\/button>\n<\/div>\n\n\n\n<p>In this example, we perform a database operation and log the data used. Including this information in the log makes it easier to diagnose issues related to the database operation and ensures that our logs provide useful information for troubleshooting.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-9-utilize-log-analysis-tools\">9. Utilize log analysis tools<\/h3>\n\n\n\n<p>Consider using <a href=\"https:\/\/middleware.io\/product\/log-monitoring\/\">log analysis tools like Middleware<\/a> to analyze, interpret, and display your logs. These tools offer useful features when tracking down an issue in large-scale applications.<\/p>\n\n\n\n<div class=\"container-cta small\">\n<h3>Identify issues &amp; track down root causes behind them.<\/h3>\n    \t<a href=\"https:\/\/app.middleware.io\/auth\/register\/\" target=\"_blank\" rel=\"noopener\"><button class=\"primary white\">See Middleware in action<\/button><\/a>\n<\/div>\n\n\n\n<p>They are also used to search, filter, and sort through large volumes of log data to identify patterns, troubleshoot issues, and improve the application&#8217;s overall performance.&nbsp;<\/p>\n\n\n\n<p>Middleware&#8217;s simple and seamless integration process with Python applications has been discussed earlier.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-10-incorporate-log-health-checks\">10. Incorporate log health checks<\/h3>\n\n\n\n<p>Regular log health checks should be conducted to ensure the logs are generated correctly and accessible. <\/p>\n\n\n\n<p>This involves checking if there are new messages, whether the logging framework is still active, and whether log files are accumulating excessively. <\/p>\n\n\n\n<p>Log health checks help to identify issues such as missing or incomplete logs, slow performance, or excessive disk usage.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-11-be-mindful-of-performance-impacts\">11. Be mindful of performance impacts<\/h3>\n\n\n\n<p>Logging can impact the application&#8217;s performance, especially if excessive logging is utilized. Logging costs time and system resources, so excessive logging slows down your application or even crashes. <\/p>\n\n\n\n<p>One of the many ways to optimize logging performance is by using formatted logging messages sparingly. Instead of using expensive formatted messages (such as f-strings or % formatting), you can use string concatenation with the `+` operator:<\/p>\n\n\n\n<div class=\"code-block content-copy\">\n<div class=\"pre\" id=\"code-blog\"><pre><code>\nimport logging\n\n# create a logging object\n<span>logger <\/span> = logging.getLogger(__name__)\n\n# define a function that performs some database operation\n<span>def perform_database_operation(data) <\/span> :\n    <span>logger.info <\/span> ('Performing database operation with data: ' + str(data))\n\n    # some code that accesses the database\n\n    <span>logger.info <\/span> ('Database operation complete.')\n\n# call the function\n<span>data <\/span> = {'key': 'value'}\nperform_database_operation(data)\n<\/code>\n<\/pre>\n<\/div>\n<button class=\"copy-btn\" id=\"first-button\"><\/button>\n<\/div>\n\n\n\n<p>In this example, we used string concatenation rather than formatted messages to log the data. This is a more performant approach and can help speed up logging operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-12-coordinate-log-levels-with-third-party-libraries\">12. Coordinate log levels with third-party libraries<\/h3>\n\n\n\n<p>In complex applications, it is common to use third-party libraries. When using third-party libraries, ensure that log levels with these libraries align with those used in your application. <\/p>\n\n\n\n<p>This prevents logs from these libraries from flooding the application logs with redundant information or noise. Failure to so coordinate leads to inconsistencies in debugging efforts and partial code coverage.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-use-middleware-for-real-time-error-tracking\">Use Middleware for real-time error tracking<\/h2>\n\n\n\n<p>By integrating Middleware into your application architectures for monitoring, you will gain real-time error tracking of your applications. <\/p>\n\n\n\n<p><a href=\"https:\/\/middleware.io\/\" target=\"_blank\" rel=\"noreferrer noopener\">Middleware<\/a> natively complies with all the Python logging best practices discussed earlier. Three key features set Middleware apart from other logging tools:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-real-time-error-alerting-and-analysis\">1. Real-time error alerting and analysis<\/h3>\n\n\n\n<p>Middleware provides real-time insight into application errors, allowing developers to respond to and fix issues that arise quickly. With Middleware, you can set up <a href=\"https:\/\/middleware.io\/platform\/alerts\/\">alerts to notify you<\/a> of critical errors and failures and diagnose issues in real-time to resolve them quickly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-telemetry-and-timeline-data\">2. Telemetry and timeline data<\/h3>\n\n\n\n<p>Middleware tracks and analyzes telemetry and timeline data across an application, giving developers a comprehensive view of how the application is performing. This includes CPU and memory usage data, network traffic, and application behavior, among other things.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-unified-timestamping\">3. Unified timestamping<\/h3>\n\n\n\n<p>Middleware provides unified timestamping across an application, helping developers to correlate log messages and telemetry data and gain deeper insights into the application&#8217;s behavior.&nbsp;<\/p>\n\n\n\n<p>With the unified timestamp, you can easily identify the root cause of issues and better understand how your applications are performing over time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-in-closing\">In closing&nbsp;<\/h2>\n\n\n\n<p>Implementing the best practices for Python logging can significantly improve an application&#8217;s diagnosability and debugging efficiency. <\/p>\n\n\n\n<p>However, it is important to note that these practices alone may not provide the full <a href=\"https:\/\/middleware.io\/blog\/observability\/\">observability<\/a> required to maintain and improve complex applications.&nbsp;<\/p>\n\n\n\n<p>In such cases, adding observability tools like Middleware can fill the gaps and provide a complete understanding of the system&#8217;s behavior, performance, and faults. <a href=\"https:\/\/app.middleware.io\/auth\/register\/\" target=\"_blank\" rel=\"noreferrer noopener\">Sign up now<\/a> to see it in action<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to use Python logging to track errors, debug applications, and troubleshoot issues. This article covers best practices for configuring Python logging, including how to choose the right logging levels, format your logs, and filter your logs.<\/p>\n","protected":false},"author":1,"featured_media":5235,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[695],"tags":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>12 Python Logging Best Practices To Debug Apps Faster<\/title>\n<meta name=\"description\" content=\"Discover more about Python Logging, Python Logging best practices, examples, and tips in this comprehensive guide.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"ja_JP\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"12 Python Logging Best Practices To Debug Apps Faster\" \/>\n<meta property=\"og:description\" content=\"Discover more about Python Logging, Python Logging best practices, examples, and tips in this comprehensive guide.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/stage-site.middleware.io\/ja\/python-logging-best-practices\/\" \/>\n<meta property=\"og:site_name\" content=\"Middleware\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-30T12:17:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-22T11:19:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/stage-site.middleware.io\/wp-content\/uploads\/2023\/05\/Python-logging-best-practices-to-debug-apps-faster-1-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1300\" \/>\n\t<meta property=\"og:image:height\" content=\"700\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"MW Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u57f7\u7b46\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"MW Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u63a8\u5b9a\u8aad\u307f\u53d6\u308a\u6642\u9593\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/stage-site.middleware.io\/ja\/python-logging-best-practices\/\",\"url\":\"https:\/\/stage-site.middleware.io\/ja\/python-logging-best-practices\/\",\"name\":\"12 Python Logging Best Practices To Debug Apps Faster\",\"isPartOf\":{\"@id\":\"https:\/\/stage-site.middleware.io\/ja\/#website\"},\"datePublished\":\"2023-05-30T12:17:44+00:00\",\"dateModified\":\"2023-06-22T11:19:53+00:00\",\"author\":{\"@id\":\"https:\/\/stage-site.middleware.io\/ja\/#\/schema\/person\/4ea1dce092ad60621e4fa874f02ccb92\"},\"description\":\"Discover more about Python Logging, Python Logging best practices, examples, and tips in this comprehensive guide.\",\"breadcrumb\":{\"@id\":\"https:\/\/stage-site.middleware.io\/ja\/python-logging-best-practices\/#breadcrumb\"},\"inLanguage\":\"ja\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/stage-site.middleware.io\/ja\/python-logging-best-practices\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/stage-site.middleware.io\/ja\/python-logging-best-practices\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/stage-site.middleware.io\/ja\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Best Practices\",\"item\":\"https:\/\/stage-site.middleware.io\/category\/best-practices\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"12 Python Logging Best Practices To Debug Apps Faster\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/stage-site.middleware.io\/ja\/#website\",\"url\":\"https:\/\/stage-site.middleware.io\/ja\/\",\"name\":\"Middleware\",\"description\":\"Observability platform\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/stage-site.middleware.io\/ja\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"ja\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/stage-site.middleware.io\/ja\/#\/schema\/person\/4ea1dce092ad60621e4fa874f02ccb92\",\"name\":\"MW Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ja\",\"@id\":\"https:\/\/stage-site.middleware.io\/ja\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/14ba28b020541bea7bc669595bdba058?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/14ba28b020541bea7bc669595bdba058?s=96&d=mm&r=g\",\"caption\":\"MW Team\"},\"sameAs\":[\"http:\/\/localhost\/melt\"],\"url\":\"https:\/\/stage-site.middleware.io\/ja\/author\/mittalmiddleware-io\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"12 Python Logging Best Practices To Debug Apps Faster","description":"Discover more about Python Logging, Python Logging best practices, examples, and tips in this comprehensive guide.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"ja_JP","og_type":"article","og_title":"12 Python Logging Best Practices To Debug Apps Faster","og_description":"Discover more about Python Logging, Python Logging best practices, examples, and tips in this comprehensive guide.","og_url":"https:\/\/stage-site.middleware.io\/ja\/python-logging-best-practices\/","og_site_name":"Middleware","article_published_time":"2023-05-30T12:17:44+00:00","article_modified_time":"2023-06-22T11:19:53+00:00","og_image":[{"width":1300,"height":700,"url":"https:\/\/stage-site.middleware.io\/wp-content\/uploads\/2023\/05\/Python-logging-best-practices-to-debug-apps-faster-1-1.png","type":"image\/png"}],"author":"MW Team","twitter_card":"summary_large_image","twitter_misc":{"\u57f7\u7b46\u8005":"MW Team","\u63a8\u5b9a\u8aad\u307f\u53d6\u308a\u6642\u9593":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/stage-site.middleware.io\/ja\/python-logging-best-practices\/","url":"https:\/\/stage-site.middleware.io\/ja\/python-logging-best-practices\/","name":"12 Python Logging Best Practices To Debug Apps Faster","isPartOf":{"@id":"https:\/\/stage-site.middleware.io\/ja\/#website"},"datePublished":"2023-05-30T12:17:44+00:00","dateModified":"2023-06-22T11:19:53+00:00","author":{"@id":"https:\/\/stage-site.middleware.io\/ja\/#\/schema\/person\/4ea1dce092ad60621e4fa874f02ccb92"},"description":"Discover more about Python Logging, Python Logging best practices, examples, and tips in this comprehensive guide.","breadcrumb":{"@id":"https:\/\/stage-site.middleware.io\/ja\/python-logging-best-practices\/#breadcrumb"},"inLanguage":"ja","potentialAction":[{"@type":"ReadAction","target":["https:\/\/stage-site.middleware.io\/ja\/python-logging-best-practices\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/stage-site.middleware.io\/ja\/python-logging-best-practices\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/stage-site.middleware.io\/ja\/"},{"@type":"ListItem","position":2,"name":"Best Practices","item":"https:\/\/stage-site.middleware.io\/category\/best-practices\/"},{"@type":"ListItem","position":3,"name":"12 Python Logging Best Practices To Debug Apps Faster"}]},{"@type":"WebSite","@id":"https:\/\/stage-site.middleware.io\/ja\/#website","url":"https:\/\/stage-site.middleware.io\/ja\/","name":"Middleware","description":"Observability platform","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/stage-site.middleware.io\/ja\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"ja"},{"@type":"Person","@id":"https:\/\/stage-site.middleware.io\/ja\/#\/schema\/person\/4ea1dce092ad60621e4fa874f02ccb92","name":"MW Team","image":{"@type":"ImageObject","inLanguage":"ja","@id":"https:\/\/stage-site.middleware.io\/ja\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/14ba28b020541bea7bc669595bdba058?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/14ba28b020541bea7bc669595bdba058?s=96&d=mm&r=g","caption":"MW Team"},"sameAs":["http:\/\/localhost\/melt"],"url":"https:\/\/stage-site.middleware.io\/ja\/author\/mittalmiddleware-io\/"}]}},"_links":{"self":[{"href":"https:\/\/stage-site.middleware.io\/ja\/wp-json\/wp\/v2\/posts\/5231"}],"collection":[{"href":"https:\/\/stage-site.middleware.io\/ja\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stage-site.middleware.io\/ja\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stage-site.middleware.io\/ja\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/stage-site.middleware.io\/ja\/wp-json\/wp\/v2\/comments?post=5231"}],"version-history":[{"count":9,"href":"https:\/\/stage-site.middleware.io\/ja\/wp-json\/wp\/v2\/posts\/5231\/revisions"}],"predecessor-version":[{"id":5377,"href":"https:\/\/stage-site.middleware.io\/ja\/wp-json\/wp\/v2\/posts\/5231\/revisions\/5377"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/stage-site.middleware.io\/ja\/wp-json\/wp\/v2\/media\/5235"}],"wp:attachment":[{"href":"https:\/\/stage-site.middleware.io\/ja\/wp-json\/wp\/v2\/media?parent=5231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stage-site.middleware.io\/ja\/wp-json\/wp\/v2\/categories?post=5231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stage-site.middleware.io\/ja\/wp-json\/wp\/v2\/tags?post=5231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}