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

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

""" 

Handle application logging items 

 

Current db model: 

 

id, user, component, status, message, payload, tstamp 

 

""" 

import json 

import logging 

from bookie.models.applog import AppLogMgr 

 

LOG = logging.getLogger(__name__) 

 

 

class Log(object): 

    """Log handler""" 

 

    # status levels 

    ERROR = 0 

    WARNING = 1 

    INFO = 2 

    DEBUG = 3 

 

    @staticmethod 

    def store(status, message, **kwargs): 

        """Store a log item""" 

        LogRecord(status, message, **kwargs) 

 

 

class AuthLog(Log): 

    """Store auth specific log items""" 

    component = "AUTH" 

 

    @staticmethod 

    def login(username, success, password=None): 

        """Store that a user logged into the system""" 

        get_status = lambda x: Log.INFO if x else Log.ERROR 

        passwd = lambda x: None if password is None else {'password': password} 

 

        status = get_status(success) 

        message = "User {0} attempted to login {1}".format(username, 

                                                           success) 

 

        data = { 

            'user': username, 

            'component': AuthLog.component, 

            'payload': passwd(password) 

        } 

 

        AuthLog.store(status, message, **data) 

 

    @staticmethod 

    def disabled(username): 

        """Attempt to log into a disabled account""" 

        msg = "{0} is a disabled user account".format(username) 

 

        data = { 

            'user': username, 

            'component': AuthLog.component 

        } 

 

        AuthLog.store(Log.INFO, msg, **data) 

 

    @staticmethod 

    def reactivate(username, success=True, code=None): 

        """The account was marked for reactivation""" 

        if success: 

            msg = "{0} was reactivated".format(username) 

        else: 

            msg = "{0} attempted to reactivate with invalid credentials" 

            msg = msg.format(username) 

 

        LOG.debug(msg) 

        data = { 

            'user': username, 

            'component': AuthLog.component, 

            'payload': { 

                'success': success, 

                'code': code, 

            } 

        } 

 

        AuthLog.store(Log.INFO, msg, **data) 

 

 

class BmarkLog(Log): 

    """Bookmark specific log items""" 

    component = "BMARKS" 

 

    @staticmethod 

    def export(for_user, current_user): 

        """Note that a user has exported their bookmarks""" 

        get_status = lambda x: Log.WARNING if x else Log.INFO 

 

        your_export = False 

        if current_user and current_user == for_user: 

            your_export = True 

 

        elif current_user is None: 

            current_user = "None" 

 

        status = get_status(your_export) 

        message = "User {0} exported the bookmarks for {1}".format( 

            current_user, for_user) 

 

        data = { 

            'user': current_user, 

            'component': BmarkLog.component, 

        } 

 

        BmarkLog.store(status, message, **data) 

 

 

class LogRecord(object): 

    """A record in the log""" 

 

    def __init__(self, status, message, **kwargs): 

        """A record in the log""" 

        kwargs['status'] = status 

        kwargs['message'] = message 

 

        # we need to hash down the payload if there is one 

        if 'payload' in kwargs and kwargs['payload'] is not None: 

            kwargs['payload'] = json.dumps(dict(kwargs.get('payload'))) 

 

        AppLogMgr.store(**kwargs) 

 

 

class SignupLog(object): 

    """Signup Log records.""" 

 

    def __init__(self, status, message, **kwargs): 

        """A record in the log""" 

        kwargs['status'] = status 

        kwargs['message'] = message 

 

        # we need to hash down the payload if there is one 

        if 'payload' in kwargs and kwargs['payload'] is not None: 

            kwargs['payload'] = json.dumps(dict(kwargs.get('payload'))) 

 

        AppLogMgr.store(**kwargs)