logging does not show the result of array

2019-09-06 09:11发布

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?

2条回答
劳资没心,怎么记你
2楼-- · 2019-09-06 09:51

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.

查看更多
贼婆χ
3楼-- · 2019-09-06 09:53

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!

查看更多
登录 后发表回答