apv- 🐍 advanced python logging 📔 |
git clone git://git.acid.vegas/apv.git |
Log | Files | Refs | Archive | README | LICENSE |
unit_test.py (2968B)
1 #!/usr/bin/env python3 2 # Advanced Python Logging - Developed by acidvegas in Python (https://git.acid.vegas/apv) 3 # unit_test.py 4 5 import logging 6 import os 7 import random 8 import sys 9 import time 10 11 sys.dont_write_bytecode = True # FUCKOFF __pycache__ 12 13 import apv 14 15 16 def test_console_logging(): 17 '''Test basic console logging functionality''' 18 19 print('\nTesting Console Logging...') 20 apv.setup_logging(level='DEBUG', date_format='%H:%M:%S') 21 for level in ['debug', 'info', 'warning', 'error', 'critical']: 22 getattr(logging, level)(f'Testing {level} message in console.') 23 time.sleep(1) 24 25 26 def test_json_console_logging(): 27 '''Test JSON console logging''' 28 29 print('\nTesting JSON Console Logging...') 30 apv.setup_logging(level='DEBUG', date_format='%H:%M:%S', json_log=True, log_to_disk=False) 31 logging.info('Test JSON console message with custom field', extra={'_custom_field': 'test value'}) 32 logging.warning('Test JSON console warning with error', exc_info=Exception('Test error')) 33 time.sleep(1) 34 35 36 def test_detailed_logging(): 37 '''Test console logging with details''' 38 39 print('\nTesting Detailed Logging...') 40 apv.setup_logging(level='DEBUG', show_details=True) 41 for level in ['debug', 'info', 'warning', 'error', 'critical']: 42 getattr(logging, level)(f'Testing {level} message with details.') 43 time.sleep(1) 44 45 46 def test_file_logging(): 47 '''Test file logging with rotation''' 48 49 print('\nTesting File Logging...') 50 log_file = 'logs/test_log.log' 51 apv.setup_logging(level='DEBUG', log_to_disk=True, max_log_size=1024, max_backups=3, log_file_name='test_log') 52 for i in range(50): 53 level = random.choice(['debug', 'info', 'warning', 'error', 'critical']) 54 getattr(logging, level)(f'File logging test message {i}') 55 56 assert os.path.exists(log_file), "Log file was not created" 57 time.sleep(1) 58 59 60 def test_json_logging(): 61 '''Test JSON format logging''' 62 63 print('\nTesting JSON Logging...') 64 apv.setup_logging(level='DEBUG', log_to_disk=True, log_file_name='json_test', json_log=True) 65 logging.info('Test JSON formatted log message') 66 assert os.path.exists('logs/json_test.json'), "JSON log file was not created" 67 time.sleep(1) 68 69 70 def test_compressed_logging(): 71 '''Test compressed log files''' 72 73 print('\nTesting Compressed Logging...') 74 apv.setup_logging(level='DEBUG', log_to_disk=True, max_log_size=512, max_backups=2, log_file_name='compressed_test', compress_backups=True) 75 for i in range(100): 76 logging.info(f'Testing compression message {i}') 77 time.sleep(1) 78 # Check for .gz files 79 gz_files = [f for f in os.listdir('logs') if f.startswith('compressed_test') and f.endswith('.gz')] 80 assert len(gz_files) > 0, 'No compressed log files were created' 81 82 83 if __name__ == '__main__': 84 # Create logs directory if it doesn't exist 85 os.makedirs('logs', exist_ok=True) 86 87 # Run all tests 88 test_console_logging() 89 test_json_console_logging() 90 test_detailed_logging() 91 test_file_logging() 92 test_json_logging() 93 test_compressed_logging() 94 95 print('\nAll tests completed successfully!')