|
| 1 | +# Logging System Architecture |
| 2 | + |
| 3 | +## Flow Diagram |
| 4 | + |
| 5 | +``` |
| 6 | +┌─────────────────────────────────────────────────────────────┐ |
| 7 | +│ User runs script.py │ |
| 8 | +│ python scripts/script.py --dir ./files │ |
| 9 | +└────────────────────────┬────────────────────────────────────┘ |
| 10 | + │ |
| 11 | + ▼ |
| 12 | + ┌───────────────────────────────┐ |
| 13 | + │ Check --no-logs flag? │ |
| 14 | + └───────┬───────────────┬───────┘ |
| 15 | + │ │ |
| 16 | + No │ │ Yes |
| 17 | + │ │ |
| 18 | + ▼ ▼ |
| 19 | + ┌────────────────────┐ ┌──────────────────┐ |
| 20 | + │ setup_logger() │ │ Skip logging │ |
| 21 | + │ Redirect stdout │ │ Run normally │ |
| 22 | + └─────────┬──────────┘ └──────────────────┘ |
| 23 | + │ |
| 24 | + ▼ |
| 25 | + ┌─────────────────────────────────────┐ |
| 26 | + │ All print() calls captured to: │ |
| 27 | + │ 1. Terminal (user sees output) │ |
| 28 | + │ 2. Log buffer (stored in memory) │ |
| 29 | + └─────────────────┬───────────────────┘ |
| 30 | + │ |
| 31 | + ▼ |
| 32 | + ┌─────────────────────────────────────┐ |
| 33 | + │ Script execution completes │ |
| 34 | + │ (success or error) │ |
| 35 | + └─────────────────┬───────────────────┘ |
| 36 | + │ |
| 37 | + ▼ |
| 38 | + ┌────────────────────────────────┐ |
| 39 | + │ Check --auto-save-logs flag? │ |
| 40 | + └────────┬──────────────┬────────┘ |
| 41 | + │ │ |
| 42 | + No │ │ Yes |
| 43 | + │ │ |
| 44 | + ▼ ▼ |
| 45 | + ┌──────────────────────┐ ┌─────────────────────┐ |
| 46 | + │ Prompt user: │ │ Auto-save with │ |
| 47 | + │ "Save logs? (y/n)" │ │ timestamp │ |
| 48 | + └──────┬───────────────┘ └──────────┬──────────┘ |
| 49 | + │ │ |
| 50 | + ▼ │ |
| 51 | + ┌──────────────┐ │ |
| 52 | + │ User says │ │ |
| 53 | + │ 'y' or 'n'? │ │ |
| 54 | + └──┬───────┬───┘ │ |
| 55 | + │ │ │ |
| 56 | + y │ │ n │ |
| 57 | + │ │ │ |
| 58 | + ▼ ▼ │ |
| 59 | + ┌────┐ ┌────────┐ │ |
| 60 | + │Save│ │Discard │ │ |
| 61 | + └─┬──┘ └────────┘ │ |
| 62 | + │ │ |
| 63 | + ▼ │ |
| 64 | + ┌──────────────────────┐ │ |
| 65 | + │ Prompt for filename │ │ |
| 66 | + │ (or use timestamp) │ │ |
| 67 | + └──────────┬───────────┘ │ |
| 68 | + │ │ |
| 69 | + ▼ ▼ |
| 70 | + ┌──────────────────────────────────────────┐ |
| 71 | + │ Save to logs/run_YYYYMMDD_HHMMSS.log │ |
| 72 | + │ or logs/custom_name.log │ |
| 73 | + └──────────────────────────────────────────┘ |
| 74 | +``` |
| 75 | + |
| 76 | +## Component Interaction |
| 77 | + |
| 78 | +``` |
| 79 | +┌─────────────────────────────────────────────────────────────┐ |
| 80 | +│ script.py │ |
| 81 | +│ ┌────────────────────────────────────────────────────┐ │ |
| 82 | +│ │ Main Execution │ │ |
| 83 | +│ │ • Parse arguments │ │ |
| 84 | +│ │ • Setup logger (if enabled) │ │ |
| 85 | +│ │ • Run file processing │ │ |
| 86 | +│ │ • Handle log saving (finally block) │ │ |
| 87 | +│ └────────────────────────────────────────────────────┘ │ |
| 88 | +└───────────────────────┬─────────────────────────────────────┘ |
| 89 | + │ imports |
| 90 | + ▼ |
| 91 | +┌─────────────────────────────────────────────────────────────┐ |
| 92 | +│ logger.py │ |
| 93 | +│ ┌────────────────────────────────────────────────────┐ │ |
| 94 | +│ │ Logger Class │ │ |
| 95 | +│ │ • write(message) → terminal + buffer │ │ |
| 96 | +│ │ • get_logs() → return all buffered logs │ │ |
| 97 | +│ │ • save_logs(filename) → write to file │ │ |
| 98 | +│ │ • Thread-safe with locks │ │ |
| 99 | +│ └────────────────────────────────────────────────────┘ │ |
| 100 | +│ │ |
| 101 | +│ ┌────────────────────────────────────────────────────┐ │ |
| 102 | +│ │ Helper Functions │ │ |
| 103 | +│ │ • setup_logger() → initialize & redirect stdout │ │ |
| 104 | +│ │ • get_logger() → get global instance │ │ |
| 105 | +│ │ • restore_stdout() → restore original stdout │ │ |
| 106 | +│ └────────────────────────────────────────────────────┘ │ |
| 107 | +└─────────────────────────────────────────────────────────────┘ |
| 108 | + │ |
| 109 | + ▼ |
| 110 | +┌─────────────────────────────────────────────────────────────┐ |
| 111 | +│ File System │ |
| 112 | +│ FileSense/ │ |
| 113 | +│ └── logs/ │ |
| 114 | +│ ├── run_20251205_143022.log │ |
| 115 | +│ ├── run_20251205_145930.log │ |
| 116 | +│ └── custom_name.log │ |
| 117 | +└─────────────────────────────────────────────────────────────┘ |
| 118 | +``` |
| 119 | + |
| 120 | +## Thread Safety |
| 121 | + |
| 122 | +``` |
| 123 | +┌─────────────────────────────────────────────────────────────┐ |
| 124 | +│ Multi-threaded Processing │ |
| 125 | +│ │ |
| 126 | +│ Thread 1: process_file("doc1.pdf") │ |
| 127 | +│ │ │ |
| 128 | +│ └──► print("Processing doc1...") ──┐ │ |
| 129 | +│ │ │ |
| 130 | +│ Thread 2: process_file("doc2.pdf") │ │ |
| 131 | +│ │ │ │ |
| 132 | +│ └──► print("Processing doc2...") ──┤ │ |
| 133 | +│ │ │ |
| 134 | +│ Thread 3: process_file("doc3.pdf") │ │ |
| 135 | +│ │ │ │ |
| 136 | +│ └──► print("Processing doc3...") ──┤ │ |
| 137 | +│ │ │ |
| 138 | +│ ▼ │ |
| 139 | +│ ┌──────────────────┐ │ |
| 140 | +│ │ Logger.write() │ │ |
| 141 | +│ │ with lock: │ │ |
| 142 | +│ │ buffer.append │ │ |
| 143 | +│ └──────────────────┘ │ |
| 144 | +│ │ │ |
| 145 | +│ ▼ │ |
| 146 | +│ ┌──────────────────┐ │ |
| 147 | +│ │ Synchronized │ │ |
| 148 | +│ │ Log Buffer │ │ |
| 149 | +│ └──────────────────┘ │ |
| 150 | +└─────────────────────────────────────────────────────────────┘ |
| 151 | +``` |
| 152 | + |
| 153 | +## Data Flow |
| 154 | + |
| 155 | +``` |
| 156 | +User Input (print statement) |
| 157 | + │ |
| 158 | + ▼ |
| 159 | +sys.stdout.write() → Logger.write() |
| 160 | + │ │ |
| 161 | + │ ├──► Terminal (immediate display) |
| 162 | + │ │ |
| 163 | + │ └──► Log Buffer (with lock) |
| 164 | + │ │ |
| 165 | + │ │ (stored in memory) |
| 166 | + │ │ |
| 167 | + ▼ ▼ |
| 168 | +Script completes User prompted |
| 169 | + │ │ |
| 170 | + │ ▼ |
| 171 | + │ Save to file? |
| 172 | + │ │ |
| 173 | + │ ▼ |
| 174 | + └──────────────────────► logs/filename.log |
| 175 | +``` |
0 commit comments