Skip to content

Commit

Permalink
Merge pull request #46 from geoadmin/fix-BGDIINF_SB-3114-flask-reques…
Browse files Browse the repository at this point in the history
…t-attributes

BGDIINF_SB-3114: Return view_args empty dict instead of None if missing - #patch
  • Loading branch information
ltflb-bgdi authored Mar 7, 2024
2 parents 5c3efca + dfb5737 commit 0799e90
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 54 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ PR can be directly open on `master`. Then the PR title define the version bump a
- PR title and/or commit message contains `#patch` or head branch name starts with `bug-|hotfix-|bugfix-` => patch version is bumped
- Otherwise by default the minor version is bumped

Note that publish is done by `.github/workflow/semver.yml` and not by codebuild.

## Contribution

Every contribution to this library is welcome ! So if you find a bug or want to add a new feature everyone is welcome to open an [issue](https://github.com/geoadmin/lib-py-logging-utilities/issues) or created a [Pull Request](https://github.com/geoadmin/lib-py-logging-utilities/pulls).
Expand Down
51 changes: 0 additions & 51 deletions buildspec.yml

This file was deleted.

7 changes: 6 additions & 1 deletion logging_utilities/filters/flask_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ def filter(self, record):
value = str(request.data)
else:
raise
if isinstance(value, (ImmutableDict, ImmutableMultiDict, MultiDict)):
# Accessing flask_request_view_args.<key> might rise an exception if
# flask_request_view_args is Null. To safely access flask_request_view_args
# None is replaced by an empty dict.
if attribute == 'view_args' and value is None:
setattr(record, rec_attribute, {})
elif isinstance(value, (ImmutableDict, ImmutableMultiDict, MultiDict)):
setattr(record, rec_attribute, dict(value))
elif value is None or isinstance(value, (str, int, float, dict, list)):
setattr(record, rec_attribute, value)
Expand Down
38 changes: 37 additions & 1 deletion tests/test_flask_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def test_empty_flask_attribute_no_context(self):
with self.assertLogs('test_formatter', level=logging.DEBUG) as ctx:
logger = logging.getLogger('test_formatter')
self._configure_flask_attribute(logger, FLASK_DEFAULT_FMT, FLASK_DEFAULT_ATTRIBUTES)
reset_log_record_factory()
with self.assertRaises((ValueError, KeyError)):
logger.info('Simple message')

Expand Down Expand Up @@ -103,7 +104,6 @@ def test_flask_attribute_query_string(self):
with self.assertLogs('test_formatter', level=logging.DEBUG) as ctx:
logger = logging.getLogger('test_formatter')
self._configure_flask_attribute(logger, FLASK_DEFAULT_FMT, FLASK_DEFAULT_ATTRIBUTES)

with app.test_request_context('/make_report/2017?param1=value1'):
logger.info('Simple message')
logger.info('Composed message: %s', 'this is a composed message')
Expand Down Expand Up @@ -227,3 +227,39 @@ def test_flask_attribute_mimetype(self):
"Composed message with extra:text/plain:{'charset': 'utf-8'}",
]
)

def test_flask_attribute_view_args(self):
with self.assertLogs('test_formatter', level=logging.DEBUG) as ctx:
logger = logging.getLogger('test_formatter')
self._configure_flask_attribute(
logger, "%(message)s:%(flask_request_view_args)s", ['view_args']
)

# Handler required by add_url_rule function
def handle_time(time):
return

# Request without view_args
with app.test_request_context('/make_report'):
logger.info('Simple message')
logger.info('Composed message: %s', 'this is a composed message')
logger.info('Composed message %s', 'with extra', extra={'extra1': 23})

# Request with view args
app.add_url_rule('/make_report/<time>', view_func=handle_time)
with app.test_request_context('/make_report/current'):
logger.info('Simple message')
logger.info('Composed message: %s', 'this is a composed message')
logger.info('Composed message %s', 'with extra', extra={'extra1': 23})

self.assertEqual(
ctx.output,
[
'Simple message:{}',
'Composed message: this is a composed message:{}',
'Composed message with extra:{}',
"Simple message:{'time': 'current'}",
"Composed message: this is a composed message:{'time': 'current'}",
"Composed message with extra:{'time': 'current'}",
]
)
2 changes: 1 addition & 1 deletion tests/unittest.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ log-level = DEBUG

[coverage]
always-on = True
coverage = logging_utilities
coverage = logging_utilities

0 comments on commit 0799e90

Please sign in to comment.