Skip to content

Commit

Permalink
Add configurable logging for REST
Browse files Browse the repository at this point in the history
We now log status code and error message for failing REST requests.
Introduces new config item rest_logging in section [web].
Fixes (part of) issue2551274.
  • Loading branch information
schlatterbeck committed Jan 17, 2024
1 parent ac55253 commit 0351b57
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ Features:
- issue2550852 - support for specifying a PostgreSQL schema to use for
the Roundup database. (Patch by Stuart McGraw; slight modifications,
tests, docs: John Rouillard).
- issue2551274: add configurable logging for REST API when something
fails, we now log status code and error message.
(Ralf Schlatterbeck)

2023-07-13 2.3.0

Expand Down
15 changes: 15 additions & 0 deletions roundup/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,19 @@ def str2value(self, value):
value = value.decode("utf-8")
return re.compile(value, self.flags)

class LogLevelOption(Option):

"""A log level, one of none, debug, info, warning, error, critical"""

values = "none debug info warning error critical".split ()
class_description = "Allowed values: %s" % (', '.join (values))

def str2value(self, value):
_val = value.lower()
if _val in self.values:
return _val
else:
raise OptionValueError(self, value, self.class_description)

try:
import jinja2 # noqa: F401
Expand Down Expand Up @@ -1257,6 +1270,8 @@ def str2value(self, value):
"""Whether to enable i18n for the rest endpoint. Enable it if
you want to enable translation based on browsers lang
(if enabled), trackers lang (if set) or environment."""),
(LogLevelOption, 'rest_logging', 'none',
"Log-Level for REST errors."),
(IntegerNumberGeqZeroOption, 'api_calls_per_interval', "0",
"Limit API calls per api_interval_in_sec seconds to\n"
"this number.\n"
Expand Down
4 changes: 4 additions & 0 deletions roundup/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ def format_object(self, *args, **kwargs):
# decorate it
self.client.response_code = code
if code >= 400: # any error require error format
logmethod = getattr(logger, self.db.config.WEB_REST_LOGGING, None)
if logmethod:
logmethod("statuscode: %s" % code)
logmethod('message: "%s"' % data)
result = {
'error': {
'status': code,
Expand Down

0 comments on commit 0351b57

Please sign in to comment.