autograder.model.log

 1import typing
 2
 3import edq.util.time
 4import lms.model.base
 5
 6LOG_LEVEL_TEXT_TO_INT: typing.Dict[str, int] = {
 7    'TRACE': -20,
 8    'DEBUG': -10,
 9    'INFO': 0,
10    'WARN': 10,
11    'ERROR': 20,
12    'FATAL': 30,
13    'OFF': 100,
14}
15""" Map text API log levels to int values. """
16
17LOG_LEVEL_INT_TO_TEXT: typing.Dict[int, str] = {value: key for (key, value) in LOG_LEVEL_TEXT_TO_INT.items()}
18""" Map int API log levels to text. """
19
20class LogRecord(lms.model.base.BaseType):
21    """
22    A logging record.
23    """
24
25    CORE_FIELDS = [
26        'timestamp',
27        'level',
28        'message',
29        'error',
30        'course',
31        'assignment',
32        'user',
33    ]
34
35    def __init__(self,
36            level: str,
37            timestamp: edq.util.time.Timestamp,
38            message: typing.Union[str, None] = None,
39            error: typing.Union[str, None] = None,
40            course: typing.Union[str, None] = None,
41            assignment: typing.Union[str, None] = None,
42            user: typing.Union[str, None] = None,
43            attributes: typing.Union[typing.Dict[str, typing.Any], None] = None,
44            **kwargs: typing.Any) -> None:
45        super().__init__(**kwargs)
46
47        self.level: str = level
48        """ Logging level. """
49
50        self.timestamp: edq.util.time.Timestamp = timestamp
51        """ Log time. """
52
53        self.message: typing.Union[str, None] = message
54        """ Log message. """
55
56        self.error: typing.Union[str, None] = error
57        """ Log error. """
58
59        self.course: typing.Union[str, None] = course
60        """ Log course. """
61
62        self.assignment: typing.Union[str, None] = assignment
63        """ Log assignment. """
64
65        self.user: typing.Union[str, None] = user
66        """ Log user. """
67
68        if (attributes is None):
69            attributes = {}
70
71        self.attributes: typing.Dict[str, typing.Any] = attributes
72        """ Additional attributes attatched to the logging record. """
73
74    @classmethod
75    def from_api(cls, data: typing.Dict[str, typing.Any]) -> 'LogRecord':
76        """ Convert a dict coming from autograder API JSON. """
77
78        data = data.copy()
79
80        data['level'] = LOG_LEVEL_INT_TO_TEXT[data['level']]
81        data['timestamp'] = edq.util.time.Timestamp(data['timestamp'])
82
83        return LogRecord(**data)
LOG_LEVEL_TEXT_TO_INT: Dict[str, int] = {'TRACE': -20, 'DEBUG': -10, 'INFO': 0, 'WARN': 10, 'ERROR': 20, 'FATAL': 30, 'OFF': 100}

Map text API log levels to int values.

LOG_LEVEL_INT_TO_TEXT: Dict[int, str] = {-20: 'TRACE', -10: 'DEBUG', 0: 'INFO', 10: 'WARN', 20: 'ERROR', 30: 'FATAL', 100: 'OFF'}

Map int API log levels to text.

