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)
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)
from diary import Diary import os import time def is_down(website, timeout=10): response = os.system('ping -c 1 -w {timeout} {website}'.format( timeout=timeout, website=website )) if response == 0: return False return True # Create a logger with an output file logger = Diary("google_status.txt") # If a logger should point to a db give it a db # logger = Diary("status.db") while True: if is_down("google.com"): logger.error("GOOGLE IS DOWN!") else: logger.log("Google is up.") time.sleep(5)
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_raises_specific(self): log = Diary(self.ERRORS_LOG_PATH) with self.assertRaises(AssertionError, msg="ERROR"): log.error("ERROR", raises=True, e_type=AssertionError)
def test_error_raises(self): log = Diary(self.ERRORS_LOG_PATH) with self.assertRaises(Exception, msg="ERROR"): log.error("ERROR", raises=True)
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()