summaryrefslogtreecommitdiff
path: root/nova/hacking
diff options
context:
space:
mode:
authorTakashi NATSUME <natsume.takashi@lab.ntt.co.jp>2016-06-28 16:42:31 +0900
committerTakashi NATSUME <natsume.takashi@lab.ntt.co.jp>2016-10-11 08:39:48 +0000
commit4eb89c206e68a7172ebad897ad24769036c7bdd6 (patch)
tree53e88dba13b979dc1dc084cf385e81282fc9761f /nova/hacking
parent3d7ff766a7e288faa462cf64cd53e94317814589 (diff)
downloadnova-4eb89c206e68a7172ebad897ad24769036c7bdd6.tar.gz
Add a hacking rule for string interpolation at logging
String interpolation should be delayed to be handled by the logging code, rather than being done at the point of the logging call. So add the following hacking rule for it. - [N354] String interpolation should be delayed at logging calls. See the oslo i18n guideline. * http://docs.openstack.org/developer/oslo.i18n/guidelines.html Change-Id: Ief6d3ee3539c0857098fffdb7acfeec3e0fed6eb Closes-Bug: #1596829
Diffstat (limited to 'nova/hacking')
-rw-r--r--nova/hacking/checks.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/nova/hacking/checks.py b/nova/hacking/checks.py
index e679b0c145..19c46f6747 100644
--- a/nova/hacking/checks.py
+++ b/nova/hacking/checks.py
@@ -105,6 +105,9 @@ doubled_words_re = re.compile(
r"\b(then?|[iao]n|i[fst]|but|f?or|at|and|[dt]o)\s+\1\b")
log_remove_context = re.compile(
r"(.)*LOG\.(.*)\(.*(context=[_a-zA-Z0-9].*)+.*\)")
+log_string_interpolation = re.compile(r".*LOG\.(error|warning|info"
+ r"|critical|exception|debug)"
+ r"\([^,]*%[^,]*[,)]")
class BaseASTChecker(ast.NodeVisitor):
@@ -794,6 +797,28 @@ def check_context_log(logical_line, physical_line, filename):
"kwarg.")
+def check_delayed_string_interpolation(logical_line, physical_line, filename):
+ """Check whether string interpolation is delayed at logging calls
+
+ Not correct: LOG.debug('Example: %s' % 'bad')
+ Correct: LOG.debug('Example: %s', 'good')
+
+ N354
+ """
+ if "nova/tests" in filename:
+ return
+
+ if pep8.noqa(physical_line):
+ return
+
+ if log_string_interpolation.match(logical_line):
+ yield(logical_line.index('%'),
+ "N354: String interpolation should be delayed to be "
+ "handled by the logging code, rather than being done "
+ "at the point of the logging call. "
+ "Use ',' instead of '%'.")
+
+
def factory(register):
register(import_no_db_in_virt)
register(no_db_session_in_public_api)
@@ -834,3 +859,4 @@ def factory(register):
register(no_log_warn)
register(CheckForUncalledTestClosure)
register(check_context_log)
+ register(check_delayed_string_interpolation)