def test_to_structured_logging(): application_name = "test_structured_logging" balsa = Balsa(application_name, __author__, verbose=True) balsa.init_logger() log = get_logger(application_name) question = "life" answer = 42 log.info(sf("test structured logging", question=question, answer=answer)) # todo: get these to work # crazy = 'a "crazy" string' # newline_string = "a\nnewline" crazy = "a crazy string" newline_string = "anewline" some_float = 3.3 a_bool = True log.info( sf("test", "more,stuff", question=question, answer=answer, newline_string=newline_string, crazy=crazy, some_float=some_float, a_bool=a_bool)) complex_value = 1 + 2j log.info(sf(complex_value=complex_value))
def init_logger(self): balsa_log = Balsa(self.instance_name, author_name, log_directory=os.path.join('temp', 'log'), delete_existing_log_files=True) balsa_log.init_logger() self.log = get_logger(application_name)
def test_threaded_gui(): global exception_complete timeout = 10 application_name = 'main_thread' log = get_logger(application_name) balsa = Balsa(application_name, __author__, verbose=True, log_directory='temp', gui=True, is_root=False, delete_existing_log_files=True) balsa.init_logger() log.info('starting main thread') exception_complete = False gui_thread = QtGuiThread() gui_thread.start() loop_count = 0 while gui_thread.isRunning() and loop_count < timeout: time.sleep(1) loop_count += 1 assert exception_complete exception_complete = False gui_thread = GuiThread() gui_thread.start() gui_thread.join(timeout) assert exception_complete
def test_multiprocessing(): log_directory = Path("temp", application_name) balsa = Balsa(application_name, __author__, verbose=True, log_directory=log_directory, delete_existing_log_files=True) balsa.init_logger() log = get_logger(application_name) log.info("process started") balsa_config = balsa.config_as_dict() my_process = MyProcess(balsa_config) my_process.start() my_process.join() process_log_file_path = Path(log_directory, f"{application_name}_a.log") assert process_log_file_path.exists() log_text = process_log_file_path.read_text() # check everything except the timestamp and line number assert "test_balsa_multiprocess - a - test_multiprocessing.py -" in log_text assert "- run - INFO - hello from a" in log_text log.info("process finished")
def main(): balsa = Balsa(application_name, 'james abel') balsa.init_logger() log.info('I am a message') log.info('So am I') for s in balsa.get_string_list(): print(s)
def cli_main(args): ui_type = UITypes.cli balsa = Balsa(__application_name__, __author__) balsa.log_console_prefix = "\r" balsa.init_logger_from_args(args) log.info(f"__application_name__={__application_name__}") log.info(f"__author__={__author__}") log.info(f"__version__={__version__}") try: preferences = get_preferences(ui_type) preferences.backup_directory = args.path # backup classes will read the preferences DB directly preferences.github_token = args.token preferences.aws_profile = args.profile # If setting the exclusions, just do it for one backup type at a time. The values are stored for subsequent runs. if args.exclude is not None and len(args.exclude) > 0: if args.s3: ExclusionPreferences(BackupTypes.S3.name).set(args.exclude) elif args.dynamodb: ExclusionPreferences(BackupTypes.DynamoDB.name).set( args.exclude) elif args.github: ExclusionPreferences(BackupTypes.github.name).set(args.exclude) did_something = False dynamodb_local_backup = None s3_local_backup = None github_local_backup = None if args.s3 or args.aws: s3_local_backup = S3Backup(ui_type, log.info, log.warning, log.error) s3_local_backup.start() did_something = True if args.dynamodb or args.aws: dynamodb_local_backup = DynamoDBBackup(ui_type, log.info, log.warning, log.error) dynamodb_local_backup.start() did_something = True if args.github: github_local_backup = GithubBackup(ui_type, log.info, log.warning, log.error) github_local_backup.start() did_something = True if not did_something: print( "nothing to do - please specify a backup to do or -h/--help for help" ) if dynamodb_local_backup is not None: dynamodb_local_backup.join() if s3_local_backup is not None: s3_local_backup.join() if github_local_backup is not None: github_local_backup.join() except Exception as e: log.exception(e)
def main(): balsa = Balsa(application_name, author, verbose=True) balsa.init_logger() my_value = 42 my_name = "me" log.info(sf("myapp", my_name=my_name, my_value=my_value))
def test_balsa_simple(): application_name = 'test_balsa_simple' balsa = Balsa(application_name, __author__, is_root=False) balsa.init_logger() log = get_logger(application_name) log.error('test error message')
def test_balsa_simple(): application_name = 'test_balsa' balsa = Balsa(application_name, __author__, verbose=True, log_directory='temp', gui=True, delete_existing_log_files=True) balsa.init_logger() log = get_logger(application_name) log.error('test error message')
def main(): parser = argparse.ArgumentParser() parser.add_argument('-v', '--verbose', action='store_true', help='verbose') args = parser.parse_args() balsa = Balsa(application_name, author, error_callback=balsa_example_error_callback) balsa.init_logger_from_args(args) something_useful()
def test_two_logs(): """ test two logs where one is the root (catch-all) and other is separate (no propagation to root) """ log_names = ["a", "b", "c"] # instantiate the balsa objects # The 'a' log is the root and therefore the catch-all # The 'b' log has no propagation and is therefore only its messages balsas = { "a": Balsa("a", __author__, verbose=True), "b": Balsa("b", __author__, verbose=True, propagate=False, is_root=False) } [b.init_logger() for k, b in balsas.items()] logs = [get_logger(test_name) for test_name in log_names] # get the loggers for index, log in enumerate(logs): # log something log.info(log_names[index]) # check the contents assert len(balsas["a"].get_string_list() ) == 2 # gets the 'c' log in addition to the 'a' log assert len(balsas["b"].get_string_list()) == 1 # just the 'b' log_string_min_length = 35 # SWAG to try to ensure the log string includes the time stamp, level, etc. assert balsas["a"].get_string_list()[0][-1] == "a" assert len(balsas["a"].get_string_list()[0]) > log_string_min_length assert balsas["a"].get_string_list()[1][-1] == "c" assert len(balsas["a"].get_string_list()[1]) > log_string_min_length assert balsas["b"].get_string_list()[0][-1] == "b" assert len(balsas["b"].get_string_list()[0]) > log_string_min_length
def test_balsa_clone_parent(): my_balsa = Balsa("test_balsa_clone_parent_a", "parent_a") my_balsa_config = my_balsa.config_as_dict() new_balsa = balsa_clone(my_balsa_config, "test_balsa_clone_parent_b", my_balsa) new_balsa.init_logger() new_balsa.log.error("no parent") my_balsa.remove() new_balsa.remove()
def test_balsa_sentry(): application_name = "test_balsa_sentry" if "SENTRY_DSN" in os.environ: balsa = Balsa(application_name, __author__, use_sentry=True, inhibit_cloud_services=False, is_root=False, sentry_dsn=os.environ["SENTRY_DSN"]) balsa.init_logger() log = get_logger(application_name) log.error("test balsa sentry error message") else: print("Please set SENTRY_DSN environment variable to have a good %s test" % __name__)
def test_balsa_simple(): application_name = 'test_balsa' balsa = Balsa(application_name, __author__, verbose=True, log_directory='temp', error_callback=my_callback) balsa.init_logger() log.error('test error message')
def test_balsa_gui_std(): application_name = "test_balsa_gui_std" # verbose to make the popup button happen balsa = Balsa(application_name, __author__, verbose=True, log_directory="temp", gui=True, is_root=False, delete_existing_log_files=True) balsa.init_logger() press_enter_thread = threading.Thread(target=press_enter) press_enter_thread.start() print("GUIs should not write to stdout but I am") press_enter_thread.join()
def test_balsa_callback(): application_name = "test_balsa_callback" balsa = Balsa(application_name, __author__, verbose=True, log_directory="temp", error_callback=my_callback, is_root=False) balsa.init_logger() log.error("test error message")
def test_balsa_gui(): application_name = "test_balsa_gui" balsa = Balsa(application_name, __author__, verbose=True, log_directory="temp", gui=True, is_root=False, delete_existing_log_files=True) balsa.init_logger() log = get_logger(application_name) press_enter_thread = threading.Thread(target=press_enter) press_enter_thread.start() log.error("test error message") press_enter_thread.join()
def test_multiprocessing(): balsa = Balsa(application_name, author, verbose=True) balsa.init_logger() log = get_logger(application_name) log.info("process started") my_process = MyProcess(balsa.config_as_dict()) my_process.start() my_process.join() log.info("process finished")
def gui_main(): balsa = Balsa(__application_name__, __author__, gui=True, verbose=get_gui_preferences().verbose) balsa.init_logger() balsa.handlers[HandlerType.DialogBox].setLevel(logging.ERROR) # don't pop up warnings try: app = QApplication([]) bup_gui = BupDialog() bup_gui.show() app.exec_() except Exception as e: log.exception(e)
def test_not_root(): """ test that a non-root log does not pick up the logging to another logger """ balsa = Balsa('a', __author__, verbose=True, is_root=False) balsa.init_logger() some_other_logger = logging.getLogger('b') some_other_logger.error( 'some log' ) # will only be processed by default settings (no formatting, only to stdout) assert (len(balsa.get_string_list()) == 0 ) # ensure the other logger didn't get caught by the balsa logger
def test_gui_rate_limit(): application_name = "test_gui_rate_limit" balsa = Balsa(application_name, __author__, verbose=True, log_directory="temp", gui=True, is_root=False, delete_existing_log_files=True) balsa.init_logger() log = get_logger(application_name) press_enter_thread = threading.Thread(target=press_enter) press_enter_thread.start() for count in range(0, 4): log.warning(str(count)) press_enter_thread.join()
def test_balsa_sentry(): application_name = 'test_balsa_sentry' if 'SENTRY_DSN' in os.environ: balsa = Balsa(application_name, __author__, use_sentry=True, inhibit_cloud_services=False, is_root=False) balsa.init_logger() log = get_logger(application_name) log.error('test balsa sentry error message') else: print( 'Please set SENTRY_DSN environment variable to have a good %s test' % __name__)
def test_balsa_exception(): application_name = 'test_balsa_exception' balsa = Balsa(application_name, __author__, gui=True, is_root=False) balsa.rate_limits[logging.ERROR]['count'] = 4 # we have 3 messages balsa.init_logger() log = get_logger(application_name) press_enter_thread = threading.Thread(target=press_enter) press_enter_thread.start() try: a = 1.0 / 0.0 # generate an exception for testing (not a real error) except ZeroDivisionError: log.error('test exception') log.error(traceback_string()) press_enter_thread.join()
def session_fixture(): temp_dir.mkdir(parents=True, exist_ok=True) cache_dir.mkdir(parents=True, exist_ok=True) balsa = Balsa(__application_name__, __author__, log_directory=Path("log"), delete_existing_log_files=True, verbose=False) # add handler that will throw an assert on ERROR or greater test_handler = TestAWSimpleLoggingHandler() test_handler.setLevel(logging.ERROR) logging.getLogger().addHandler(test_handler) balsa.init_logger() print(f"{is_mock()=}")
def test_global_balsa(): with pytest.raises(RuntimeError): # not yet initialized get_global_balsa() with pytest.raises(RuntimeError): # not yet initialized get_global_config() balsa = Balsa("test_global_balsa", __author__, is_root=False) balsa.init_logger() global_balsa = get_global_balsa() assert global_balsa is not None assert global_balsa.backup_count > 0 # just test some attribute global_config = get_global_config() assert global_config is not None assert global_config["author"] == __author__
def main(): balsa = Balsa(application_name, 'james abel') balsa.init_logger() log.error('my error example')
def test_balsa_args(): balsa = Balsa(application_name, __author__) balsa.init_logger_from_args(None) # args is fake for now ... log.info(__file__)
def main(): epilog = """ cmpa - directory compare (by abel.co) Prints the result of the directory comparison. Only the differences and the summary flag will be printed, unless verbose is given (or if silent given then nothing will be printed). - means extra files in first directory + means extra files in second directory ! means file contents mismatch = equality flag s summary process returns 1 if any mis-compares """ parser = argparse.ArgumentParser( epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('directories', nargs=2, help='two directories to compare') parser.add_argument('-f', '--filters', nargs='*', default=['*'], help='file filters') parser.add_argument( '-e', '--exclude', nargs='*', default=[], help='exclude directories that start with these name(s)') parser.add_argument('-s', '--silent', action='store_true', help='do not print') parser.add_argument( '-t', '--text', action='store_true', help='text based compare that ignores CR/LF differences') parser.add_argument('-i', '--image', action='store_true', help='image based compare') parser.add_argument('--version', action='store_true', help='display version') parser.add_argument('-v', '--verbose', action='store_true', help='verbose') args = parser.parse_args() balsa = Balsa(__title__, __author__) balsa.init_logger_from_args(args) if args.version: print(__version__) cd = Compare(args.directories, args.filters, args.silent, args.text, args.exclude, args.verbose) sys.exit(int(not cd.compare_ok_all)) # 0 is all compared OK, 1 otherwise
def main(): balsa = Balsa(application_name, author, gui=True) balsa.init_logger() something_useful()
import time import random from ismain import is_main from balsa import get_logger, Balsa from bup import print_log, __application_name__, __author__ log = get_logger(__application_name__) def test_print_log(): start = 11 for value in range(start, 0, -1): if value < start - 1: time.sleep(1) print_log(f"{value-1}" * int(round(50.0 * random.random()))) if is_main(): balsa = Balsa(__application_name__, __author__) balsa.init_logger() test_print_log()