def test_override_name_re_filter(): """override apply to logger names match regular expression """ print(" " * 4, "test_override_name_re_filter") logger1 = DLogger(name="main", filename="app.log", log_level=logging.WARNING) logger2 = DLogger(name="sys1", filename="sys1.log", log_level=logging.ERROR) logger3 = DLogger(name="sys2", filename="sys2.log", log_level=logging.WARNING) write_message(logger1) write_message(logger2) write_message(logger3) assert message_count(path.join("logs", "app.log")) == 3 assert message_count(path.join("logs", "sys1.log")) == 2 assert message_count(path.join("logs", "sys2.log")) == 3 LOG_CONF.logger_name_re_filter = "^sys" # apply to logger names start with "sys" LOG_CONF.log_level = logging.INFO LOG_CONF.override_logger_settings(True) write_message(logger1) write_message(logger2) write_message(logger3) assert message_count(path.join("logs", "app.log")) == 3 + 3 assert message_count(path.join("logs", "sys1.log")) == 2 + 4 assert message_count(path.join("logs", "sys2.log")) == 3 + 4
def test_override_log_filenames(): """ test_override_log_filenames """ print(" " * 4, "test_override_log_filenames") logger1 = DLogger(name="sys", filename="sys.log", log_level=logging.WARNING) logger2 = DLogger(name="app", filename="app.log", log_level=logging.INFO, log_fmt=FORMATS[Fmt.FNAME_LEVEL_MSG]) write_message(logger1) write_message(logger2) print_file_contents(path.join("logs", "sys.log")) assert message_count(path.join("logs", "sys.log")) == 3 print_file_contents(path.join("logs", "app.log")) assert message_count(path.join("logs", "app.log")) == 4 # move all messages to 'logs/main.log' LOG_CONF.filename = "main.log" LOG_CONF.override_logger_settings(True) write_message(logger1) write_message(logger2) assert message_count(path.join("logs", "sys.log")) == 3 assert message_count(path.join("logs", "app.log")) == 4 print_file_contents(path.join("logs", "main.log")) assert message_count(path.join("logs", "main.log")) == 7
def test_propagate_false(): print("test_propagate_false()") clog1 = DLogger("main", filename="main.log", log_enabled=True) clog2 = DLogger("main.sub", filename="sub.log", log_enabled=True, propagate=False) clog1.info("1 - msg to main.log") clog2.info("2- msg to sub.log and not propagate to main.log") print_file_contents(path.join("logs", "main.log")) print_file_contents(path.join("logs", "sub.log"))
def test_propagate(): print("test_propagate()") clog1 = DLogger("main", filename="main.log") clog2 = DLogger("main.sub", filename="main.sub.log", propagate=True) clog1.info("1 - msg to main.log") clog2.info("2 - msg to main.sub.log and propagate to main.log") print_file_contents(path.join("logs", "main.log")) print_file_contents(path.join("logs", "main.sub.log"))
def test_simple(): logger = DLogger("main", filename="app_simple.log", log_fmt=FORMATS[Fmt.LEVEL_MSG]) write_message(logger) if os.getcwd().endswith('tests'): assert compare_files(path.join("test_data", "app_simple.log"), path.join("logs", "app_simple.log")) else: assert compare_files(path.join("tests", "test_data", "app_simple.log"), path.join("logs", "app_simple.log"))
def test_override_name_re_filter_2(): """ override apply to logger names match regular expression skip override_allowed=False loggers """ print(" " * 4, "test_override_name_re_filter_2") logger1 = DLogger(name="main", filename="app.log", log_level=logging.WARNING) logger2 = DLogger(name="sys1", filename="sys1.log", log_level=logging.ERROR, override_allowed=False) logger3 = DLogger(name="sys2", filename="sys2.log", log_level=logging.WARNING) write_message(logger1) write_message(logger2) write_message(logger3) assert message_count(path.join("logs", "app.log")) == 3 assert message_count(path.join("logs", "sys1.log")) == 2 assert message_count(path.join("logs", "sys2.log")) == 3 # apply to logger names start w/ "sys" & override_allowed=True LOG_CONF.logger_name_re_filter = "^sys" LOG_CONF.log_level = logging.INFO LOG_CONF.override_logger_settings(True) write_message(logger1) write_message(logger2) write_message(logger3) assert message_count(path.join("logs", "app.log")) == 3 + 3 assert message_count(path.join( "logs", "sys1.log")) == 2 + 2 # logger 'sys1' override_allowed=False assert message_count(path.join("logs", "sys2.log")) == 3 + 4
def test_override_log_level(): """test_override_log_level""" print(" " * 4, "test_override_log_level") logger = DLogger(name="main", filename="app.log", log_level=logging.INFO) write_message(logger) print_file_contents(path.join("logs", "app.log")) assert message_count(path.join("logs", "app.log")) == 4 # override log_level 'logging.INFO' w/ 'logging.WARNING' LOG_CONF.log_level = logging.WARNING LOG_CONF.override_logger_settings(True) write_message(logger) print_file_contents(path.join("logs", "app.log")) assert message_count(path.join("logs", "app.log")) == 7
def test_override_log_filename(): """test_override_log_filename""" print(" " * 4, "test_override_log_filename") logger = DLogger(name="main", filename="app.log", log_level=logging.INFO) write_message(logger) print_file_contents(path.join("logs", "app.log")) assert message_count(path.join("logs", "app.log")) == 4 # override log filename 'app.log' w/ 'app_new.log' LOG_CONF.filename = "app_new.log" LOG_CONF.override_logger_settings(True) write_message(logger) print_file_contents(path.join("logs", "app_new.log")) assert message_count(path.join("logs", "app.log")) == 4 assert message_count(path.join("logs", "app_new.log")) == 4
def test_override_subdir(): """test_override_subdir""" print(" " * 4, "test_override_subdir") logger = DLogger(name="main", filename="app.log", log_level=logging.INFO) write_message(logger) print_file_contents(path.join("logs", "app.log")) assert message_count(path.join("logs", "app.log")) == 4 # override default empty log subdir w/ 'sys' LOG_CONF.sub_dir = "sys" LOG_CONF.override_logger_settings(True) write_message(logger) print_file_contents(path.join("logs", "sys", "app.log")) assert message_count(path.join("logs", "app.log")) == 4 assert message_count(path.join("logs", "sys", "app.log")) == 4
def test_override_log_dir(): """override default log dir, 'logs', which is under current directory""" print(" " * 4, "test_override_log_dir") logger = DLogger(name="main", filename="app.log", log_level=logging.INFO) write_message(logger) print_file_contents(path.join("logs", "app.log")) assert message_count(path.join("logs", "app.log")) == 4 # override default log dir 'logs' w/ 'test_logs' LOG_CONF.log_dir = "test_logs" LOG_CONF.override_logger_settings(True) write_message(logger) print_file_contents(path.join("test_logs", "app.log")) assert message_count(path.join("logs", "app.log")) == 4 assert message_count(path.join("test_logs", "app.log")) == 4
def test_override_multi_attributes(): """test_override_multi_attributes""" print(" " * 4, "test_override_multiple_attributes") logger = DLogger(name="main", filename="app.log", log_level=logging.INFO) write_message(logger) print_file_contents(path.join("logs", "app.log")) assert message_count(path.join("logs", "app.log")) == 4 LOG_CONF.log_dir = "test_logs" # override default log dir 'logs' w/ 'test_logs' LOG_CONF.sub_dir = "sys" # override default empty log subdir w/ 'sys' LOG_CONF.filename = "app_new.log" LOG_CONF.log_level = logging.WARNING LOG_CONF.log_fmt = FORMATS[Fmt.FNAME_LINENO_FUNCNAME] LOG_CONF.override_logger_settings(True) write_message(logger) print_file_contents(path.join("logs", "sys", "app_new.log")) assert message_count(path.join("logs", "app.log")) == 4 # no changes assert message_count(path.join("test_logs", "sys", "app_new.log")) == 3
def test_dual(capsys): clog = DLogger("main", filename="app_simple.log", log_fmt=FORMATS[Fmt.LEVEL_MSG], cout_enabled=True, cout_level=logging.INFO) clog.debug('debug message') clog.info('info message') out, err = capsys.readouterr() assert err == "info message\n" clog.warning('warn message') _, err = capsys.readouterr() assert err == "warn message\n" clog.error('error message') _, err = capsys.readouterr() assert err == "error message\n" clog.critical('critical message') _, err = capsys.readouterr() assert err == "critical message\n" if os.getcwd().endswith('tests'): assert compare_files(path.join("test_data", "app_simple.log"), path.join("logs", "app_simple.log")) else: assert compare_files(path.join("tests", "test_data", "app_simple.log"), path.join("logs", "app_simple.log"))
def test_propagate_root_stdout(): # error message goes top log file and screen print("test_propagate_root_stdout()") clog_ = DLogger("root", filename="app_root.log", log_enabled=True) # root logger if name = "" clog0 = DLogger("", filename="app_root.log", log_enabled=True) # root logger if name = "" clog1 = DLogger("main", filename="app_main.log", log_enabled=True, propagate=True) clog2 = DLogger("main.sub", filename="sub.log", log_enabled=True, propagate=False) clog0.info("clog0 - msg to root") clog1.info("clog1 - msg to main") clog2.info("clog2 - msg to sub and propagate to main") clog2.error("clog2 - error msg goes to file and stdout/stderr") print_file_contents(path.join("logs", "app_root.log")) print_file_contents(path.join("logs", "app_main.log")) print_file_contents(path.join("logs", "sub.log")) loggers = get_all_loggers() print("len(loggers): ", len(loggers)) show_all_loggers()
def test_propagate_root(): print("test_propagate_root()") clog0 = DLogger("", filename="app_root.log", log_enabled=True) # root logger if name = "" clog1 = DLogger("main", filename="app_main.log", log_enabled=True, propagate=True) clog2 = DLogger("main.sub", filename="sub.log", log_enabled=True, propagate=True) clog0.info("0 - msg to root") clog1.info("1 - msg to main") clog2.info("2 - msg to sub and propagate to main") print_file_contents(path.join("logs", "app_root.log")) print_file_contents(path.join("logs", "app_main.log")) print_file_contents(path.join("logs", "sub.log"))
def test_info_warn(): clog1 = DLogger("system", cout_fmt=FORMATS[Fmt.FNAME_LEVEL_MSG], log_level=logging.INFO, cout_level=logging.WARN, cout_enabled=True) display_details(clog1) clog1.debug('1 - msg debug') clog1.info('2 - msg info') clog1.warning('3 - msg warn') clog1.error('4 - msg error') clog1.critical('5 - msg critical') expect(no_items_in_log=4, no_items_in_cout=3)