Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-app / pithos / middleware / log.py @ 4a669c71

History | View | Annotate | Download (2.6 kB)

1 2e662088 Antony Chazapis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 aa4fac11 Giorgos Verigakis
# 
3 aa4fac11 Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 aa4fac11 Giorgos Verigakis
# without modification, are permitted provided that the following
5 aa4fac11 Giorgos Verigakis
# conditions are met:
6 aa4fac11 Giorgos Verigakis
# 
7 aa4fac11 Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 aa4fac11 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 aa4fac11 Giorgos Verigakis
#      disclaimer.
10 aa4fac11 Giorgos Verigakis
# 
11 aa4fac11 Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 aa4fac11 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 aa4fac11 Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 aa4fac11 Giorgos Verigakis
#      provided with the distribution.
15 aa4fac11 Giorgos Verigakis
# 
16 aa4fac11 Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 aa4fac11 Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 aa4fac11 Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 aa4fac11 Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 aa4fac11 Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 aa4fac11 Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 aa4fac11 Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 aa4fac11 Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 aa4fac11 Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 aa4fac11 Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 aa4fac11 Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 aa4fac11 Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 aa4fac11 Giorgos Verigakis
# 
29 aa4fac11 Giorgos Verigakis
# The views and conclusions contained in the software and
30 aa4fac11 Giorgos Verigakis
# documentation are those of the authors and should not be
31 aa4fac11 Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 aa4fac11 Giorgos Verigakis
# or implied, of GRNET S.A.
33 aa4fac11 Giorgos Verigakis
34 aa4fac11 Giorgos Verigakis
from django.conf import settings
35 d7d60147 Antony Chazapis
from django.core.exceptions import MiddlewareNotUsed
36 aa4fac11 Giorgos Verigakis
37 18d8fb23 Antony Chazapis
from pithos.lib.dictconfig import dictConfig
38 18d8fb23 Antony Chazapis
39 d7d60147 Antony Chazapis
import logging
40 aa4fac11 Giorgos Verigakis
41 d7d60147 Antony Chazapis
42 18d8fb23 Antony Chazapis
class NullHandler(logging.Handler):
43 18d8fb23 Antony Chazapis
    def emit(self, record):
44 18d8fb23 Antony Chazapis
        pass
45 18d8fb23 Antony Chazapis
46 18d8fb23 Antony Chazapis
47 d7d60147 Antony Chazapis
class LoggingConfigMiddleware:
48 d7d60147 Antony Chazapis
    def __init__(self):
49 d7d60147 Antony Chazapis
        '''Initialise the logging setup from settings, called on first request.'''
50 18d8fb23 Antony Chazapis
        logging_setting = getattr(settings, 'LOGGING_SETUP', None)
51 18d8fb23 Antony Chazapis
        if logging_setting:
52 18d8fb23 Antony Chazapis
            # Disable handlers that are not used by any logger.
53 18d8fb23 Antony Chazapis
            active_handlers = set()
54 18d8fb23 Antony Chazapis
            loggers = logging_setting.get('loggers', {})
55 18d8fb23 Antony Chazapis
            for logger in loggers.values():
56 18d8fb23 Antony Chazapis
                active_handlers.update(logger.get('handlers', []))
57 18d8fb23 Antony Chazapis
            handlers = logging_setting.get('handlers', {})
58 18d8fb23 Antony Chazapis
            for handler in handlers:
59 18d8fb23 Antony Chazapis
                if handler not in active_handlers:
60 18d8fb23 Antony Chazapis
                    handlers[handler] = {'class': 'logging.NullHandler'}
61 18d8fb23 Antony Chazapis
            
62 18d8fb23 Antony Chazapis
            logging.NullHandler = NullHandler
63 18d8fb23 Antony Chazapis
            dictConfig(logging_setting)
64 d7d60147 Antony Chazapis
        raise MiddlewareNotUsed('Logging setup only.')