def test_print_in_file_exceptions_log(self): log = LogHelper() exception = Exception('Excepción test') log.error(exception) try: 1 / 0 except Exception as ex: log.fatal(ex) log_name = 'test_print_in_file_exceptions_log.log' self.assertTrue(log.logs.__len__() == 2) self.assertEqual(log.save_logs(log_name, log_level=LogHelper.DEBUG), True) with open(f'./{log_name}') as file: data = file.read() self.assertTrue(exception.__str__() in data) self.assertTrue('division by zero' in data)
def test_print_in_file_only_info_or_higher_levels(self): log = LogHelper() log.debug(self.DEBUG_MESSAGE) log.info(self.INFO_MESSAGE) log.warning(self.WARNING_MESSAGE) log.error(self.ERROR_MESSAGE) log.fatal(self.FATAL_MESSAGE) log_name = 'test_print_in_file_only_info_or_higher_levels.log' self.assertTrue(log.logs.__len__() == 5) self.assertEqual(log.save_logs(log_name, log_level=LogHelper.INFO), True) with open(f'./{log_name}') as file: data = file.read() self.assertFalse(self.DEBUG_MESSAGE in data) self.assertTrue(self.INFO_MESSAGE in data) self.assertTrue(self.WARNING_MESSAGE in data) self.assertTrue(self.ERROR_MESSAGE in data) self.assertTrue(self.FATAL_MESSAGE in data)
def test_add_external_logs(self): log = LogHelper() external_logs = LogHelper() log.debug(self.DEBUG_MESSAGE) log.info(self.INFO_MESSAGE) log.warning(self.WARNING_MESSAGE) log.error(self.ERROR_MESSAGE) log.fatal(self.FATAL_MESSAGE) external_msg_debug = 'Mensaje de debug externo' external_msg_info = 'Mensaje de info externo' external_msg_warning = 'Mensaje de warning externo' external_msg_error = 'Mensaje de error externo' external_msg_fatal = 'Mensaje de fatal externo' external_logs.debug(external_msg_debug) external_logs.info(external_msg_info) external_logs.warning(external_msg_warning) external_logs.error(external_msg_error) external_logs.fatal(external_msg_fatal) log.add_external_logs(external_logs.logs) self.assertTrue(log.logs.__len__() == 10) log_name = 'test_add_external_logs.log' self.assertEqual(log.save_logs(log_name, log_level=LogHelper.DEBUG), True) with open(f'./{log_name}') as file: data = file.read() self.assertTrue(self.DEBUG_MESSAGE in data) self.assertTrue(self.INFO_MESSAGE in data) self.assertTrue(self.WARNING_MESSAGE in data) self.assertTrue(self.ERROR_MESSAGE in data) self.assertTrue(self.FATAL_MESSAGE in data) self.assertTrue(external_msg_debug in data) self.assertTrue(external_msg_info in data) self.assertTrue(external_msg_warning in data) self.assertTrue(external_msg_error in data) self.assertTrue(external_msg_fatal in data)