class LogRecord(lms.model.base.BaseType):
21class LogRecord(lms.model.base.BaseType):
22    """
23    A logging record.
24    """
25
26    CORE_FIELDS = [
27        'timestamp',
28        'level',
29        'message',
30        'error',
31        'course',
32        'assignment',
33        'user',
34    ]
35
36    def __init__(self,
37            level: str,
38            timestamp: edq.util.time.Timestamp,
39            message: typing.Union[str, None] = None,
40            error: typing.Union[str, None] = None,
41            course: typing.Union[str, None] = None,
42            assignment: typing.Union[str, None] = None,
43            user: typing.Union[str, None] = None,
44            attributes: typing.Union[typing.Dict[str, typing.Any], None] = None,
45            **kwargs: typing.Any) -> None:
46        super().__init__(**kwargs)
47
48        self.level: str = level
49        """ Logging level. """
50
51        self.timestamp: edq.util.time.Timestamp = timestamp
52        """ Log time. """
53
54        self.message: typing.Union[str, None] = message
55        """ Log message. """
56
57        self.error: typing.Union[str, None] = error
58        """ Log error. """
59
60        self.course: typing.Union[str, None] = course
61        """ Log course. """
62
63        self.assignment: typing.Union[str, None] = assignment
64        """ Log assignment. """
65
66        self.user: typing.Union[str, None] = user
67        """ Log user. """
68
69        if (attributes is None):
70            attributes = {}
71
72        self.attributes: typing.Dict[str, typing.Any] = attributes
73        """ Additional attributes attatched to the logging record. """
74
75    @classmethod
76    def from_api(cls, data: typing.Dict[str, typing.Any]) -> 'LogRecord':
77        """ Convert a dict coming from autograder API JSON. """
78
79        data = data.copy()
80
81        data['level'] = LOG_LEVEL_INT_TO_TEXT[data['level']]
82        data['timestamp'] = edq.util.time.Timestamp(data['timestamp'])
83
84        return LogRecord(**data)

A logging record.

LogRecord( level: str, timestamp: edq.util.time.Timestamp, message: Optional[str] = None, error: Optional[str] = None, course: Optional[str] = None, assignment: Optional[str] = None, user: Optional[str] = None, attributes: Optional[Dict[str, Any]] = None, **kwargs: Any)
36    def __init__(self,
37            level: str,
38            timestamp: edq.util.time.Timestamp,
39            message: typing.Union[str, None] = None,
40            error: typing.Union[str, None] = None,
41            course: typing.Union[str, None] = None,
42            assignment: typing.Union[str, None] = None,
43            user: typing.Union[str, None] = None,
44            attributes: typing.Union[typing.Dict[str, typing.Any], None] = None,
45            **kwargs: typing.Any) -> None:
46        super().__init__(**kwargs)
47
48        self.level: str = level
49        """ Logging level. """
50
51        self.timestamp: edq.util.time.Timestamp = timestamp
52        """ Log time. """
53
54        self.message: typing.Union[str, None] = message
55        """ Log message. """
56
57        self.error: typing.Union[str, None] = error
58        """ Log error. """
59
60        self.course: typing.Union[str, None] = course
61        """ Log course. """
62
63        self.assignment: typing.Union[str, None] = assignment
64        """ Log assignment. """
65
66        self.user: typing.Union[str, None] = user
67        """ Log user. """
68
69        if (attributes is None):
70            attributes = {}
71
72        self.attributes: typing.Dict[str, typing.Any] = attributes
73        """ Additional attributes attatched to the logging record. """
CORE_FIELDS = ['timestamp', 'level', 'message', 'error', 'course', 'assignment', 'user']

The common fields shared across backends for this type that are used for comparison and other operations. Child classes should set this to define how comparisons are made.

level: str

Logging level.

timestamp: edq.util.time.Timestamp

Log time.

message: Optional[str]

Log message.

error: Optional[str]

Log error.

course: Optional[str]

Log course.

assignment: Optional[str]

Log assignment.

user: Optional[str]

Log user.

attributes: Dict[str, Any]

Additional attributes attatched to the logging record.

@classmethod
def from_api(cls, data: Dict[str, Any]) -> LogRecord:
75    @classmethod
76    def from_api(cls, data: typing.Dict[str, typing.Any]) -> 'LogRecord':
77        """ Convert a dict coming from autograder API JSON. """
78
79        data = data.copy()
80
81        data['level'] = LOG_LEVEL_INT_TO_TEXT[data['level']]
82        data['timestamp'] = edq.util.time.Timestamp(data['timestamp'])
83
84        return LogRecord(**data)

Convert a dict coming from autograder API JSON.