def test_warn_log_trace(self): log = Diary(self.WARNINGS_LOG_PATH, async=False) log.warn(self.INFO, log_trace=True) log.close() with open(log.log_file.name) as f: self.assertTrue( "logged(event, *args, **kwargs)" in f.read())
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_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_log_event_instance(self): mock_level = "CRITICAL" log = Diary(self.INIT_DIR, db_name="events.db", async=False) e = Event(self.INFO, level=mock_level) log.log(e) self.assertEquals(e.level, mock_level) log.close() with DiaryDB(log.db_file.name) as db: db.assert_event_logged(self.INFO, level=mock_level)
def test_custom_format_init(self): logger = Diary(self.API_DIR, log_format=emergency_format, file_name="EmergencyLogs2.log", db_name="EmergencyDB2.db") logger.log(self.INFO) logger.close() with open(logger.log_file.name) as f: self.assertEquals(f.readline(), emergency_format(logger.last_logged_event) + '\n') with DiaryDB(logger.db_file.name) as db: db.assert_event_logged(self.INFO)
def test_queue_join(self): trials = 10 log = Diary(self.INIT_DIR, async=True, db_name="QUEUE_TEST.db") for i in range(trials): log.log(i) log.close() self.assertFalse(log.thread.is_alive()) with DiaryDB(log.db_file.name) as db: entries = db.cursor.execute("SELECT * FROM logs") self.assertEquals(len(entries.fetchall()), trials)
def test_log_event_formatted(self): log = Diary(self.INIT_DIR, file_name="formatted.log", async=False) e = Event(self.INFO, "LEVEL") e.set_formatter("{info}|{level}") log.log(e) log.close() with open(log.log_file.name) as f: self.assertEquals("{info}|{level}\n".format(info=self.INFO, level="LEVEL"), f.readline()) e.set_formatter(None) # Set Event formatter back to None to not upset other tests
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_log_event_in_init(self): class PrettyEvent(Event): formatter = "{info}|{level_str}" log = Diary(self.INIT_DIR, file_name="pretty.log", db_name="prettyevents.db", async=False, event=PrettyEvent) log.log(self.INFO) log.close() with DiaryDB(log.db_file.name) as db: db.assert_event_logged(self.INFO) with open(log.log_file.name) as f: self.assertEquals("{info}|{level}\n".format(info=self.INFO, level="INFO"), f.readline())
def test_custom_format_event(self): class FormattedEvent(Event): formatter = "|{dt}|{info}|{level_str}|" logger = Diary(self.API_DIR, file_name="formatted.txt", db_name="formattedDB.db", event=FormattedEvent, async=False) logger.log(self.INFO) logger.close() with open(logger.log_file.name) as f: self.assertEquals(f.readline(), logger.last_logged_event.formatted() + '\n') with DiaryDB(logger.db_file.name) as db: db.assert_event_logged(self.INFO, "INFO")
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_unicode_PY2(self): if not _PY2: return unicode_str = u"\u3002" log = Diary(os.path.join(self.INIT_DIR, "unicode_test.log"), async=False, encoding="utf-8") log.log(unicode_str) log.close() with codecs.open(log.log_file.name, encoding=log.encoding) as f: line = f.readline() self.assertTrue(unicode_str in line)
def test_unicode_PY3(self): if _PY2: return unicode_str = u"\u3002" log = Diary(self.INIT_DIR, file_name="unicode_test.log", async=False) log.log(unicode_str) log.close() with codecs.open(log.log_file.name, encoding=log.encoding) as f: line = f.readline() self.assertTrue(unicode_str in line)
def test_unicode_event_formatted(self): class PrettyEvent(Event): formatter = "{info}|{level_str}" unicode_str = u"\u3002" log = Diary(os.path.join(self.INIT_DIR, "unicode_test.log"), async=False, encoding="utf-8", event=PrettyEvent) log.log(unicode_str) log.close() with codecs.open(log.log_file.name, encoding=log.encoding) as f: line = f.readline() self.assertTrue(unicode_str in line)
def test_log(self): FILE_NAME = "test_log.txt" log = Diary(self.INIT_DIR, async=False, file_name=FILE_NAME) self.assertTrue(exists_with_ext(os.path.join( self.INIT_DIR, FILE_NAME ), '.txt') ) log.log(self.INFO) log.logdb.assert_event_logged(self.INFO, level="INFO", limit=1) log.close() self.assertEquals(os.path.split(log.log_file.name)[-1], FILE_NAME) with open(os.path.join(self.INIT_DIR, FILE_NAME)) as f: self.assertTrue(self.INFO in f.readline())
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_everything(self): logger = Diary(self.API_DIR, file_name="withlevel.txt", db_name="level_user_events.db", db=UserActivityDB, event=UserEvent) event_to_log = UserEvent(self.INFO, user_name="super") logger.log(event_to_log, level=critical) logger.close() with open(logger.log_file.name) as f: self.assertTrue(event_to_log.formatted() + '\n', f.readline()) 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)
def test_write(self): FILE_NAME = "test_write.txt" log = Diary(self.INIT_DIR, async=False, file_name=FILE_NAME) simple_event = Event(self.INFO, "LEVEL") self.assertTrue(exists_with_ext(os.path.join( self.INIT_DIR, FILE_NAME ), '.txt') ) log._write(simple_event) log.logdb.assert_event_logged(self.INFO, level="LEVEL") log.close() self.assertEquals(os.path.split(log.log_file.name)[-1], FILE_NAME) self.assertIs(log.last_logged_event, simple_event) with open(os.path.join(self.INIT_DIR, FILE_NAME)) as f: self.assertTrue(self.INFO in f.readline())
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)
def test_warn(self): DB_NAME = 'levels.db' log = Diary(os.path.join(self.INIT_DIR), async=False, db_name=DB_NAME) log.warn(self.INFO) log.logdb.assert_event_logged(self.INFO, "WARN", 1) log.close()
class TestDiaryThread(unittest.TestCase): TEST_DIR_PATH = os.path.join(os.path.dirname(__file__), 'testing_dir') INFO = "event was logged" TEMP_DB = os.path.join(TEST_DIR_PATH, "db_test.db") TEMP_FILE = os.path.join(TEST_DIR_PATH, "thread_test.txt") TRIALS = 3 count = 0 def setUp(self): self.log = Diary(self.TEST_DIR_PATH, file_name=self.TEMP_FILE, db_name=self.TEMP_DB, async=True) def tearDown(self): self.log.close() os.remove(self.TEMP_FILE) @classmethod def tearDownClass(cls): os.remove(cls.TEMP_DB) def test_constructor(self): self.assertIsNotNone(self.log.thread) self.assertTrue(self.log.thread.isDaemon()) self.assertTrue(self.log.thread.sets_db) self.assertIs(self.log.thread.diary, self.log) def test_join(self): returned = self.log.thread.join() self.assertIsNone(returned) def test_logs(self): self.log.log(self.INFO) self.log.close() with open(self.log.log_file.name) as f: self.assertTrue(self.INFO in f.readline()) with DiaryDB(self.log.db_file.name) as db: db.assert_event_logged(self.INFO) def test_timer_no_async(self): log = Diary(self.TEST_DIR_PATH, async=False) with self.assertRaises(RuntimeError, msg="In order to set a timer async must be enabled"): log.set_timer(100, lambda: None) def test_timer(self): def log_counter(): self.count += 1 self.log.log(self.INFO + str(self.count)) self.log.set_timer(1, log_counter) time.sleep(self.TRIALS + .1) self.log.close() with DiaryDB(self.log.db_file.name) as db: db.assert_event_logged(self.INFO + str(self.count)) with open(self.log.log_file.name) as f: for i in range(1, self.TRIALS + 1): self.assertTrue(self.INFO + str(i) in f.readline()) def test_timer_with_args(self): def log_arg(arg): self.log.log(arg) self.log.set_timer(1, log_arg, self.INFO) time.sleep(self.TRIALS + .1) self.log.close() with DiaryDB(self.log.db_file.name) as db: entries = db.cursor.execute( '''SELECT * FROM logs WHERE log=(?) AND level LIKE (?) ORDER BY inputDT ASC LIMIT (?)''', (self.INFO, '%', self.TRIALS)) for i in range(self.TRIALS): self.assertTrue(entries.fetchone()) with open(self.log.log_file.name) as f: self.assertTrue(self.INFO in f.readline()) def test_timer_with_kwargs(self): repeats = 10 def log_kwarg(s, repeats=2): self.log.log(s * repeats) self.log.set_timer(1, log_kwarg, self.INFO, repeats=repeats) time.sleep(self.TRIALS + .1) self.log.close() with DiaryDB(self.log.db_file.name) as db: entries = db.cursor.execute( '''SELECT * FROM logs WHERE log=(?) AND level LIKE (?) ORDER BY inputDT ASC LIMIT (?)''', (self.INFO * repeats, '%', self.TRIALS)) for i in range(self.TRIALS): self.assertTrue(entries.fetchone()) with open(self.log.log_file.name) as f: self.assertTrue(self.INFO * repeats in f.readline()) def test_timer_all_args(self): params = (self.INFO, "abc", "def", "123") def log_joined(*args): self.log.log(' '.join(args)) self.log.set_timer(1, log_joined, *params) time.sleep(self.TRIALS + .1) self.log.close() with DiaryDB(self.log.db_file.name) as db: entries = db.cursor.execute( '''SELECT * FROM logs WHERE log=(?) AND level LIKE (?) ORDER BY inputDT ASC LIMIT (?)''', (' '.join(params), '%', self.TRIALS)) for i in range(self.TRIALS): self.assertTrue(entries.fetchone()) with open(self.log.log_file.name) as f: self.assertTrue(' '.join(params) in f.readline())
def test_error_log_trace(self): log = Diary(self.ERRORS_LOG_PATH, async=False ) log.error("ERROR", log_trace=True) log.close() with open(log.log_file.name) as f: self.assertTrue('log.error("ERROR", log_trace=True)' in f.read())
def test_error(self): DB_NAME = 'levels.db' log = Diary(os.path.join(self.INIT_DIR), async=False, db_name=DB_NAME) log.error(self.INFO, log_trace=False) log.logdb.assert_event_logged(self.INFO, "ERROR", 1) log.close()
def test_custom_level(self): logger = Diary(os.path.join(self.API_DIR)) logger.log("URGENT ATTENTION NEEDED", level=critical) logger.close() with open(logger.log_file.name) as f: self.assertEquals(f.readline(), formats.standard(logger.last_logged_event) + '\n')
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()
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_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_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)