Exemplo n.º 1
0
    def test_unicode_PY2_DB_error(self):
        if not _PY2:
            return

        unicode_str =  u"\u3002"
        log = Diary(self.INIT_DIR, async=False, file_name="unicode_test.log", db_name="unicode.db")
        with self.assertRaises(ValueError, msg="diary does not support logging unicode strings into a database in python2"):
            log.log(unicode_str)
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
    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
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
    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")
Exemplo n.º 7
0
    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")
Exemplo n.º 8
0
    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())
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
    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)
Exemplo n.º 11
0
    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)
Exemplo n.º 12
0
    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())
Exemplo n.º 13
0
    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)
Exemplo n.º 14
0
    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)
Exemplo n.º 15
0
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)
Exemplo n.º 16
0
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())
Exemplo n.º 17
0
 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')