Skip to content

Use logger instead of print #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions django_print_sql/print_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.db.models.sql.compiler import SQLCompiler
from functools import wraps
from contextlib import contextmanager
import logging
logger = logging.getLogger(__name__)

try:
import sqlparse
Expand All @@ -18,7 +20,7 @@ def format(statement, *args, **kwargs):

def pprint_sql(query):
statement = query[0] % query[1]
print(sqlparse.format(statement, reindent=True, keyword_case='upper'))
logger.debug(sqlparse.format(statement, reindent=True, keyword_case='upper'))


original_execute_sql = SQLCompiler.execute_sql
Expand All @@ -38,7 +40,7 @@ def execute_sql(self, *args, **kwargs):
shared_var['total_time'] += time_elapsed
if not count_only:
pprint_sql(self.as_sql())
print('[Time elapsed: {time:.2f}ms]\n'.format(time=time_elapsed * 1000))
logger.debug('[Time elapsed: {time:.2f}ms]\n'.format(time=time_elapsed * 1000))
return ret

# monkey patching the SQLCompiler
Expand All @@ -49,7 +51,7 @@ def execute_sql(self, *args, **kwargs):
# restore original execute_sql
SQLCompiler.execute_sql = original_execute_sql

print('[{num_of_queries} {query_word} executed, total time elapsed {time:.2f}ms]\n'.format(
logger.debug('[{num_of_queries} {query_word} executed, total time elapsed {time:.2f}ms]\n'.format(
num_of_queries=shared_var['count'],
query_word='query' if shared_var['count'] == 1 else 'queries',
time=shared_var['total_time'] * 1000
Expand Down