Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

""" 

Handle logging of the application stuff to the database 

 

This will be replaced by something outside the app at some point, so the realy 

code should all be in /lib/applogging.py vs in here. This is only the db store 

side 

 

""" 

from datetime import datetime 

from datetime import timedelta 

 

from sqlalchemy import Column 

from sqlalchemy import DateTime 

from sqlalchemy import func 

from sqlalchemy import Integer 

from sqlalchemy import Unicode 

from sqlalchemy import UnicodeText 

 

from bookie.models import Base 

from bookie.models import DBSession 

 

 

class AppLogMgr(object): 

 

    @staticmethod 

    def store(**kwargs): 

        """Store a new log record to the db""" 

        stored = AppLog(**kwargs) 

        DBSession.add(stored) 

 

    @staticmethod 

    def find(days=1, message_filter=None, status=None): 

        """Find a set of app log records based on predefined filters.""" 

        qry = AppLog.query 

        if status is not None: 

            qry = qry.filter(AppLog.status == status) 

 

        if message_filter: 

            mfilter = '%{0}%'.format(message_filter) 

            qry = qry.filter(func.lower(AppLog.message).like(mfilter)) 

 

        now = datetime.utcnow() 

        limit = now - timedelta(days=days) 

        qry = qry.filter(AppLog.tstamp > limit) 

 

        return qry.order_by(AppLog.tstamp.desc()).all() 

 

 

class AppLog(Base): 

    __tablename__ = 'logging' 

 

    id = Column(Integer, autoincrement=True, primary_key=True) 

    user = Column(Unicode(255), nullable=False) 

    component = Column(Unicode(50), nullable=False) 

    status = Column(Unicode(10), nullable=False) 

    message = Column(Unicode(255), nullable=False) 

    payload = Column(UnicodeText) 

    tstamp = Column(DateTime, default=datetime.utcnow)