logging does not show the result of array

天大地大妈咪最大 提交于 2019-12-25 08:50:03

问题


Software

  • Django 1.9
  • Python 3.4

What did I do?

I have the following Django code in my views.py

from django.db import connection
    cursor = connection.cursor()
    cursor.execute('SELECT p.name, p.name_zh_hans, p.art_number, ....')
    rows = cursor.fetchall()

logger = logging.getLogger(__name__)

for row in rows:
        row_num += 1
        logger.info(row)
        for col_num in range(len(row)):
            ws.write(row_num, col_num, row[col_num], font_style)

What did I get inside the log file?

(0.005) SELECT p.name, p.name_zh_hans, p.art_number, ....; args=None
(0.005) SELECT p.name, p.name_zh_hans, p.art_number, ....; args=None
(0.006) SELECT p.name, p.name_zh_hans, p.art_number, ....; args=None

What did I expect?

Display of the array contents inside the log file

What went wrong?


回答1:


You might need to set logging.basicConfig(level=logging.INFO) before you call logging.getLogger(__name__).

You can check out the Logging Cookbook for a detailed guide.




回答2:


I belive that what you are seeing are actually logs from django.db.backends, checkout this reference.

Messages relating to the interaction of code with the database. For example, every application-level SQL statement executed by a request is logged at the DEBUG level to this logger.

Messages to this logger have the following extra context:

duration: The time taken to execute the SQL statement.
sql: The SQL statement that was executed.
params: The parameters that were used in the SQL call.

For performance reasons, SQL logging is only enabled when settings.DEBUG is set to True, regardless of the logging level or handlers that are installed.

This logging does not include framework-level initialization (e.g. SET TIMEZONE) or transaction management queries (e.g. BEGIN, COMMIT, and ROLLBACK). Turn on query logging in your database if you wish to view all database queries.

You are logging with level log level info meanwhile django.db.backends logs at DEBUG level so checkout also your log handlers to be sure what you are actually logging and what you are not :)

Hope it helps!



来源:https://stackoverflow.com/questions/40582796/logging-does-not-show-the-result-of-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!