[PATCH 1 of 2 v3] helpers: add automatic logging of Flash messages shown to users

Thomas De Schampheleire patrickdepinguin at gmail.com
Thu Jun 11 05:43:59 EDT 2015


# HG changeset patch
# User Thomas De Schampheleire <thomas.de.schampheleire at gmail.com>
# Date 1427573281 -3600
#      Sat Mar 28 21:08:01 2015 +0100
# Node ID 4f3f6762ad6403b42919b61912bd234ad9a488ec
# Parent  53d68f201e4602d3f2ccfcd27107d2ebea2deea2
helpers: add automatic logging of Flash messages shown to users

Log all messages displayed through a flash to the user.

Subsequent patches can change the log level for individual flash calls to
make the log more useful.

diff --git a/kallithea/lib/helpers.py b/kallithea/lib/helpers.py
--- a/kallithea/lib/helpers.py
+++ b/kallithea/lib/helpers.py
@@ -406,6 +406,25 @@ class _Message(object):
 
 class Flash(_Flash):
 
+    def __call__(self, message, category=None, ignore_duplicate=False, logf=None):
+        """
+        Show a message to the user _and_ log it through the specified function
+
+        category: notice (default), warning, error, success
+        logf: a custom log function - such as log.debug
+
+        logf defaults to log.info, unless category equals 'success', in which
+        case logf defaults to log.debug.
+        """
+        if logf is None:
+            logf = log.info
+            if category == 'success':
+                logf = log.debug
+
+        logf('Flash %s: %s', category, message)
+
+        super(Flash, self).__call__(message, category, ignore_duplicate)
+
     def pop_messages(self):
         """Return all accumulated messages and delete them from the session.
 


More information about the kallithea-general mailing list