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)
def test_custom_db_formatted_event(self): logger = Diary(self.API_DIR, file_name="withdb.txt", db_name="user_events.db", db=UserActivityDB, event=UserEvent) logger.log("Starting app") event_to_log = UserEvent("Super user logged in", user_name="super") logger.debug(event_to_log) logger.close() with open(logger.log_file.name) as f: contents = f.read() self.assertTrue("Starting app" in contents) self.assertTrue(logger.last_logged_event.formatted() in contents) with UserActivityDB(logger.db_file.name) as db: entries = db.cursor.execute("""SELECT * FROM user_activity WHERE log=(?) AND level LIKE (?) AND user=(?)""", (event_to_log.info, event_to_log.level_str, event_to_log.user_name)) entry = entries.fetchone() self.assertEquals(entry[0], event_to_log.dt) self.assertEquals(entry[1], event_to_log.level_str) self.assertEquals(entry[2], event_to_log.info) self.assertEquals(entry[3], event_to_log.user_name)
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(''' SELECT * FROM files ''') for row in entries: print(row)
def test_debug(self): DB_NAME = 'levels.db' log = Diary(self.INIT_DIR, async=False, db_name=DB_NAME) log.debug(self.INFO) log.logdb.assert_event_logged(self.INFO, "DEBUG", 1) log.close()