Projet

Général

Profil

0003-logging_handler-add-new-logging-handler-that-ip-and-.patch

Benjamin Dauvergne, 14 août 2014 15:12

Télécharger (1,77 ko)

Voir les différences:

Subject: [PATCH 3/4] logging_handler: add new logging handler that ip and
 user info to any handler (refs #5281)

It uses the StoreRequestMiddleware to retrieve ip and user information
 authentic2/logging_handler.py |   34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 authentic2/logging_handler.py
authentic2/logging_handler.py
1
from . import middleware, utils
2

  
3
class WrappedHandler(object):
4
    def __init__(self, real_class, **kwargs):
5
        if isinstance(real_class, basestring):
6
            real_class = utils.import_class(real_class)
7
        self.wrapped = real_class(**kwargs)
8

  
9
    def fill_record(self, record):
10
        raise NotImplementedError
11

  
12
    def handle(self, record):
13
        return self.wrapped.handle(record)
14

  
15
    def __getattr__(self, name):
16
        return getattr(self.wrappped, name)
17

  
18
    def __setattr(self, name, value):
19
        setattr(self.wrapped, name, value)
20

  
21
class RequestWrappedHandler(WrappedHandler):
22
    def fill_record(self, record):
23
        request = middleware.StoreRequestMiddleware.get_request()
24
        if request is None:
25
            record.update({
26
                'host': '-',
27
                'ip': '-',
28
                'user': '-',
29
            })
30
        record.update({
31
                'host': request.META.get('HTTP_HOST', '-'),
32
                'ip': request.META.get('REMOTE_ADDR', '-'),
33
                'user': request.user.get_username(),
34
        })
0
-