
Question:
Software
<ul><li>Django 1.9</li> <li>Python 3.4</li> </ul>
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
<strong>What went wrong?</strong>
Answer1:You might need to set logging.basicConfig(level=logging.INFO)
before you call logging.getLogger(__name__)
.
You can check out the <a href="https://docs.python.org/3.4/howto/logging-cookbook.html" rel="nofollow">Logging Cookbook</a> for a detailed guide.
Answer2:I belive that what you are seeing are actually logs from <strong>django.db.backends</strong>, checkout <a href="https://docs.djangoproject.com/en/1.10/topics/logging/#django-db-backends" rel="nofollow">this reference</a>.
<blockquote>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:
<strong>duration</strong>: The time taken to execute the SQL statement. <br /><strong>sql</strong>: The SQL statement that was executed. <br /><strong>params</strong>: 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.
</blockquote>You are logging with level <strong>log level info</strong> meanwhile <strong>django.db.backends</strong> logs at <strong>DEBUG</strong> level so checkout also your log handlers to be sure what you are actually logging and what you are not :)
Hope it helps!