now using logging.config.dictConfig

Fixes #4853
This commit is contained in:
Jérôme Schneider 2014-05-27 18:38:44 +02:00
parent 9180b216ca
commit 8946f5e40d
5 changed files with 140 additions and 75 deletions

View File

@ -206,7 +206,7 @@ class SAML2Auth(AuthForm):
if not metadata_file_path:
raise MandayeSamlException("sso: unable to load provider")
logger.debug('sso: target_idp is %r', target_idp)
logger.debug('sso: metadata url is %r', self.config.IDP_METADATA)
logger.debug('sso: metadata url is %r', self.config['saml2_idp_metadata'])
logger.debug('sso: mandaye metadata are %r', self._get_metadata(env))
server = lasso.Server.newFromBuffers(self._get_metadata(env),
self.config['saml2_signature_private_key'])

View File

@ -14,21 +14,62 @@ storage_backend = "mandaye.backends.sql"
# dialect+driver://username:password@host:port/database
db_url = 'sqlite:///test.db'
# urllib2 debug mode
debug = False
# Log configuration
import logging
debug = False
syslog = False
log_file = os.path.join(_PROJECT_PATH, 'mandaye/mandaye.log')
log_level = logging.INFO
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
# Log rotation
# W[0-6] : weekly (0: Monday), D: day, ... (python doc)
log_when = 'W6'
# Every week
log_interval = 1
# BackupCount (keep one year of log)
log_backup = 52
'formatters': {
'console': {
'format': '%(asctime)s %(levelname)s %(message)s',
'datefmt': '%H:%M:%S',
},
'file': {
'format': '%(asctime)s %(levelname)s %(uuid)s %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S'
}
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'console'
},
'rotate_file': {
'level': 'INFO',
'class': 'logging.handlers.TimedRotatingFileHandler',
'formatter': 'file',
'filename': os.path.join(_PROJECT_PATH, 'mandaye.log'),
# W[0-6] : weekly (0: Monday), D: day, ... (python doc)
'when': 'W6',
# Every week
'interval': 1,
# BackupCount (keep one year of log)
'backupCount': 52
},
'syslog': {
'level': 'INFO',
'class': 'logging.handlers.SysLogHandler',
'formatter': 'file',
'address': '/dev/log'
},
},
'loggers': {
'': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': False,
},
'mandaye': {
'handlers': ['console', 'rotate_file'],
'level': 'DEBUG',
'propagate': False,
},
},
}
# Template directory
template_directory = os.path.join(_PROJECT_PATH, 'mandaye/templates')
@ -72,22 +113,29 @@ alembic_script_path = os.path.join(_PROJECT_PATH, 'mandaye/alembic')
# supported authentification
authentifications = {
'saml2': 'mandaye.auth.SAML2Auth'
}
'saml2': 'mandaye.auth.SAML2Auth'
}
# supported mappers
mappers = {}
# beaker session configuration
session_opts = {
'session.type': 'file',
'session.cookie_expires': True,
'session.timeout': 3600,
'session.data_dir': '/var/tmp/beaker'
}
'session.type': 'file',
'session.cookie_expires': True,
'session.timeout': 3600,
'session.data_dir': '/var/tmp/beaker'
}
# Needed if ssl is activated
ssl = False
keyfile = ''
certfile = ''
if raven_dsn:
LOGGING['handlers']['sentry'] = {
'level': 'ERROR',
'class': 'raven.handlers.logging.SentryHandler',
'dsn': raven_dsn,
}
LOGGING['loggers']['']['handlers'].append('sentry')

View File

@ -1,49 +1,17 @@
import logging
from logging.handlers import SysLogHandler, TimedRotatingFileHandler
from logging.config import dictConfig
from mandaye import config
class UuidFilter(logging.Filter):
uuid = ""
def filter(self, record):
record.uuid = UuidFilter.uuid
return True
dictConfig(config.LOGGING)
logger = logging.getLogger(__name__)
logger.setLevel(config.log_level)
# Syslog logging
if config.syslog:
syslog_handler = SysLogHandler(address='/dev/log')
logger.addHandler(syslog_handler)
else:
syslog_handler = None
# File logging
if config.log_file:
filehandler = TimedRotatingFileHandler(config.log_file,
when=config.log_when,
interval=config.log_interval, backupCount=config.log_backup)
filehandler.setFormatter(
logging.Formatter('%(asctime)s %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'))
logger.addHandler(filehandler)
else:
filehandler = None
# Stream logging
sh = logging.StreamHandler()
sh.setFormatter(logging.Formatter('%(levelname)s %(message)s'))
logger.addHandler(sh)
def format_logging_handlers(uuid):
""" uuid: unique id to trace a request
Format logging handlers
"""
if filehandler:
filehandler.setFormatter(
logging.Formatter('%(asctime)s %(levelname)s [' +\
uuid + '] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
)
if syslog_handler:
syslog_handler.setFormatter(
logging.Formatter('%(levelname)s [' + uuid + '] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
)
logger.addFilter(UuidFilter())

View File

@ -13,7 +13,7 @@ from urlparse import urlparse
from mandaye import config
from mandaye.exceptions import ImproperlyConfigured
from mandaye.dispatcher import Dispatcher
from mandaye.log import logger, format_logging_handlers
from mandaye.log import logger, UuidFilter
from mandaye.handlers.default import MandayeRedirectHandler, MandayeErrorHandler
from mandaye.http import HTTPHeader, HTTPRequest, HTTPResponse
from mandaye.response import _404, _502, _500
@ -99,7 +99,7 @@ class MandayeApp(object):
self.dispatcher = None
local_host = env['HTTP_HOST']
path_info = env['PATH_INFO']
format_logging_handlers(self.env['mandaye.uuid'])
UuidFilter.uuid = self.env['mandaye.uuid']
logger.info("Client %s - %s %s://%s%s" %\
(self.env['REMOTE_ADDR'], self.env['REQUEST_METHOD'],
self.env['wsgi.url_scheme'], self.env['HTTP_HOST'],

View File

@ -10,18 +10,67 @@ _PROJECT_PATH = os.path.join(os.path.dirname(__file__), '..')
# dialect+driver://username:password@host:port/database
db_url = 'sqlite:///' + os.path.join(_PROJECT_PATH, '{project_name}.db')
## Log configuration
debug = False
syslog = False
log_file = os.path.join(_PROJECT_PATH, '{project_name}/{project_name}.log')
log_level = logging.INFO
# Log rotation
# W[0-6] : weekly (0: Monday), D: day, ... (python doc)
log_when = 'W6'
# Every week
log_interval = 1
# BackupCount (keep one year of log)
log_backup = 52
# Log configuration
LOGGING = {{
'version': 1,
'disable_existing_loggers': True,
'formatters': {{
'console': {{
'format': '%(asctime)s %(levelname)s %(message)s',
'datefmt': '%H:%M:%S',
}},
'file': {{
'format': '%(asctime)s %(levelname)s %(uuid)s %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S'
}}
}},
'handlers': {{
'console': {{
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'console'
}},
'rotate_file': {{
'level': 'INFO',
'class': 'logging.handlers.TimedRotatingFileHandler',
'formatter': 'file',
'filename': os.path.join(_PROJECT_PATH, '{project_name}.log'),
# W[0-6] : weekly (0: Monday), D: day, ... (python doc)
'when': 'W6',
# Every week
'interval': 1,
# BackupCount (keep one year of log)
'backupCount': 52
}},
'syslog': {{
'level': 'INFO',
'class': 'logging.handlers.SysLogHandler',
'formatter': 'file',
'address': '/dev/log'
}},
}},
'loggers': {{
'': {{
'handlers': ['console'],
'level': 'DEBUG',
'propagate': False,
}},
'mandaye': {{
'handlers': ['console', 'rotate_file'],
'level': 'DEBUG',
'propagate': False,
}},
'{project_name}': {{
'handlers': ['console', 'rotate_file'],
'level': 'DEBUG',
'propagate': False,
}},
}},
}}
## PATH
# Template directory