|
| 1 | +# 🧩 13_Logging_and_Debugging |
| 2 | + |
| 3 | +In DevOps automation, **logging and debugging** are essential for identifying issues, tracking execution, and ensuring reliability in scripts and systems. |
| 4 | +Python provides a robust **logging module** and built-in debugging tools like **pdb** to help developers monitor and fix issues efficiently. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## 🔹 1. Python Logging Module |
| 9 | + |
| 10 | +### 📘 Definition: |
| 11 | + |
| 12 | +The **logging module** in Python is used to record messages that describe the flow of a program, including information, warnings, and errors. |
| 13 | + |
| 14 | +It allows: |
| 15 | + |
| 16 | +* Tracking **events and errors** in automation scripts |
| 17 | +* Writing logs to **console or file** |
| 18 | +* Setting different **log levels** for better filtering |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +### 🧩 Example 1: Basic Logging Example |
| 23 | + |
| 24 | +```python |
| 25 | +import logging |
| 26 | + |
| 27 | +# Configure basic logging |
| 28 | +logging.basicConfig(level=logging.INFO) |
| 29 | + |
| 30 | +logging.debug("This is a debug message") |
| 31 | +logging.info("Script started successfully") |
| 32 | +logging.warning("Low disk space warning") |
| 33 | +logging.error("File not found!") |
| 34 | +logging.critical("Server down! Immediate attention needed!") |
| 35 | +``` |
| 36 | + |
| 37 | +🟢 Output: |
| 38 | + |
| 39 | +``` |
| 40 | +INFO:root:Script started successfully |
| 41 | +WARNING:root:Low disk space warning |
| 42 | +ERROR:root:File not found! |
| 43 | +CRITICAL:root:Server down! Immediate attention needed! |
| 44 | +``` |
| 45 | + |
| 46 | +✅ *Use Case:* Log automation progress (e.g., file backup, deployment steps). |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +## 🔹 2. Logging Levels and Configuration |
| 51 | + |
| 52 | +### 📘 Definition: |
| 53 | + |
| 54 | +Logging levels help define the **severity** of log messages. |
| 55 | +Python provides the following levels (from lowest to highest severity): |
| 56 | + |
| 57 | +| Level | Description | |
| 58 | +| -------- | ----------------------------------------------------- | |
| 59 | +| DEBUG | Detailed information, useful for debugging | |
| 60 | +| INFO | General information about normal operations | |
| 61 | +| WARNING | Something unexpected happened, but program still runs | |
| 62 | +| ERROR | Serious issue; some function failed | |
| 63 | +| CRITICAL | Very serious error; may cause system failure | |
| 64 | + |
| 65 | +--- |
| 66 | + |
| 67 | +### 🧩 Example 2: Custom Logging Configuration |
| 68 | + |
| 69 | +```python |
| 70 | +import logging |
| 71 | + |
| 72 | +logging.basicConfig( |
| 73 | + level=logging.DEBUG, |
| 74 | + format='%(asctime)s - %(levelname)s - %(message)s', |
| 75 | + datefmt='%Y-%m-%d %H:%M:%S' |
| 76 | +) |
| 77 | + |
| 78 | +logging.debug("Debugging the script execution") |
| 79 | +logging.info("Deployment script initiated") |
| 80 | +logging.warning("Memory usage high") |
| 81 | +logging.error("Build failed due to missing dependency") |
| 82 | +logging.critical("Critical failure in pipeline execution") |
| 83 | +``` |
| 84 | + |
| 85 | +🟢 Output (formatted): |
| 86 | + |
| 87 | +``` |
| 88 | +2025-11-02 22:00:01 - DEBUG - Debugging the script execution |
| 89 | +2025-11-02 22:00:02 - INFO - Deployment script initiated |
| 90 | +2025-11-02 22:00:03 - WARNING - Memory usage high |
| 91 | +2025-11-02 22:00:04 - ERROR - Build failed due to missing dependency |
| 92 | +2025-11-02 22:00:05 - CRITICAL - Critical failure in pipeline execution |
| 93 | +``` |
| 94 | + |
| 95 | +✅ *Use Case:* Helps in Jenkins or CI/CD jobs for detailed run logs. |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +## 🔹 3. Writing Logs to Files |
| 100 | + |
| 101 | +### 📘 Definition: |
| 102 | + |
| 103 | +You can store logs in files for **long-term tracking, analysis, or debugging**. |
| 104 | +This is useful for DevOps pipelines, cron jobs, and background services. |
| 105 | + |
| 106 | +--- |
| 107 | + |
| 108 | +### 🧩 Example 3: Writing Logs to File |
| 109 | + |
| 110 | +```python |
| 111 | +import logging |
| 112 | + |
| 113 | +logging.basicConfig( |
| 114 | + filename='automation.log', |
| 115 | + level=logging.INFO, |
| 116 | + format='%(asctime)s - %(levelname)s - %(message)s' |
| 117 | +) |
| 118 | + |
| 119 | +logging.info("Script started") |
| 120 | +logging.warning("API response delayed") |
| 121 | +logging.error("Failed to connect to database") |
| 122 | +``` |
| 123 | + |
| 124 | +🟢 File: `automation.log` |
| 125 | + |
| 126 | +``` |
| 127 | +2025-11-02 22:05:01 - INFO - Script started |
| 128 | +2025-11-02 22:05:02 - WARNING - API response delayed |
| 129 | +2025-11-02 22:05:03 - ERROR - Failed to connect to database |
| 130 | +``` |
| 131 | + |
| 132 | +✅ *Use Case:* Maintain automation or monitoring logs in `/var/log/devops_scripts.log`. |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## 🔹 4. Debugging with **pdb** (Python Debugger) |
| 137 | + |
| 138 | +### 📘 Definition: |
| 139 | + |
| 140 | +`pdb` is the **Python Debugger**, a built-in tool that helps you **pause code execution**, inspect variables, and step through lines interactively. |
| 141 | + |
| 142 | +It is invaluable when troubleshooting complex automation logic. |
| 143 | + |
| 144 | +--- |
| 145 | + |
| 146 | +### 🧩 Example 4: Using pdb for Debugging |
| 147 | + |
| 148 | +```python |
| 149 | +import pdb |
| 150 | + |
| 151 | +def divide(a, b): |
| 152 | + pdb.set_trace() # Pauses execution here |
| 153 | + return a / b |
| 154 | + |
| 155 | +result = divide(10, 0) |
| 156 | +print(result) |
| 157 | +``` |
| 158 | + |
| 159 | +When you run this, Python enters **debug mode** in the terminal: |
| 160 | + |
| 161 | +``` |
| 162 | +(Pdb) p a |
| 163 | +10 |
| 164 | +(Pdb) p b |
| 165 | +0 |
| 166 | +(Pdb) c |
| 167 | +ZeroDivisionError: division by zero |
| 168 | +``` |
| 169 | + |
| 170 | +✅ *Common pdb Commands:* |
| 171 | + |
| 172 | +| Command | Description | |
| 173 | +| --------- | -------------------- | |
| 174 | +| `n` | Next line | |
| 175 | +| `s` | Step into function | |
| 176 | +| `c` | Continue execution | |
| 177 | +| `p <var>` | Print variable value | |
| 178 | +| `q` | Quit debugger | |
| 179 | + |
| 180 | +✅ *Use Case:* Debug automation errors like failed file transfers or API calls. |
| 181 | + |
| 182 | +--- |
| 183 | + |
| 184 | +## 🔹 5. Debugging with IDE Debuggers |
| 185 | + |
| 186 | +### 📘 Definition: |
| 187 | + |
| 188 | +Modern IDEs (like **VS Code**, **PyCharm**, or **IDLE**) come with **graphical debuggers** that allow: |
| 189 | + |
| 190 | +* Setting **breakpoints** |
| 191 | +* Viewing **variable values in real-time** |
| 192 | +* Stepping through code visually |
| 193 | + |
| 194 | +✅ *Use Case:* Debug Jenkins scripts or AWS automation functions before deployment. |
| 195 | + |
| 196 | +--- |
| 197 | + |
| 198 | +## 🧾 Summary |
| 199 | + |
| 200 | +| Concept | Description | DevOps Use Case | |
| 201 | +| -------------- | ----------------------------------- | ------------------------------ | |
| 202 | +| Logging module | Built-in library for event tracking | Monitor automation scripts | |
| 203 | +| Log levels | Define severity | Filter logs in CI/CD | |
| 204 | +| File logging | Write logs to files | Persist job logs | |
| 205 | +| pdb | Command-line debugger | Debug runtime errors | |
| 206 | +| IDE Debugger | Visual debugging | Test Python automation locally | |
| 207 | + |
| 208 | +--- |
| 209 | + |
| 210 | +## 💡 DevOps Real-Life Use Cases |
| 211 | + |
| 212 | +* Record execution of **infrastructure provisioning scripts**. |
| 213 | +* Debug failed **AWS automation (boto3)** or **API integrations**. |
| 214 | +* Maintain **deployment logs** for auditing and compliance. |
| 215 | +* Detect and resolve **cron job or CI/CD failures**. |
| 216 | + |
| 217 | +--- |
0 commit comments