使用django在数据库中保存异常消息

时间:2013-12-05 13:28:47

标签: django

我想使用django将我的异常存储在数据库中。  我怎么能做到这一点。是否有任何libaray来执行此任务以将异常存储在数据库中。

2 个答案:

答案 0 :(得分:0)

将您的例外定义为Django模型。

答案 1 :(得分:0)

最近我完成了你想要的一个项目。

我们有一个非Django Python引擎的网站。我创建了一个简单的Django项目

  1. 通过http请求接收有关新异常的信息。基础项目中有一个全局尝试捕获。如果发生异常,则http请求将发送异常详细信息。因此,我们需要一个特殊的视图来接收有关新例外的信息。
  2. 显示有关例外组和例外的信息。 例外组是一组类似的例外情况。如果例外具有非常相似的追溯,则例外情况属于同一组。我使用下一个函数来检查组中的异常:

    def are_slightly_diff( s1, s2 ):
        """ Returns True if s1 is similar to s2. 
            s1 and s2 are several strings in one (slashn-separated). """
    
        similar_count = 0
    
        s2_list = list( s2 )
    
        for ch in s1:
            try:            
                i_s2 = s2_list.index( ch )
            except ValueError:
                pass
            else:
            del s2_list[ i_s2 ]
    
        len_s2_list = len( s2_list )
    
        max_diff = int( len( s1 ) * 0.1 )
    
        if max_diff < 1:
            max_diff = 1
    
        return len_s2_list <= max_diff
    
  3. 所以我有两个模型 - 一个用于例外组:

    class ExceptionItemGroup( models.Model ):
    
        _NEW_GROUP_NAME = 'Exceptions {0}'
    
        name = models.CharField( max_length = 512 )
        last_line = models.TextField() # matching template
        prev_lines = models.TextField()
    
        created_dt = models.DateTimeField( auto_now_add = True )
        last_modified_dt = models.DateTimeField( auto_now = True )
    
        def rename( self, name ):
            self.name = name
    
        @staticmethod
        def find( cur_lines, site_id ):
            """ Find an exception group corresponding to the exception item. 
                Exc_item must contain formalized exception. """
    
            # 1) Check prev_lines
            cur_prev_lines = cur_lines[ : -1 ]
    
            cur_last_line = cur_lines[ -1 ]
    
            similar_groups = ExceptionItemGroup.objects.filter( site_id = site_id, prev_lines = list2line( cur_prev_lines ) )
    
            for sim_g in similar_groups:        
                if are_slightly_diff( cur_last_line, sim_g.last_line ):
                    return sim_g # group was found        
            return None # group was not found
    
        @staticmethod
        def create_from_data( site_id, exc_lines, name ):
            " Use this method if a group was not found. "        
    
            with transaction.commit_on_success():
                g = ExceptionItemGroup( site_id = site_id, name = name )
    
                g.prev_lines = list2line( exc_lines[ : -1 ] )
                g.last_line = exc_lines[ -1 ]    
                g.save()
    
            return g
    
        def __unicode__( self ):
            return self.name
    
        class Meta:
            db_table = 'exc_item_group'
    

    另一个是组中的异常(因此,模型有外键):

    class ExceptionItem( models.Model ):
        group = models.ForeignKey( ExceptionItemGroup )
        title = models.TextField()
        body = models.TextField()
        created_dt = models.DateTimeField( auto_now_add = True )
    
        @staticmethod
        def create( group, title, html ):
    
            item = ExceptionItem( group = group, title = title, body = html )
            item.save()
    
        def __unicode__( self ):
            return self.title
    
        class Meta:
            db_table = 'exc_item'
    

    可以在异常之间导航:

    1. 包含例外组列表的视图
    2. 包含组中例外情况列表的视图
    3. 从基本项目中收​​到完整异常信息的视图。