Esempio n. 1
0
 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)
Esempio n. 2
0
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
Esempio n. 3
0
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)
Esempio n. 4
0
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))
Esempio n. 5
0
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")
Esempio n. 6
0
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))
Esempio n. 7
0
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')
Esempio n. 8
0
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')
Esempio n. 9
0
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__)
Esempio n. 10
0
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')
Esempio n. 11
0
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()
Esempio n. 12
0
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")
Esempio n. 13
0
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")
Esempio n. 15
0
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)
Esempio n. 16
0
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
Esempio n. 17
0
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()
Esempio n. 18
0
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__)
Esempio n. 19
0
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()
Esempio n. 20
0
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()=}")
Esempio n. 21
0
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__
Esempio n. 22
0
def main():
    balsa = Balsa(application_name, 'james abel')
    balsa.init_logger()
    log.error('my error example')
Esempio n. 23
0
def main():

    parser = argparse.ArgumentParser(
        prog=__application_name__,
        description=__description__,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        epilog=
        f"v{__version__}, www.abel.co, see github.com/jamesabel/backupjca for LICENSE.",
    )
    parser.add_argument("path", help="directory to back up to")
    parser.add_argument("-a",
                        "--aws",
                        action="store_true",
                        default=False,
                        help="backup AWS S3 and DynamoDB")
    parser.add_argument("-s",
                        "--s3",
                        action="store_true",
                        default=False,
                        help="backup AWS S3")
    parser.add_argument("-d",
                        "--dynamodb",
                        action="store_true",
                        default=False,
                        help="backup AWS DynamoDB")
    parser.add_argument("-g",
                        "--github",
                        action="store_true",
                        default=False,
                        help="backup github")
    parser.add_argument(
        "--github_subdir",
        default="github",
        help="github subdir (can be used to shorten the overall path)")
    parser.add_argument("-e",
                        "--exclude",
                        nargs="*",
                        help="exclude these AWS S3 buckets and/or tables")
    parser.add_argument(
        "-x",
        "--exclude_file",
        help="exclude the AWS S3 buckets and/or tables listed in this file")
    parser.add_argument(
        "-p",
        "--profile",
        help="AWS profile (uses the default AWS profile if not given)")
    parser.add_argument(
        "--dry_run",
        action="store_true",
        default=False,
        help=
        "Displays operations that would be performed using the specified command without actually running them"
    )
    parser.add_argument("-v",
                        "--verbose",
                        action="store_true",
                        default=False,
                        help="set verbose")
    args = parser.parse_args()

    balsa = Balsa(__application_name__, __author__)
    if args.verbose:
        balsa.verbose = args.verbose
    balsa.log_directory = args.path
    balsa.delete_existing_log_files = True
    balsa.init_logger()
    log.info(f"__application_name__={__application_name__}")
    log.info(f"__author__={__author__}")
    log.info(f"__version__={__version__}")

    if args.exclude is not None:
        exclusion_list = args.exclude
    else:
        exclusion_list = []
    if args.exclude_file is not None:
        with open(args.exclude_file) as f:
            for file_line in f:
                if file_line is not None:
                    file_line = file_line.strip()
                    if len(file_line) > 0 and file_line[0] != "#":
                        exclusion_list.append(file_line)
    log.debug(f"exclusion_list={exclusion_list}")

    did_something = False
    if args.s3 or args.aws:
        s3_local_backup(args.path, args.profile, args.dry_run, exclusion_list)
        did_something = True
    if args.dynamodb or args.aws:
        dynamodb_local_backup(args.path, args.profile, args.dry_run,
                              exclusion_list)
        did_something = True
    if args.github:
        github_local_backup(args.path, args.github_subdir)
        did_something = True
    if not did_something:
        print(
            "nothing to do - please specify a backup to do or -h/--help for help"
        )
Esempio n. 24
0
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()
Esempio n. 25
0
def main():
    balsa = Balsa(application_name, author, gui=True)
    balsa.init_logger()

    something_useful()