#!/usr/bin/env python3 """ Randomly writes from a set of messages simulating logging. """ import argparse import logging import random import time def parse_args(): argp = argparse.ArgumentParser() argp.add_argument('file', help="Full path for log file") argp.add_argument('-d', '--delay', type=int, default=5, help="Number to delay log entries (1/n)") return argp.parse_args() def random_message(log_level): debug_messages = [ "Values = [10, 20, 33]", "Cake ratio = 3.14", "Setting flat mode", "Back to normal mode", "Voltage: 1.21 Gigawatts", "Speed: 88 mph", "Temperature: 32.4C" ] info_messages = [ "My cat is blue", "All your base are belong to us", "I like turtles", "Chocolate rain...", "I hate prunes", "The cake is a lie", "I'm giving her all she's got!" ] warning_messages = [ "Numbers reaching critical levels", "Magnetic constrictors around the anti-matter containment fields are failing", "Approaching thermal thresholds", "Searching is taking too long", "There are monkeyboys in the facility", "Structural integrity approaching tolerance", "Process taking too long" ] error_messages = [ "Unable to process request", "Connection lost with host", "Buffer overflow", "Warp engines are offline", "No more coffee", "Server not responding", "Cannot divide by zero" ] message_level = [ debug_messages, info_messages, warning_messages, error_messages ] which_message = random.randint(0, 6) return message_level[log_level - 1][which_message] def main(): args = parse_args() _LEVEL = logging.NOTSET _FORMAT = logging.Formatter("%(asctime)-15s [%(levelname)-8s] : %(lineno)d : %(name)s.%(funcName)s : %(message)s") _file_handler = logging.FileHandler(args.file, mode='a') _file_handler.setFormatter(_FORMAT) _file_handler.setLevel(_LEVEL) log = logging.Logger('fake-logs') log.addHandler(_file_handler) try: while True: sleep_time = random.random() * float(f"0.{args.delay}") logging_level = random.randint(0, 40) % 4 log.log(logging_level * 10, random_message(logging_level)) time.sleep(sleep_time) except KeyboardInterrupt: time.sleep(0.25) log.error(random_message(4)) if __name__ == "__main__": main()