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

Thomas De Schampheleire patrickdepinguin at gmail.com
Sun May 10 14:22:54 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 b7e259611340434bead43af0327e5be53275319b
# Parent  877fa67247ecfe9cb01a6b70bfaeee265bb65b9e
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, or 'info'
otherwise.

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,43 @@ class _Message(object):
 
 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: info, warning, error, debug
+
+        If log_category is not set, the following mapping applies:
+
+        category | log_category
+        ---------+-------------
+         None    | info
+         notice  | info
+         warning | info
+         error   | error
+         success | debug
+
+        '''
+        log_function = {
+            'debug': log.debug,
+            'info': log.info,
+            'warning': log.warning,
+            'error': log.error,
+        }
+
+        if not log_category:
+            transform = {
+                None: 'info',
+                'notice': 'info',
+                'warning': 'info',
+                'error': 'error',
+                'success': 'debug',
+            }
+            log_category = transform[category]
+
+        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