django mysql db 日志无输出 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kingofvir
V2EX    Django

django mysql db 日志无输出

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

    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     Solana     919 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 23:07 PVG 07:07 LAX 15:07 JFK 18:07
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86