Browse Source

retrieve tracim logger

Guénaël Muller 7 years ago
parent
commit
d1e0206a62
3 changed files with 38 additions and 10 deletions
  1. 10 10
      tracim/config.py
  2. 27 0
      tracim/logger.py
  3. 1 0
      tracim/views/default.py

+ 10 - 10
tracim/config.py View File

@@ -1,11 +1,9 @@
1 1
 from paste.deploy.converters import asbool
2 2
 from urllib.parse import urlparse
3
-import logging
3
+from tracim.logger import logger
4 4
 
5 5
 from pyramid.request import Request
6 6
 
7
-logger = logging.getLogger(__name__)
8
-
9 7
 
10 8
 class RequestWithCFG(Request):
11 9
 
@@ -33,16 +31,18 @@ class CFG(object):
33 31
             # we do not show URL because At the time of configuration setup,
34 32
             # it can't be evaluated
35 33
             # We do not show CONTENT in order not to pollute log files
36
-            logger.info('CONFIG: [ {} | {} ]'.format(key, value))
34
+            logger.info(self, 'CONFIG: [ {} | {} ]'.format(key, value))
37 35
         else:
38
-            logger.info('CONFIG: [ {} | <value not shown> ]'.format(key))
36
+            logger.info(self, 'CONFIG: [ {} | <value not shown> ]'.format(key))
39 37
 
40 38
         self.__dict__[key] = value
41 39
 
42 40
     def __init__(self, settings):
43 41
         """Parse configuration file."""
44 42
 
45
-        ### General
43
+        ###
44
+        # General
45
+        ###
46 46
 
47 47
         mandatory_msg = \
48 48
             'ERROR: {} configuration is mandatory. Set it before continuing.'
@@ -107,6 +107,7 @@ class CFG(object):
107 107
         if not self.WEBSITE_SERVER_NAME:
108 108
             self.WEBSITE_SERVER_NAME = urlparse(self.WEBSITE_BASE_URL).hostname
109 109
             logger.warning(
110
+                self,
110 111
                 'NOTE: Generated website.server_name parameter from '
111 112
                 'website.base_url parameter -> {0}'
112 113
                 .format(self.WEBSITE_SERVER_NAME)
@@ -331,7 +332,7 @@ class CFG(object):
331 332
         #             self.WEBSITE_SERVER_NAME,
332 333
         #             self.WSGIDAV_PORT,
333 334
         #         )
334
-        #     logger.warning(
335
+        #     logger.warning(self,
335 336
         #         'NOTE: Generated wsgidav.client.base_url parameter with '
336 337
         #         'followings parameters: website.server_name and '
337 338
         #         'wsgidav.conf port'.format(
@@ -372,7 +373,7 @@ class CFG(object):
372 373
         # )
373 374
         # if not self.RADICALE_SERVER_ALLOW_ORIGIN:
374 375
         #     self.RADICALE_SERVER_ALLOW_ORIGIN = self.WEBSITE_BASE_URL
375
-        #     logger.warning(
376
+        #     logger.warning(self,
376 377
         #         'NOTE: Generated radicale.server.allow_origin parameter with '
377 378
         #         'followings parameters: website.base_url ({0})'
378 379
         #         .format(self.WEBSITE_BASE_URL)
@@ -403,7 +404,7 @@ class CFG(object):
403 404
         #         = '/' + self.RADICALE_CLIENT_BASE_URL_PREFIX
404 405
         #
405 406
         # if not self.RADICALE_CLIENT_BASE_URL_HOST:
406
-        #     logger.warning(
407
+        #     logger.warning(self,
407 408
         #         'Generated radicale.client.base_url.host parameter with '
408 409
         #         'followings parameters: website.server_name -> {}'
409 410
         #         .format(self.WEBSITE_SERVER_NAME)
@@ -415,7 +416,6 @@ class CFG(object):
415 416
         #     self.RADICALE_CLIENT_BASE_URL_PREFIX,
416 417
         # )
417 418
 
418
-
419 419
     class CST(object):
420 420
         ASYNC = 'ASYNC'
421 421
         SYNC = 'SYNC'

+ 27 - 0
tracim/logger.py View File

@@ -0,0 +1,27 @@
1
+import logging
2
+
3
+class Logger(object):
4
+    TPL = '[{cls}] {msg}'
5
+    def __init__(self, logger_name):
6
+        self._name = logger_name
7
+        self._logger = logging.getLogger(self._name)
8
+
9
+    def _txt(self, instance_or_class):
10
+        if instance_or_class.__class__.__name__ in ('function', 'type'):
11
+            return instance_or_class.__name__
12
+        else:
13
+            return instance_or_class.__class__.__name__
14
+
15
+    def debug(self, instance_or_class, message):
16
+        self._logger.debug(Logger.TPL.format(cls=self._txt(instance_or_class), msg=message))
17
+
18
+    def error(self, instance_or_class, message, exc_info=0):
19
+        self._logger.error(Logger.TPL.format(cls=self._txt(instance_or_class), msg=message, exc_info=exc_info))
20
+
21
+    def info(self, instance_or_class, message):
22
+        self._logger.info(Logger.TPL.format(cls=self._txt(instance_or_class), msg=message))
23
+
24
+    def warning(self, instance_or_class, message):
25
+        self._logger.warning(Logger.TPL.format(cls=self._txt(instance_or_class), msg=message))
26
+
27
+logger = Logger('tracim')

+ 1 - 0
tracim/views/default.py View File

@@ -8,6 +8,7 @@ from ..models import MyModel
8 8
 
9 9
 @view_config(route_name='home', renderer='../templates/mytemplate.jinja2')
10 10
 def my_view(request):
11
+    request.config()
11 12
     try:
12 13
         query = request.dbsession.query(MyModel)
13 14
         one = query.filter(MyModel.name == 'one').first()