django mysql db 日志无输出

2019-04-28 16:47:49 +08:00
 kingofvir

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=('%少年%',)
2641 次点击
所在节点    Django
5 条回复
kingofvir
2019-04-28 17:52:04 +08:00
django2.1.4
est
2019-04-28 17:57:34 +08:00
getLogger 看一下 config。
kingofvir
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
2019-04-28 19:44:45 +08:00
@kingofvir 升级到对应的 MySQLdb 版本,它 import MySQLdb as Database
kingofvir
2019-04-28 19:55:29 +08:00
@est 突然发现不必这么复杂,直接安装最新 MySQLdb 即可

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/559521

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX