2009-12-16 02:53:21 +03:00
|
|
|
import logging
|
|
|
|
import logging.handlers
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
2010-05-28 23:47:47 +04:00
|
|
|
import commonware.log
|
|
|
|
import dictconfig
|
|
|
|
|
2011-02-25 01:30:56 +03:00
|
|
|
from cef import cef
|
|
|
|
|
2010-05-28 23:47:47 +04:00
|
|
|
|
|
|
|
class NullHandler(logging.Handler):
|
|
|
|
|
|
|
|
def emit(self, record):
|
|
|
|
pass
|
2009-12-16 02:53:21 +03:00
|
|
|
|
|
|
|
|
2010-05-27 02:10:12 +04:00
|
|
|
base_fmt = ('%(name)s:%(levelname)s %(message)s '
|
2010-05-20 21:46:02 +04:00
|
|
|
':%(pathname)s:%(lineno)s')
|
2010-05-28 23:47:47 +04:00
|
|
|
|
|
|
|
cfg = {
|
|
|
|
'version': 1,
|
|
|
|
'filters': {},
|
|
|
|
'formatters': {
|
|
|
|
'debug': {
|
|
|
|
'()': commonware.log.Formatter,
|
2011-05-19 04:50:57 +04:00
|
|
|
'datefmt': '%H:%M:%S',
|
2010-05-28 23:47:47 +04:00
|
|
|
'format': '%(asctime)s ' + base_fmt,
|
|
|
|
},
|
|
|
|
'prod': {
|
|
|
|
'()': commonware.log.Formatter,
|
2011-05-19 04:50:57 +04:00
|
|
|
'datefmt': '%H:%M:%S',
|
2011-04-27 22:32:50 +04:00
|
|
|
'format': ('%s: [%%(USERNAME)s][%%(REMOTE_ADDR)s] %s'
|
|
|
|
% (settings.SYSLOG_TAG, base_fmt)),
|
2010-05-28 23:47:47 +04:00
|
|
|
},
|
2011-01-25 01:26:13 +03:00
|
|
|
'prod2': {
|
|
|
|
'()': commonware.log.Formatter,
|
2011-05-19 04:50:57 +04:00
|
|
|
'datefmt': '%H:%M:%S',
|
2011-04-27 22:32:50 +04:00
|
|
|
'format': ('%s: [%%(USERNAME)s][%%(REMOTE_ADDR)s] %s'
|
|
|
|
% (settings.SYSLOG_TAG2, base_fmt)),
|
2011-01-25 01:26:13 +03:00
|
|
|
},
|
2011-02-25 01:30:56 +03:00
|
|
|
'csp': {
|
|
|
|
'()': cef.SysLogFormatter,
|
2011-05-19 04:50:57 +04:00
|
|
|
'datefmt': '%H:%M:%S',
|
2011-02-25 01:30:56 +03:00
|
|
|
'format': '%s: %s' % (settings.SYSLOG_CSP, base_fmt),
|
|
|
|
},
|
2010-05-28 23:47:47 +04:00
|
|
|
},
|
|
|
|
'handlers': {
|
|
|
|
'console': {
|
|
|
|
'()': logging.StreamHandler,
|
|
|
|
'formatter': 'debug',
|
|
|
|
},
|
|
|
|
'syslog': {
|
|
|
|
'()': logging.handlers.SysLogHandler,
|
|
|
|
'facility': logging.handlers.SysLogHandler.LOG_LOCAL7,
|
|
|
|
'formatter': 'prod',
|
|
|
|
},
|
2011-01-25 01:26:13 +03:00
|
|
|
'syslog2': {
|
|
|
|
'()': logging.handlers.SysLogHandler,
|
|
|
|
'facility': logging.handlers.SysLogHandler.LOG_LOCAL7,
|
|
|
|
'formatter': 'prod2',
|
|
|
|
},
|
2011-02-25 01:30:56 +03:00
|
|
|
'syslog_csp': {
|
|
|
|
'()': logging.handlers.SysLogHandler,
|
2011-03-11 01:26:16 +03:00
|
|
|
'facility': logging.handlers.SysLogHandler.LOG_LOCAL4,
|
2011-02-25 01:30:56 +03:00
|
|
|
'formatter': 'csp',
|
|
|
|
},
|
2010-05-28 23:47:47 +04:00
|
|
|
'null': {
|
|
|
|
'()': NullHandler,
|
|
|
|
},
|
2010-11-19 22:53:38 +03:00
|
|
|
'mail_admins': {
|
|
|
|
'level': 'ERROR',
|
2011-03-02 21:05:17 +03:00
|
|
|
'class': 'admin_log.AdminEmailHandler'
|
2010-11-19 22:53:38 +03:00
|
|
|
},
|
2010-05-28 23:47:47 +04:00
|
|
|
},
|
|
|
|
'loggers': {
|
|
|
|
'z': {},
|
2010-11-19 22:53:38 +03:00
|
|
|
'django.request': {
|
|
|
|
'handlers': ['mail_admins'],
|
|
|
|
'level': 'ERROR',
|
|
|
|
'propagate': False,
|
|
|
|
},
|
2010-05-28 23:47:47 +04:00
|
|
|
},
|
|
|
|
'root': {},
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value in settings.LOGGING.items():
|
|
|
|
cfg[key].update(value)
|
|
|
|
|
2011-01-25 01:26:13 +03:00
|
|
|
|
|
|
|
USE_SYSLOG = settings.HAS_SYSLOG and not settings.DEBUG
|
|
|
|
|
2011-02-25 01:30:56 +03:00
|
|
|
|
2011-01-25 01:26:13 +03:00
|
|
|
if USE_SYSLOG:
|
2011-01-25 01:38:39 +03:00
|
|
|
cfg['loggers']['z.timer'] = {'handlers': ['syslog2']}
|
2011-02-25 01:30:56 +03:00
|
|
|
cfg['loggers']['z.csp'] = {'handlers': ['syslog_csp'], 'level':'WARNING'}
|
2011-01-25 01:26:13 +03:00
|
|
|
|
2010-05-28 23:47:47 +04:00
|
|
|
# Set the level and handlers for all loggers.
|
|
|
|
for logger in cfg['loggers'].values() + [cfg['root']]:
|
|
|
|
if 'handlers' not in logger:
|
2011-01-25 01:26:13 +03:00
|
|
|
logger['handlers'] = ['syslog' if USE_SYSLOG else 'console']
|
2010-05-28 23:47:47 +04:00
|
|
|
if 'level' not in logger:
|
|
|
|
logger['level'] = settings.LOG_LEVEL
|
|
|
|
if logger is not cfg['root'] and 'propagate' not in logger:
|
|
|
|
logger['propagate'] = False
|
|
|
|
|
|
|
|
dictconfig.dictConfig(cfg)
|