apv- 🐍 advanced python logging 📔 |
git clone git://git.acid.vegas/apv.git |
Log | Files | Refs | Archive | README |
unittest.py (3925B)
1 #! /usr/bin/env python3 2 # Advanced Python Logging - Developed by acidvegas in Python (https://git.acid.vegas/apv) 3 # unittest.py 4 5 import logging 6 import random 7 import time 8 9 # prevent bytecode files (.pyc) from being written 10 from sys import dont_write_bytecode 11 dont_write_bytecode = True 12 13 import apv 14 15 # Test console logging with custom date format 16 apv.setup_logging(level='DEBUG', date_format='%H:%M:%S') 17 logging.debug('Testing debug message in console.') 18 logging.info('Testing info message in console.') 19 logging.warning('Testing warning message in console.') 20 logging.error('Testing error message in console.') 21 logging.critical('Testing critical message in console.') 22 23 print() 24 25 # Test console logging with details 26 time.sleep(2) 27 apv.setup_logging(level='DEBUG', date_format='%Y-%m-%d %H:%M:%S', show_details=True) 28 logging.debug('Testing debug message in console with details.') 29 logging.info('Testing info message in console with details.') 30 logging.warning('Testing warning message in console with details.') 31 logging.error('Testing error message in console with details.') 32 logging.critical('Testing critical message in console with details.') 33 34 print() 35 36 # Test disk logging with JSON and regular rotation 37 logging.debug('Starting test: Disk logging with JSON and regular rotation...') 38 time.sleep(2) 39 apv.setup_logging(level='DEBUG', log_to_disk=True, max_log_size=1024, max_backups=3, log_file_name='json_log', json_log=True, show_details=True) 40 for i in range(100): 41 log_level = random.choice([logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL]) 42 logging.log(log_level, f'Log entry {i+1} for JSON & regular rotation test.') 43 time.sleep(0.1) 44 45 print() 46 47 # Test disk logging with rotation & compression 48 logging.debug('Starting test: Disk logging with rotation & compression...') 49 time.sleep(2) 50 apv.setup_logging(level='DEBUG', log_to_disk=True, max_log_size=1024, max_backups=3, log_file_name='plain_log', show_details=True, compress_backups=True) 51 for i in range(100): 52 log_level = random.choice([logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL]) 53 logging.log(log_level, f'Log entry {i+1} for disk rotation & compression test.') 54 time.sleep(0.1) 55 56 logging.info('Test completed. Check the logs directory for disk logging & JSON logging tests.') 57 58 print() 59 60 try: 61 import ecs_logging 62 except ImportError: 63 pass 64 else: 65 # Test ECS logging 66 logging.debug('Starting test: ECS logging...') 67 time.sleep(2) 68 apv.setup_logging(level='DEBUG', ecs_log=True) 69 logging.debug('This is a test log message to ECS.') 70 logging.info('This is a test log message to ECS.') 71 logging.warning('This is a test log message to ECS.') 72 logging.error('This is a test log message to ECS.') 73 logging.critical('This is a test log message to ECS.') 74 75 print() 76 77 # Test Graylog handler (Uncomment & configure to test) 78 # logging.debug('Starting test: Graylog handler...') 79 # time.sleep(2) 80 # apv.setup_logging(level='DEBUG', enable_graylog=True, graylog_host='your_graylog_host', graylog_port=12201) 81 # logging.debug('This is a test log message to Graylog.') 82 # logging.info('This is a test log message to Graylog.') 83 # logging.warning('This is a test log message to Graylog.') 84 # logging.error('This is a test log message to Graylog.') 85 # logging.critical('This is a test log message to Graylog.') 86 87 # Test CloudWatch handler (Uncomment & configure to test) 88 # logging.debug('Starting test: CloudWatch handler...') 89 # time.sleep(2) 90 # apv.setup_logging(level='DEBUG', enable_cloudwatch=True, cloudwatch_group_name='your_log_group', cloudwatch_stream_name='your_log_stream') 91 # logging.debug('This is a test log message to CloudWatch.') 92 # logging.info('This is a test log message to CloudWatch.') 93 # logging.warning('This is a test log message to CloudWatch.') 94 # logging.error('This is a test log message to CloudWatch.') 95 # logging.critical('This is a test log message to CloudWatch.')