Skip to content

Commit 0351b57

Browse files
committed
Add configurable logging for REST
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.
1 parent ac55253 commit 0351b57

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ Features:
107107
- issue2550852 - support for specifying a PostgreSQL schema to use for
108108
the Roundup database. (Patch by Stuart McGraw; slight modifications,
109109
tests, docs: John Rouillard).
110+
- issue2551274: add configurable logging for REST API when something
111+
fails, we now log status code and error message.
112+
(Ralf Schlatterbeck)
110113

111114
2023-07-13 2.3.0
112115

roundup/configuration.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,19 @@ def str2value(self, value):
977977
value = value.decode("utf-8")
978978
return re.compile(value, self.flags)
979979

980+
class LogLevelOption(Option):
981+
982+
"""A log level, one of none, debug, info, warning, error, critical"""
983+
984+
values = "none debug info warning error critical".split ()
985+
class_description = "Allowed values: %s" % (', '.join (values))
986+
987+
def str2value(self, value):
988+
_val = value.lower()
989+
if _val in self.values:
990+
return _val
991+
else:
992+
raise OptionValueError(self, value, self.class_description)
980993

981994
try:
982995
import jinja2 # noqa: F401
@@ -1257,6 +1270,8 @@ def str2value(self, value):
12571270
"""Whether to enable i18n for the rest endpoint. Enable it if
12581271
you want to enable translation based on browsers lang
12591272
(if enabled), trackers lang (if set) or environment."""),
1273+
(LogLevelOption, 'rest_logging', 'none',
1274+
"Log-Level for REST errors."),
12601275
(IntegerNumberGeqZeroOption, 'api_calls_per_interval', "0",
12611276
"Limit API calls per api_interval_in_sec seconds to\n"
12621277
"this number.\n"

roundup/rest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ def format_object(self, *args, **kwargs):
107107
# decorate it
108108
self.client.response_code = code
109109
if code >= 400: # any error require error format
110+
logmethod = getattr(logger, self.db.config.WEB_REST_LOGGING, None)
111+
if logmethod:
112+
logmethod("statuscode: %s" % code)
113+
logmethod('message: "%s"' % data)
110114
result = {
111115
'error': {
112116
'status': code,

0 commit comments

Comments
 (0)