def test_init_file(self): log = Diary(self.TXT_PATH, async=False) log.info(self.INFO) log.close() with open(self.TXT_PATH) as f: line = f.readline() self.assertTrue(self.INFO in line)
def test_init_does_not_exist(self): log = Diary(self.NO_EXIST_PATH, async=False) log.info(self.INFO) log.close() with open(self.NO_EXIST_PATH) as f: line = f.readline() self.assertTrue(self.INFO in line)
def test_diary_print(self): log = Diary(self.INIT_DIR, file_name="printing.log", also_print=True) info_to_log = "hello there world!!!" log.info(info_to_log) log.close() # Nothing bad happened and stdout is hard to capture reliably with open(log.log_file.name) as f: self.assertTrue(info_to_log in f.readline())
def test_custom_event(self): logger = Diary(self.API_DIR, file_name="UserEvents.txt", event=UserEvent) logger.log("Start logging") logger.info(UserEvent(self.INFO, user_name="admin")) # Directly log events logger.close() with open(logger.log_file.name) as f: contents = f.read() self.assertTrue("Start logging" in contents) self.assertTrue(logger.last_logged_event.formatted() in contents) with DiaryDB(logger.db_file.name) as db: db.assert_event_logged(self.INFO, "INFO")
def test_levels_setting_levels(self): log = Diary(self.INIT_DIR, db_name="levels.db", async=False) e = Event(self.INFO, level="") log.info(e) self.assertIs(e.level, levels.info) log.warn(e) self.assertIs(e.level, levels.warn) log.error(e) self.assertIs(e.level, levels.error) log.debug(e) self.assertIs(e.level, levels.debug) log.close() with DiaryDB(log.db_file.name) as db: db.assert_event_logged(self.INFO, level="INFO", limit=4) db.assert_event_logged(self.INFO, level="WARN", limit=4) db.assert_event_logged(self.INFO, level="ERROR", limit=4) db.assert_event_logged(self.INFO, level="DEBUG", limit=4)
INSERT INTO files(inputDT, level, info, path, success) VALUES(?, ?, ?, ?, ?)''', (event.dt, event.level_str, event.info, event.path, event.success)) logger = Diary("file_info", db_name="file_processes.db", db=FileProcessDB, file_name="file_processes.log") target_dir = "data" # files_to_process = os.listdir(target_dir) files_to_process = range(10) # We will pretend the numbers up to 10 are files for f in files_to_process: if process_file(f) == 1: e = FileProcessEvent("Success!", 1, f) logger.info(e) elif process_file(f) == 2: e = FileProcessEvent("The goal was not achieved", 2, f) logger.warn(e) elif process_file(f) == 3: e = FileProcessEvent("An error occurred", 3, f) logger.error(e) else: e = FileProcessEvent("Could not process file", -1, f) logger.debug(e) logger.close() with DiaryDB(logger.db_file.name) as db: entries = db.cursor.execute('''
def test_init_new_db(self): log = Diary(self.NEW_DB_PATH, async=False) log.info(self.INFO) log.close() with DiaryDB(self.NEW_DB_PATH) as db: db.assert_event_logged(self.INFO, level="INFO", limit=1)
def test_init_no_ext(self): log = Diary(self.NO_EXT_PATH, async=False) log.info(self.INFO) log.close() with open(self.NO_EXT_PATH) as f: self.assertTrue(self.INFO in f.readline())
def test_info(self): DB_NAME = 'levels.db' log = Diary(os.path.join(self.INIT_DIR), async=False, db_name=DB_NAME) log.info(self.INFO) log.logdb.assert_event_logged(self.INFO, "INFO", 1) log.close()