V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kingofvir
V2EX  ›  Django

django mysql db 日志无输出

  •  2
     
  •   kingofvir · 2019-04-28 16:47:49 +08:00 · 2628 次点击
    这是一个创建于 1817 天前的主题,其中的信息可能已经有所发展或是发生改变。

    django logging 配置:

    
    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'first_formatter': {
                'format': '%(levelname)s [%(asctime)s] [%(name)s:%(module)s:%(funcName)s:%(lineno)s] [%(exc_info)s] %(message)s',
                'datefmt': '%Y-%m-%d %H:%M:%S'
            },
        },
        'filters': {
            'DebugFalse': {
                '()': 'django.utils.log.RequireDebugFalse',
            },
            'DebugTrue': {
                '()': 'django.utils.log.RequireDebugTrue',
            },
        },
        'handlers': {
    
            'console': {
                'level': 'DEBUG',
                'filters': ['DebugTrue'],
                'class': 'logging.StreamHandler',
                'formatter': 'first_formatter'
            },
            'db': {
                'level': 'DEBUG',
                'class': 'logging.handlers.TimedRotatingFileHandler',
                'filename': '{0}/logs/db.log'.format(BASE_DIR),
                'formatter': 'first_formatter',
                'when': 'midnight'
            },
    
        },
        'loggers': {
             'django.db': {
                 'handlers': ['console', 'db'],
                 'level': 'DEBUG',
                 'propagate': False,
             }
        }
    }
    

    在启用 sqlite 作为数据库的时候 的时候执行有 sql 语句输出

    DEBUG [2019-04-28 16:43:19] [django.db.backends:utils:execute:111] [None] (0.000) SELECT "books_book"."id", "books_book"."book_id", "books_book"."name", "books_book"."author", "books_book"."author_re_id", "books_book"."price", "books_book"."publish", "books_book"."score", "books_book"."cover", "books_book"."url", "books_book"."introduction", "books_book"."create_time" FROM "books_book" WHERE "books_book"."name" LIKE '%少年%' ESCAPE '\'  LIMIT 21; args=('%少年%',)
    
    

    在采用 mysql 数据库的时候无具体 SQL 输出

    DEBUG [2019-04-28 16:43:29] [django.db.backends:utils:execute:111] [None] (0.006) None; args=('%少年%',)
    
    5 条回复    2019-04-28 19:55:29 +08:00
    kingofvir
        1
    kingofvir  
    OP
       2019-04-28 17:52:04 +08:00
    django2.1.4
    est
        2
    est  
       2019-04-28 17:57:34 +08:00
    getLogger 看一下 config。
    kingofvir
        3
    kingofvir  
    OP
       2019-04-28 19:43:21 +08:00
    看了 logging 源码,
    ```bash
    def last_executed_query(self, cursor, sql, params):
    # With MySQLdb, cursor objects have an (undocumented) "_last_executed"
    # attribute where the exact query sent to the database is saved.
    # See MySQLdb/cursors.py in the source distribution.
    return force_text(getattr(cursor, '_last_executed', None), errors='replace')
    ```
    需要使用 MySQLdb 游标为注明的 _last_executed 熟悉,安装了 MySQLdb, 并且在配置文件中的数据库定义前设置 install_as_MySQLdb 解决了。

    ```bash
    # import pymysql
    # pymysql.install_as_MySQLdb()
    ```
    这里还有个问题

    site-packages/django/db/backends/mysql/base.py 第 37 行中有个版本检测,解决方式
    1、升级到对应的 Database 版本
    2、修改源码注释掉,(懒得升级,我注释掉了)


    问题解决
    kingofvir
        4
    kingofvir  
    OP
       2019-04-28 19:44:45 +08:00
    @kingofvir 升级到对应的 MySQLdb 版本,它 import MySQLdb as Database
    kingofvir
        5
    kingofvir  
    OP
       2019-04-28 19:55:29 +08:00
    @est 突然发现不必这么复杂,直接安装最新 MySQLdb 即可
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   960 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 20:48 · PVG 04:48 · LAX 13:48 · JFK 16:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.