askrobots / dbbasic-logs
Public
Structured logging to TSV files with JSON context: grep-able, compressible and searchable.
test_logging.py · 16.0 KB · 539 lines
| 1 | """ |
| 2 | Tests for dbbasic-logs core logging functionality |
| 3 | """ |
| 4 | |
| 5 | import os |
| 6 | import time |
| 7 | import json |
| 8 | import tempfile |
| 9 | import shutil |
| 10 | from datetime import datetime, timedelta |
| 11 | import pytest |
| 12 | |
| 13 | from dbbasic_logs import DBBasicLogger |
| 14 | |
| 15 | |
| 16 | class TestBasicLogging: |
| 17 | """Test basic logging operations""" |
| 18 | |
| 19 | @pytest.fixture |
| 20 | def logger(self, tmp_path): |
| 21 | """Create a logger with temporary directory""" |
| 22 | log_dir = str(tmp_path / "logs") |
| 23 | return DBBasicLogger(log_dir=log_dir) |
| 24 | |
| 25 | def test_directory_creation(self, logger): |
| 26 | """Test that log directories are created""" |
| 27 | assert os.path.exists(f'{logger.log_dir}/app') |
| 28 | assert os.path.exists(f'{logger.log_dir}/errors') |
| 29 | assert os.path.exists(f'{logger.log_dir}/access') |
| 30 | |
| 31 | def test_info_logging(self, logger): |
| 32 | """Test info level logging""" |
| 33 | logger.info("Test message", user_id=42) |
| 34 | |
| 35 | today = time.strftime('%Y-%m-%d') |
| 36 | log_file = f'{logger.log_dir}/app/{today}.tsv' |
| 37 | |
| 38 | assert os.path.exists(log_file) |
| 39 | |
| 40 | with open(log_file) as f: |
| 41 | line = f.read().strip() |
| 42 | |
| 43 | parts = line.split('\t') |
| 44 | assert len(parts) == 4 |
| 45 | assert int(parts[0]) > 0 # timestamp |
| 46 | assert parts[1] == 'INFO' |
| 47 | assert parts[2] == 'Test message' |
| 48 | |
| 49 | context = json.loads(parts[3]) |
| 50 | assert context['user_id'] == 42 |
| 51 | |
| 52 | def test_warning_logging(self, logger): |
| 53 | """Test warning level logging""" |
| 54 | logger.warning("Rate limit approaching", count=95) |
| 55 | |
| 56 | today = time.strftime('%Y-%m-%d') |
| 57 | log_file = f'{logger.log_dir}/app/{today}.tsv' |
| 58 | |
| 59 | with open(log_file) as f: |
| 60 | line = f.read().strip() |
| 61 | |
| 62 | parts = line.split('\t') |
| 63 | assert parts[1] == 'WARNING' |
| 64 | assert parts[2] == 'Rate limit approaching' |
| 65 | |
| 66 | def test_error_logging(self, logger): |
| 67 | """Test error level logging""" |
| 68 | logger.error("Database connection failed", error="Timeout") |
| 69 | |
| 70 | today = time.strftime('%Y-%m-%d') |
| 71 | log_file = f'{logger.log_dir}/app/{today}.tsv' |
| 72 | |
| 73 | with open(log_file) as f: |
| 74 | line = f.read().strip() |
| 75 | |
| 76 | parts = line.split('\t') |
| 77 | assert parts[1] == 'ERROR' |
| 78 | assert parts[2] == 'Database connection failed' |
| 79 | |
| 80 | context = json.loads(parts[3]) |
| 81 | assert context['error'] == 'Timeout' |
| 82 | |
| 83 | def test_debug_logging(self, logger): |
| 84 | """Test debug level logging""" |
| 85 | logger.debug("Cache miss", key="user:42") |
| 86 | |
| 87 | today = time.strftime('%Y-%m-%d') |
| 88 | log_file = f'{logger.log_dir}/app/{today}.tsv' |
| 89 | |
| 90 | with open(log_file) as f: |
| 91 | line = f.read().strip() |
| 92 | |
| 93 | parts = line.split('\t') |
| 94 | assert parts[1] == 'DEBUG' |
| 95 | |
| 96 | def test_multiple_context_fields(self, logger): |
| 97 | """Test logging with multiple context fields""" |
| 98 | logger.info( |
| 99 | "User action", |
| 100 | user_id=42, |
| 101 | action="update_profile", |
| 102 | ip="192.168.1.1", |
| 103 | duration=0.123 |
| 104 | ) |
| 105 | |
| 106 | today = time.strftime('%Y-%m-%d') |
| 107 | log_file = f'{logger.log_dir}/app/{today}.tsv' |
| 108 | |
| 109 | with open(log_file) as f: |
| 110 | line = f.read().strip() |
| 111 | |
| 112 | parts = line.split('\t') |
| 113 | context = json.loads(parts[3]) |
| 114 | |
| 115 | assert context['user_id'] == 42 |
| 116 | assert context['action'] == 'update_profile' |
| 117 | assert context['ip'] == '192.168.1.1' |
| 118 | assert context['duration'] == 0.123 |
| 119 | |
| 120 | def test_empty_context(self, logger): |
| 121 | """Test logging without context""" |
| 122 | logger.info("Simple message") |
| 123 | |
| 124 | today = time.strftime('%Y-%m-%d') |
| 125 | log_file = f'{logger.log_dir}/app/{today}.tsv' |
| 126 | |
| 127 | with open(log_file) as f: |
| 128 | line = f.read().strip() |
| 129 | |
| 130 | parts = line.split('\t') |
| 131 | assert parts[3] == '{}' |
| 132 | |
| 133 | |
| 134 | class TestExceptionLogging: |
| 135 | """Test exception and stack trace logging""" |
| 136 | |
| 137 | @pytest.fixture |
| 138 | def logger(self, tmp_path): |
| 139 | """Create a logger with temporary directory""" |
| 140 | log_dir = str(tmp_path / "logs") |
| 141 | return DBBasicLogger(log_dir=log_dir) |
| 142 | |
| 143 | def test_exception_logging(self, logger): |
| 144 | """Test exception logging with stack trace""" |
| 145 | try: |
| 146 | result = 1 / 0 |
| 147 | except ZeroDivisionError: |
| 148 | logger.exception("Division error", operation="calculate") |
| 149 | |
| 150 | today = time.strftime('%Y-%m-%d') |
| 151 | log_file = f'{logger.log_dir}/errors/{today}.tsv' |
| 152 | |
| 153 | assert os.path.exists(log_file) |
| 154 | |
| 155 | with open(log_file) as f: |
| 156 | line = f.read().strip() |
| 157 | |
| 158 | parts = line.split('\t') |
| 159 | assert parts[1] == 'ERROR' |
| 160 | assert parts[2] == 'Division error' |
| 161 | |
| 162 | context = json.loads(parts[3]) |
| 163 | assert context['operation'] == 'calculate' |
| 164 | assert 'trace' in context |
| 165 | assert 'ZeroDivisionError' in context['trace'] |
| 166 | assert 'Traceback' in context['trace'] |
| 167 | |
| 168 | def test_exception_with_nested_calls(self, logger): |
| 169 | """Test exception logging from nested function calls""" |
| 170 | def inner_function(): |
| 171 | raise ValueError("Invalid value") |
| 172 | |
| 173 | def outer_function(): |
| 174 | inner_function() |
| 175 | |
| 176 | try: |
| 177 | outer_function() |
| 178 | except ValueError: |
| 179 | logger.exception("Nested exception", context="testing") |
| 180 | |
| 181 | today = time.strftime('%Y-%m-%d') |
| 182 | log_file = f'{logger.log_dir}/errors/{today}.tsv' |
| 183 | |
| 184 | with open(log_file) as f: |
| 185 | line = f.read().strip() |
| 186 | |
| 187 | parts = line.split('\t') |
| 188 | context = json.loads(parts[3]) |
| 189 | |
| 190 | # Check that stack trace includes nested calls |
| 191 | assert 'inner_function' in context['trace'] |
| 192 | assert 'outer_function' in context['trace'] |
| 193 | assert 'ValueError' in context['trace'] |
| 194 | |
| 195 | |
| 196 | class TestAccessLogging: |
| 197 | """Test HTTP access logging""" |
| 198 | |
| 199 | @pytest.fixture |
| 200 | def logger(self, tmp_path): |
| 201 | """Create a logger with temporary directory""" |
| 202 | log_dir = str(tmp_path / "logs") |
| 203 | return DBBasicLogger(log_dir=log_dir) |
| 204 | |
| 205 | def test_access_logging(self, logger): |
| 206 | """Test access log entry""" |
| 207 | logger.access( |
| 208 | method="GET", |
| 209 | path="/api/users", |
| 210 | status=200, |
| 211 | duration=0.05, |
| 212 | ip="192.168.1.1" |
| 213 | ) |
| 214 | |
| 215 | today = time.strftime('%Y-%m-%d') |
| 216 | log_file = f'{logger.log_dir}/access/{today}.tsv' |
| 217 | |
| 218 | assert os.path.exists(log_file) |
| 219 | |
| 220 | with open(log_file) as f: |
| 221 | line = f.read().strip() |
| 222 | |
| 223 | parts = line.split('\t') |
| 224 | assert parts[1] == 'INFO' |
| 225 | assert parts[2] == 'GET /api/users 200' |
| 226 | |
| 227 | context = json.loads(parts[3]) |
| 228 | assert context['duration'] == 0.05 |
| 229 | assert context['ip'] == '192.168.1.1' |
| 230 | |
| 231 | def test_access_logging_post_request(self, logger): |
| 232 | """Test POST request access logging""" |
| 233 | logger.access( |
| 234 | method="POST", |
| 235 | path="/login", |
| 236 | status=401, |
| 237 | duration=0.02, |
| 238 | ip="192.168.1.2", |
| 239 | user_agent="Mozilla/5.0" |
| 240 | ) |
| 241 | |
| 242 | today = time.strftime('%Y-%m-%d') |
| 243 | log_file = f'{logger.log_dir}/access/{today}.tsv' |
| 244 | |
| 245 | with open(log_file) as f: |
| 246 | line = f.read().strip() |
| 247 | |
| 248 | parts = line.split('\t') |
| 249 | assert parts[2] == 'POST /login 401' |
| 250 | |
| 251 | context = json.loads(parts[3]) |
| 252 | assert context['user_agent'] == 'Mozilla/5.0' |
| 253 | |
| 254 | |
| 255 | class TestLogSearching: |
| 256 | """Test log search functionality""" |
| 257 | |
| 258 | @pytest.fixture |
| 259 | def logger(self, tmp_path): |
| 260 | """Create a logger with temporary directory""" |
| 261 | log_dir = str(tmp_path / "logs") |
| 262 | return DBBasicLogger(log_dir=log_dir) |
| 263 | |
| 264 | def test_search_single_log(self, logger): |
| 265 | """Test searching within a single log file""" |
| 266 | logger.info("User logged in", user_id=42) |
| 267 | logger.info("User updated profile", user_id=42) |
| 268 | logger.info("User logged out", user_id=42) |
| 269 | |
| 270 | results = logger.search("logged in", log_type='app', days=1) |
| 271 | |
| 272 | assert len(results) == 1 |
| 273 | assert results[0]['message'] == 'User logged in' |
| 274 | assert results[0]['level'] == 'INFO' |
| 275 | assert results[0]['context']['user_id'] == 42 |
| 276 | |
| 277 | def test_search_multiple_matches(self, logger): |
| 278 | """Test searching with multiple matches""" |
| 279 | logger.info("User logged in", user_id=42) |
| 280 | logger.info("User logged in", user_id=43) |
| 281 | logger.info("User logged in", user_id=44) |
| 282 | |
| 283 | results = logger.search("User logged in", log_type='app', days=1) |
| 284 | |
| 285 | assert len(results) == 3 |
| 286 | |
| 287 | def test_search_with_regex(self, logger): |
| 288 | """Test searching with regex pattern""" |
| 289 | logger.info("Request completed", duration=0.1) |
| 290 | logger.info("Request completed", duration=1.5) |
| 291 | logger.info("Request completed", duration=0.05) |
| 292 | |
| 293 | # Search for duration greater than 1 second |
| 294 | results = logger.search(r'duration.*[1-9]\.', log_type='app', days=1) |
| 295 | |
| 296 | assert len(results) == 1 |
| 297 | assert results[0]['context']['duration'] == 1.5 |
| 298 | |
| 299 | def test_search_errors(self, logger): |
| 300 | """Test searching error logs""" |
| 301 | logger.error("Payment failed", order_id=123) |
| 302 | logger.error("Database timeout", query="SELECT") |
| 303 | |
| 304 | results = logger.search("ERROR", log_type='app', days=1) |
| 305 | |
| 306 | assert len(results) == 2 |
| 307 | assert all(r['level'] == 'ERROR' for r in results) |
| 308 | |
| 309 | def test_search_all_log_types(self, logger): |
| 310 | """Test searching across all log types""" |
| 311 | logger.info("App log", user_id=42) |
| 312 | logger.access("GET", "/api", 200, 0.1) |
| 313 | |
| 314 | try: |
| 315 | raise Exception("Test error") |
| 316 | except: |
| 317 | logger.exception("Error log") |
| 318 | |
| 319 | results = logger.search(".*", log_type='all', days=1) |
| 320 | |
| 321 | # Should find entries from app, access, and errors |
| 322 | log_types = set(r['log_type'] for r in results) |
| 323 | assert 'app' in log_types |
| 324 | assert 'access' in log_types |
| 325 | assert 'errors' in log_types |
| 326 | |
| 327 | def test_search_no_matches(self, logger): |
| 328 | """Test search with no matching results""" |
| 329 | logger.info("Test message") |
| 330 | |
| 331 | results = logger.search("nonexistent", log_type='app', days=1) |
| 332 | |
| 333 | assert len(results) == 0 |
| 334 | |
| 335 | def test_search_missing_log_file(self, logger): |
| 336 | """Test search when log file doesn't exist""" |
| 337 | # Don't create any logs |
| 338 | results = logger.search("anything", log_type='app', days=1) |
| 339 | |
| 340 | assert len(results) == 0 |
| 341 | |
| 342 | |
| 343 | class TestLogTailing: |
| 344 | """Test log tailing functionality""" |
| 345 | |
| 346 | @pytest.fixture |
| 347 | def logger(self, tmp_path): |
| 348 | """Create a logger with temporary directory""" |
| 349 | log_dir = str(tmp_path / "logs") |
| 350 | return DBBasicLogger(log_dir=log_dir) |
| 351 | |
| 352 | def test_tail_recent_logs(self, logger): |
| 353 | """Test getting recent log entries""" |
| 354 | for i in range(10): |
| 355 | logger.info(f"Message {i}", count=i) |
| 356 | |
| 357 | results = logger.tail('app', lines=5) |
| 358 | |
| 359 | assert len(results) == 5 |
| 360 | assert results[0]['message'] == 'Message 5' |
| 361 | assert results[-1]['message'] == 'Message 9' |
| 362 | |
| 363 | def test_tail_all_logs(self, logger): |
| 364 | """Test tailing when fewer logs than requested""" |
| 365 | logger.info("Message 1") |
| 366 | logger.info("Message 2") |
| 367 | |
| 368 | results = logger.tail('app', lines=100) |
| 369 | |
| 370 | assert len(results) == 2 |
| 371 | |
| 372 | def test_tail_errors(self, logger): |
| 373 | """Test tailing error logs""" |
| 374 | try: |
| 375 | raise ValueError("Test error 1") |
| 376 | except: |
| 377 | logger.exception("Error 1") |
| 378 | |
| 379 | try: |
| 380 | raise ValueError("Test error 2") |
| 381 | except: |
| 382 | logger.exception("Error 2") |
| 383 | |
| 384 | results = logger.tail('errors', lines=10) |
| 385 | |
| 386 | assert len(results) == 2 |
| 387 | assert all(r['level'] == 'ERROR' for r in results) |
| 388 | |
| 389 | def test_tail_empty_log(self, logger): |
| 390 | """Test tailing when log file doesn't exist""" |
| 391 | results = logger.tail('app', lines=100) |
| 392 | |
| 393 | assert len(results) == 0 |
| 394 | |
| 395 | def test_tail_access_logs(self, logger): |
| 396 | """Test tailing access logs""" |
| 397 | logger.access("GET", "/api/users", 200, 0.1) |
| 398 | logger.access("POST", "/api/users", 201, 0.2) |
| 399 | |
| 400 | results = logger.tail('access', lines=10) |
| 401 | |
| 402 | assert len(results) == 2 |
| 403 | |
| 404 | |
| 405 | class TestLogCompression: |
| 406 | """Test log compression scenarios""" |
| 407 | |
| 408 | @pytest.fixture |
| 409 | def logger(self, tmp_path): |
| 410 | """Create a logger with temporary directory""" |
| 411 | log_dir = str(tmp_path / "logs") |
| 412 | return DBBasicLogger(log_dir=log_dir) |
| 413 | |
| 414 | def test_search_compressed_logs(self, logger): |
| 415 | """Test searching compressed log files""" |
| 416 | # Create a log file and compress it |
| 417 | yesterday = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d') |
| 418 | log_file = f'{logger.log_dir}/app/{yesterday}.tsv' |
| 419 | |
| 420 | # Write some logs |
| 421 | timestamp = int(time.time()) |
| 422 | with open(log_file, 'w') as f: |
| 423 | f.write(f'{timestamp}\tINFO\tOld message\t{{"user_id":42}}\n') |
| 424 | |
| 425 | # Compress it |
| 426 | import subprocess |
| 427 | subprocess.run(['gzip', log_file], check=True) |
| 428 | |
| 429 | # Search should find it in compressed file |
| 430 | results = logger.search("Old message", log_type='app', days=2) |
| 431 | |
| 432 | assert len(results) == 1 |
| 433 | assert results[0]['message'] == 'Old message' |
| 434 | |
| 435 | |
| 436 | class TestEdgeCases: |
| 437 | """Test edge cases and error handling""" |
| 438 | |
| 439 | @pytest.fixture |
| 440 | def logger(self, tmp_path): |
| 441 | """Create a logger with temporary directory""" |
| 442 | log_dir = str(tmp_path / "logs") |
| 443 | return DBBasicLogger(log_dir=log_dir) |
| 444 | |
| 445 | def test_special_characters_in_message(self, logger): |
| 446 | """Test logging messages with special characters""" |
| 447 | logger.info("Message with special chars", data="test") |
| 448 | |
| 449 | today = time.strftime('%Y-%m-%d') |
| 450 | log_file = f'{logger.log_dir}/app/{today}.tsv' |
| 451 | |
| 452 | with open(log_file) as f: |
| 453 | line = f.read().strip() |
| 454 | |
| 455 | # Should still parse correctly |
| 456 | parts = line.split('\t') |
| 457 | assert len(parts) == 4 |
| 458 | assert parts[2] == "Message with special chars" |
| 459 | |
| 460 | def test_unicode_in_context(self, logger): |
| 461 | """Test logging with unicode characters""" |
| 462 | logger.info("Unicode test", name="José", emoji="🎉") |
| 463 | |
| 464 | today = time.strftime('%Y-%m-%d') |
| 465 | log_file = f'{logger.log_dir}/app/{today}.tsv' |
| 466 | |
| 467 | with open(log_file, encoding='utf-8') as f: |
| 468 | line = f.read().strip() |
| 469 | |
| 470 | parts = line.split('\t') |
| 471 | context = json.loads(parts[3]) |
| 472 | |
| 473 | assert context['name'] == 'José' |
| 474 | assert context['emoji'] == '🎉' |
| 475 | |
| 476 | def test_large_context(self, logger): |
| 477 | """Test logging with large context data""" |
| 478 | large_data = {"data": "x" * 10000} |
| 479 | logger.info("Large context", **large_data) |
| 480 | |
| 481 | today = time.strftime('%Y-%m-%d') |
| 482 | log_file = f'{logger.log_dir}/app/{today}.tsv' |
| 483 | |
| 484 | assert os.path.exists(log_file) |
| 485 | |
| 486 | with open(log_file) as f: |
| 487 | line = f.read().strip() |
| 488 | |
| 489 | parts = line.split('\t') |
| 490 | context = json.loads(parts[3]) |
| 491 | |
| 492 | assert len(context['data']) == 10000 |
| 493 | |
| 494 | def test_nested_context_objects(self, logger): |
| 495 | """Test logging with nested context objects""" |
| 496 | logger.info( |
| 497 | "Nested context", |
| 498 | user={"id": 42, "name": "John"}, |
| 499 | metadata={"tags": ["a", "b", "c"]} |
| 500 | ) |
| 501 | |
| 502 | today = time.strftime('%Y-%m-%d') |
| 503 | log_file = f'{logger.log_dir}/app/{today}.tsv' |
| 504 | |
| 505 | with open(log_file) as f: |
| 506 | line = f.read().strip() |
| 507 | |
| 508 | parts = line.split('\t') |
| 509 | context = json.loads(parts[3]) |
| 510 | |
| 511 | assert context['user']['id'] == 42 |
| 512 | assert context['user']['name'] == 'John' |
| 513 | assert context['metadata']['tags'] == ['a', 'b', 'c'] |
| 514 | |
| 515 | |
| 516 | class TestGlobalLoggerInstance: |
| 517 | """Test the global logger instance""" |
| 518 | |
| 519 | def test_global_log_import(self): |
| 520 | """Test that global log instance can be imported""" |
| 521 | from dbbasic_logs import log |
| 522 | |
| 523 | assert log is not None |
| 524 | assert isinstance(log, DBBasicLogger) |
| 525 | |
| 526 | def test_global_log_usage(self, tmp_path): |
| 527 | """Test using the global log instance""" |
| 528 | from dbbasic_logs import log |
| 529 | |
| 530 | # Override log directory for testing |
| 531 | log.log_dir = str(tmp_path / "logs") |
| 532 | log._ensure_directories() |
| 533 | |
| 534 | log.info("Test with global logger", test=True) |
| 535 | |
| 536 | today = time.strftime('%Y-%m-%d') |
| 537 | log_file = f'{log.log_dir}/app/{today}.tsv' |
| 538 | |
| 539 | assert os.path.exists(log_file) |