class OpenEdXAppServerLogSerializer(serializers.ModelSerializer): """ Provide the log entries for an OpenEdXAppServer """ log_entries = LogEntrySerializer(many=True, read_only=True) log_error_entries = LogEntrySerializer(many=True, read_only=True) class Meta: model = OpenEdXAppServer fields = ('log_entries', 'log_error_entries')
def emit(self, record): """ Handles an emitted log entry and stores it in the database, optionally linking it to the model object `obj` """ obj = record.__dict__.get('obj', None) if obj is None or not isinstance(obj, models.Model) or obj.pk is None: content_type, object_id = None, None else: content_type = apps.get_model('contenttypes', 'ContentType').objects.get_for_model(obj) object_id = obj.pk try: log_entry = apps.get_model('instance', 'LogEntry').objects.create( level=record.levelname, text=self.format(record), content_type=content_type, object_id=object_id ) except ProgrammingError: # This can occur if django tries to log something before migrations have created the log table. # Make sure that is actually what happened: assert 'instance_logentry' not in connection.introspection.table_names() # Send notice of entries related to any resource. Skip generic log entries that occur # in debug mode, like "GET /static/img/favicon/favicon-96x96.png": if content_type: log_event = { 'type': 'object_log_line', 'log_entry': LogEntrySerializer(log_entry).data } if hasattr(obj, 'event_context'): log_event.update(obj.event_context) # TODO: Filter out log entries for which the user doesn't have view rights # TODO: More targetted events - only emit events for what the user is looking at publish_data('log', log_event)
def emit(self, record): """ Handles an emitted log entry and stores it in the database, optionally linking it to the model object `obj` """ obj = record.__dict__.get('obj', None) if obj is None or not isinstance(obj, models.Model) or obj.pk is None: log_entry_set = apps.get_model('instance', 'GeneralLogEntry').objects else: log_entry_set = obj.log_entry_set log_entry = log_entry_set.create(level=record.levelname, text=self.format(record)) log_event = { 'type': 'instance_log', 'log_entry': LogEntrySerializer(log_entry).data } if hasattr(obj, 'event_context'): log_event.update(obj.event_context) # TODO: Filter out log entries for which the user doesn't have view rights publish_data('log', log_event)
class InstanceLogSerializer(serializers.ModelSerializer): """ Provide the log entries for an Instance """ log_entries = LogEntrySerializer(many=True, read_only=True) class Meta: model = InstanceReference fields = ('log_entries', )
def to_representation(self, obj): """ Add additional fields/data to the output """ output = super().to_representation(obj) output['instance_type'] = obj.instance_type.model details = self.serialize_details(obj.instance) # Merge instance details into the resulting dict, but never overwrite existing fields for key, val in details.items(): output.setdefault(key, val) if not self.summary_only: # Add log entries: output['log_entries'] = [ LogEntrySerializer(entry).data for entry in obj.instance.log_entries ] return output