[PATCH 1 of 2] helpers: add automatic logging to Flash

Thomas De Schampheleire patrickdepinguin at gmail.com
Sat Mar 28 17:34:25 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 2f4d67b8acefc3e85ddb5c165bc4054b0ddab90d
# Parent  65c5e70a1d0c1861d7dc835f204d132342920da2
helpers: add automatic logging to Flash

Log all messages displayed through a flash to the user.
The log level equals the flash category if it is defined and not 'success',
otherwise falls back to 'debug'.

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
@@ -423,6 +423,29 @@
 
 class Flash(_Flash):
 
+    def __call__(self, message, category=None, ignore_duplicate=False, log_category=None):
+        '''Show a message to the user
+
+        category: notice (default), warning, error, success
+        log_category: debug, notice, warning, error, success
+
+        If log_category is not set, it defaults to 'debug' unless category is
+        set and different from 'success'.
+        '''
+        log_function = {
+            'debug': log.debug,
+            'notice': log.info,
+            'success': log.info,
+            'warning': log.warning,
+            'error': log.error,
+        }
+
+        if not log_category:
+            log_category = category if category and category != 'success' else 'debug'
+
+        log_function[log_category]('Flash: %s' % 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