hat.syslog.generator
Syslog test message generator
1"""Syslog test message generator""" 2 3import argparse 4import asyncio 5import contextlib 6import logging.config 7import sys 8 9from hat import aio 10from hat.syslog import common 11 12 13mlog: logging.Logger = logging.getLogger('hat.syslog.generator') 14"""Module logger""" 15 16 17def create_argument_parser() -> argparse.ArgumentParser: 18 """Create argument parser""" 19 parser = argparse.ArgumentParser() 20 parser.add_argument( 21 '--comm-type', choices=[i.name for i in common.CommType], 22 default='TCP', help="syslog server host name (default TCP)") 23 parser.add_argument( 24 '--host', metavar='HOST', default='127.0.0.1', 25 help="syslog server host name (default 127.0.0.1)") 26 parser.add_argument( 27 '--port', metavar='PORT', type=int, default=6514, 28 help="syslog server port (default 6514)") 29 parser.add_argument( 30 '--count', metavar='N', type=int, default=1, 31 help="number of log messages (default 1)") 32 parser.add_argument( 33 '--text', metavar='TEXT', default='syslog generator test', 34 help="log message text") 35 parser.add_argument( 36 '--level', metavar='LEVEL', default='INFO', 37 choices=('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'), 38 help="log level") 39 parser.add_argument( 40 '--with-exc', action='store_true', 41 help="include exception") 42 parser.add_argument( 43 '--queue-size', metavar='N', type=int, default=1024, 44 help="client's log message queue size (default 1024)") 45 parser.add_argument( 46 '--msg-delay', metavar='T', type=float, default=0.01, 47 help="time delay between message generation in seconds " 48 "(default 0.01)") 49 parser.add_argument( 50 '--end-delay', metavar='T', type=float, default=0.5, 51 help="time delay affter all messages have been generated in seconds " 52 "(default 0.5)") 53 return parser 54 55 56def main(): 57 """Syslog test message generator""" 58 parser = create_argument_parser() 59 args = parser.parse_args() 60 61 aio.init_asyncio() 62 63 logging.config.dictConfig({ 64 'version': 1, 65 'formatters': { 66 'default': {}}, 67 'handlers': { 68 'syslog': { 69 'class': 'hat.syslog.handler.SyslogHandler', 70 'host': args.host, 71 'port': args.port, 72 'comm_type': 'TCP', 73 'level': args.level, 74 'formatter': 'default', 75 'queue_size': args.queue_size}}, 76 'loggers': { 77 'asyncio': { 78 'level': 'INFO'}}, 79 'root': { 80 'level': args.level, 81 'handlers': ['syslog']}, 82 'disable_existing_loggers': False}) 83 84 with contextlib.suppress(asyncio.CancelledError): 85 aio.run_asyncio(async_main(count=args.count, 86 text=args.text, 87 level=getattr(logging, args.level), 88 with_exc=args.with_exc, 89 msg_delay=args.msg_delay, 90 end_delay=args.end_delay)) 91 92 93async def async_main(count: int, 94 text: str, 95 level: int, 96 with_exc: bool, 97 msg_delay: float, 98 end_delay: float): 99 """Async main""" 100 digits_count = _number_of_digits(count) 101 102 for i in range(count): 103 msg = f'{{}} {{:0{digits_count}}}'.format(text, i) 104 exc_info = Exception(msg) if with_exc else None 105 mlog.log(level, msg, exc_info=exc_info) 106 107 await asyncio.sleep(msg_delay) 108 109 await asyncio.sleep(end_delay) 110 111 112def _number_of_digits(x): 113 if x < 10: 114 return 1 115 return 1 + _number_of_digits(x // 10) 116 117 118if __name__ == '__main__': 119 sys.argv[0] = 'hat-syslog-generator' 120 sys.exit(main())
Module logger
def
create_argument_parser() -> argparse.ArgumentParser:
18def create_argument_parser() -> argparse.ArgumentParser: 19 """Create argument parser""" 20 parser = argparse.ArgumentParser() 21 parser.add_argument( 22 '--comm-type', choices=[i.name for i in common.CommType], 23 default='TCP', help="syslog server host name (default TCP)") 24 parser.add_argument( 25 '--host', metavar='HOST', default='127.0.0.1', 26 help="syslog server host name (default 127.0.0.1)") 27 parser.add_argument( 28 '--port', metavar='PORT', type=int, default=6514, 29 help="syslog server port (default 6514)") 30 parser.add_argument( 31 '--count', metavar='N', type=int, default=1, 32 help="number of log messages (default 1)") 33 parser.add_argument( 34 '--text', metavar='TEXT', default='syslog generator test', 35 help="log message text") 36 parser.add_argument( 37 '--level', metavar='LEVEL', default='INFO', 38 choices=('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'), 39 help="log level") 40 parser.add_argument( 41 '--with-exc', action='store_true', 42 help="include exception") 43 parser.add_argument( 44 '--queue-size', metavar='N', type=int, default=1024, 45 help="client's log message queue size (default 1024)") 46 parser.add_argument( 47 '--msg-delay', metavar='T', type=float, default=0.01, 48 help="time delay between message generation in seconds " 49 "(default 0.01)") 50 parser.add_argument( 51 '--end-delay', metavar='T', type=float, default=0.5, 52 help="time delay affter all messages have been generated in seconds " 53 "(default 0.5)") 54 return parser
Create argument parser
def
main():
57def main(): 58 """Syslog test message generator""" 59 parser = create_argument_parser() 60 args = parser.parse_args() 61 62 aio.init_asyncio() 63 64 logging.config.dictConfig({ 65 'version': 1, 66 'formatters': { 67 'default': {}}, 68 'handlers': { 69 'syslog': { 70 'class': 'hat.syslog.handler.SyslogHandler', 71 'host': args.host, 72 'port': args.port, 73 'comm_type': 'TCP', 74 'level': args.level, 75 'formatter': 'default', 76 'queue_size': args.queue_size}}, 77 'loggers': { 78 'asyncio': { 79 'level': 'INFO'}}, 80 'root': { 81 'level': args.level, 82 'handlers': ['syslog']}, 83 'disable_existing_loggers': False}) 84 85 with contextlib.suppress(asyncio.CancelledError): 86 aio.run_asyncio(async_main(count=args.count, 87 text=args.text, 88 level=getattr(logging, args.level), 89 with_exc=args.with_exc, 90 msg_delay=args.msg_delay, 91 end_delay=args.end_delay))
Syslog test message generator
async def
async_main( count: int, text: str, level: int, with_exc: bool, msg_delay: float, end_delay: float):
94async def async_main(count: int, 95 text: str, 96 level: int, 97 with_exc: bool, 98 msg_delay: float, 99 end_delay: float): 100 """Async main""" 101 digits_count = _number_of_digits(count) 102 103 for i in range(count): 104 msg = f'{{}} {{:0{digits_count}}}'.format(text, i) 105 exc_info = Exception(msg) if with_exc else None 106 mlog.log(level, msg, exc_info=exc_info) 107 108 await asyncio.sleep(msg_delay) 109 110 await asyncio.sleep(end_delay)
Async main