Skip to content

Commit

Permalink
Merge 8c9c4c8 into acb0868
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmits committed May 11, 2017
2 parents acb0868 + 8c9c4c8 commit a510d9a
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ docs/_build
# Editor
.idea

# Cache
.cache

5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
language: python

python:
- "3.6"
- "3.5"
- "3.4"
- "3.3"
- "2.7"

# command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
install: pip install -r requirements/test.txt

# command to run tests using coverage
script: make test
script: make test

# report coverage to coveralls.io
after_success: coveralls
3 changes: 2 additions & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ Contributors

* Liam Andrew
* Arjen Vrielink

* Reinout van Rees
* Carsten Byrman
2 changes: 1 addition & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ History
0.4.3 (unreleased)
++++++++++++++++++

- Nothing changed yet.
- Add option to make messages aggregable by Sentry for example.


0.4.2 (2015-10-30)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ lint: flake8
pylint --load-plugins pylint_django -f colorized --rcfile=.pylint.cfg --disable=I0011 -j 4 -r n django_logutils/ tests/

test:
coverage run --source django_logutils -m py.test -v
coverage run --source django_logutils -m py.test -v --ignore=lib/
coverage report -m

test-all:
Expand Down
16 changes: 8 additions & 8 deletions django_logutils/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,23 @@ def create_log_dict(request, response):
}


def create_log_message(log_dict, use_sql_info=False):
def create_log_message(log_dict, use_sql_info=False, fmt=True):
"""
Create the logging message string.
"""
log_msg = (
"{remote_address} {user_email} {method} {url} {status} "
"{content_length} ({request_time:.2f} seconds)"
"%(remote_address)s %(user_email)s %(method)s %(url)s %(status)d "
"%(content_length)d (%(request_time).2f seconds)"
)
if use_sql_info:
sql_time = sum(
float(q['time']) for q in connection.queries) * 1000
extra_log = {
'nr_queries': len(connection.queries),
'sql_time': sql_time}
log_msg += " ({nr_queries} SQL queries, {sql_time} ms)"
log_msg += " (%(nr_queries)d SQL queries, %(sql_time)f ms)"
log_dict.update(extra_log)
return log_msg.format(**log_dict)
return log_msg % log_dict if fmt else log_msg


class LoggingMiddleware(object):
Expand Down Expand Up @@ -117,12 +117,12 @@ def process_response(self, request, response):
request_time > float(settings.LOGUTILS_REQUEST_TIME_THRESHOLD))
use_sql_info = settings.DEBUG or is_request_time_too_high

log_msg = create_log_message(log_dict, use_sql_info)
log_msg = create_log_message(log_dict, use_sql_info, fmt=False)

if is_request_time_too_high:
logger.warning(log_msg, extra=log_dict)
logger.warning(log_msg, log_dict, extra=log_dict)
else:
logger.info(log_msg, extra=log_dict)
logger.info(log_msg, log_dict, extra=log_dict)
except Exception as e:
logger.exception(e)

Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@
zip_safe=False,
keywords='django-logutils',
classifiers=[
'Development Status :: 3 - Alpha',
'Development Status :: 4 - Beta',
'Framework :: Django',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Natural Language :: English',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],
)
5 changes: 3 additions & 2 deletions tests/middleware/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ def test_logging_middleware_with_empty_view(client, base_settings, caplog):
assert len(response.content) == 0
assert len(caplog.records()) == 1
record = caplog.records()[0]
assert '/empty/' in record.msg
assert '127.0.0.1' in record.msg
message = record.msg % record.args
assert '/empty/' in message
assert '127.0.0.1' in message
assert record.remote_address == '127.0.0.1'
assert record.levelname == 'INFO'
assert record.method == 'GET'
Expand Down

0 comments on commit a510d9a

Please sign in to comment.