Logging - logger、admin_log

logger的日志级别

  • DEBUG:用于调试目的的底层系统信息

  • INFO:普通的系统信息

  • WARNING:表示出现一个较小的问题。

  • ERROR:表示出现一个较大的问题。

  • CRITICAL:表示出现一个致命的问题。

logging的组成

  • 记录器 :loggers
  • 处理程序:handlers
  • 过滤器:filters
  • 格式化:formatters

admin_log

  • 表:django_admin_log

  • 根据LogEntry log_action()函数仿写一个admin_log.py,重新定义各个函数:

    source_code:
    
    ADDITION = 1
    CHANGE = 2
    DELETION = 3
    class LogEntryManager(models.Manager):
          use_in_migrations = True
    
    def log_action(self, user_id, content_type_id, object_id, object_repr, action_flag, change_message=''):
          e = self.model(
              None, None, user_id, content_type_id, smart_text(object_id),
              object_repr[:200], action_flag, change_message
          )
          e.save()
    
    write admin_log.py
    
    from django.contrib.contenttypes.models import ContentType
    from django.contrib.admin.models import LogEntry, ADDITION, CHANGE, DELETION
    from django.utils.encoding import force_text
    # 将任何对象转成字符串
    
    LOGIN = 4
    LOGOUT = 5
    
    ACTION_FLAG = ((ADDITION, u'增加'),\
                   (CHANGE, u'修改'),\
                   (DELETION, u'删除'),\
                   (LOGIN, u'登录'),\
                   (LOGOUT, u'注销'))
    
    def get_content_type_for_model(obj):
        """
        Takes either a model class or an instance of a model, and returns the ContentType instance representing that model. for_concrete_model=False allows fetching the ContentType of a proxy mod
        """
        return ContentType.objects.get_for_model(obj, for_concrete_model=False)
    
    def log_change(request, object, message):
        """
        Log that an object has been successfully changed.
    
        The default implementation creates an admin LogEntry object.
    
        example:
        change_message = '描述信息'
        log_change(request, object, change_message)
        """
        ip = request.META['REMOTE_ADDR']
        LogEntry.objects.log_action(
            user_id=request.user.pk,
            content_type_id=get_content_type_for_model(object).pk,
            object_id=object.pk,
            object_repr=force_text(object),
            action_flag=CHANGE,
            change_message=u'%s IP:%s' % (message, ip)
        )
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,365评论 19 139
  • 奇形怪状 当凝视的片刻被无限延长 风不吹,树影不动 汗水从发梢滴落 不安于现状的一颗牙齿 也许是将我隐匿的思念 埋...
    慕籽阅读 343评论 0 1
  • Why We Can’t Look Away From Our Screens(3) 欲罢不能:电子设备为什么让我...
    阿飞fighting阅读 224评论 0 0
  • 天气闷热得 东西要发霉 幸好有你在 带来了一抹清凉 眼里满含的泪水 化作了 甘霖降落 还好还好 没有发霉
    芥墨阅读 168评论 5 2
  • 2018年7月7日,看完电影《我不是药神》 “我只想活下去,抓了药贩子我们只能坐着等死;我又不是药神,我不想坐牢;...
    二哈T阅读 567评论 0 0

友情链接更多精彩内容