Statistics
| Branch: | Tag: | Revision:

root / snf-webproject / synnefo / webproject / middleware / log.py @ 0fa71fcc

History | View | Annotate | Download (2.6 kB)

1 8acb1f97 Kostas Papadimitriou
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 8acb1f97 Kostas Papadimitriou
#
3 8acb1f97 Kostas Papadimitriou
# Redistribution and use in source and binary forms, with or
4 8acb1f97 Kostas Papadimitriou
# without modification, are permitted provided that the following
5 8acb1f97 Kostas Papadimitriou
# conditions are met:
6 8acb1f97 Kostas Papadimitriou
#
7 8acb1f97 Kostas Papadimitriou
#   1. Redistributions of source code must retain the above
8 8acb1f97 Kostas Papadimitriou
#      copyright notice, this list of conditions and the following
9 8acb1f97 Kostas Papadimitriou
#      disclaimer.
10 8acb1f97 Kostas Papadimitriou
#
11 8acb1f97 Kostas Papadimitriou
#   2. Redistributions in binary form must reproduce the above
12 8acb1f97 Kostas Papadimitriou
#      copyright notice, this list of conditions and the following
13 8acb1f97 Kostas Papadimitriou
#      disclaimer in the documentation and/or other materials
14 8acb1f97 Kostas Papadimitriou
#      provided with the distribution.
15 8acb1f97 Kostas Papadimitriou
#
16 8acb1f97 Kostas Papadimitriou
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 8acb1f97 Kostas Papadimitriou
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 8acb1f97 Kostas Papadimitriou
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 8acb1f97 Kostas Papadimitriou
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 8acb1f97 Kostas Papadimitriou
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 8acb1f97 Kostas Papadimitriou
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 8acb1f97 Kostas Papadimitriou
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 8acb1f97 Kostas Papadimitriou
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 8acb1f97 Kostas Papadimitriou
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 8acb1f97 Kostas Papadimitriou
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 8acb1f97 Kostas Papadimitriou
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 8acb1f97 Kostas Papadimitriou
# POSSIBILITY OF SUCH DAMAGE.
28 8acb1f97 Kostas Papadimitriou
#
29 8acb1f97 Kostas Papadimitriou
# The views and conclusions contained in the software and
30 8acb1f97 Kostas Papadimitriou
# documentation are those of the authors and should not be
31 8acb1f97 Kostas Papadimitriou
# interpreted as representing official policies, either expressed
32 8acb1f97 Kostas Papadimitriou
# or implied, of GRNET S.A.
33 8acb1f97 Kostas Papadimitriou
34 8acb1f97 Kostas Papadimitriou
from django.conf import settings
35 8acb1f97 Kostas Papadimitriou
from django.core.exceptions import MiddlewareNotUsed
36 8acb1f97 Kostas Papadimitriou
37 8acb1f97 Kostas Papadimitriou
from synnefo.lib.dictconfig import dictConfig
38 8acb1f97 Kostas Papadimitriou
39 8acb1f97 Kostas Papadimitriou
import logging
40 8acb1f97 Kostas Papadimitriou
41 8acb1f97 Kostas Papadimitriou
42 8acb1f97 Kostas Papadimitriou
class NullHandler(logging.Handler):
43 8acb1f97 Kostas Papadimitriou
    def emit(self, record):
44 8acb1f97 Kostas Papadimitriou
        pass
45 8acb1f97 Kostas Papadimitriou
46 8acb1f97 Kostas Papadimitriou
47 8acb1f97 Kostas Papadimitriou
class LoggingConfigMiddleware:
48 8acb1f97 Kostas Papadimitriou
    def __init__(self):
49 74d988b0 Christos Stavrakakis
        """Initialise the logging setup from settings.
50 74d988b0 Christos Stavrakakis

51 74d988b0 Christos Stavrakakis
        Logging setup is initialized only in the first request.
52 74d988b0 Christos Stavrakakis
        """
53 8acb1f97 Kostas Papadimitriou
        logging_setting = getattr(settings, 'LOGGING_SETUP', None)
54 8acb1f97 Kostas Papadimitriou
        if logging_setting:
55 8acb1f97 Kostas Papadimitriou
            # Disable handlers that are not used by any logger.
56 8acb1f97 Kostas Papadimitriou
            active_handlers = set()
57 8acb1f97 Kostas Papadimitriou
            loggers = logging_setting.get('loggers', {})
58 8acb1f97 Kostas Papadimitriou
            for logger in loggers.values():
59 8acb1f97 Kostas Papadimitriou
                active_handlers.update(logger.get('handlers', []))
60 8acb1f97 Kostas Papadimitriou
            handlers = logging_setting.get('handlers', {})
61 8acb1f97 Kostas Papadimitriou
            for handler in handlers:
62 8acb1f97 Kostas Papadimitriou
                if handler not in active_handlers:
63 8acb1f97 Kostas Papadimitriou
                    handlers[handler] = {'class': 'logging.NullHandler'}
64 8acb1f97 Kostas Papadimitriou
65 8acb1f97 Kostas Papadimitriou
            logging.NullHandler = NullHandler
66 8acb1f97 Kostas Papadimitriou
            dictConfig(logging_setting)
67 8acb1f97 Kostas Papadimitriou
        raise MiddlewareNotUsed('Logging setup only.')