import logging
import logging.handlers

from django.conf import settings

import commonware.log
import dictconfig

from cef import cef


class NullHandler(logging.Handler):

    def emit(self, record):
        pass


base_fmt = ('%(name)s:%(levelname)s %(message)s '
            ':%(pathname)s:%(lineno)s')

cfg = {
    'version': 1,
    'filters': {},
    'formatters': {
        'debug': {
            '()': commonware.log.Formatter,
            'datefmt': '%H:%M:%s',
            'format': '%(asctime)s ' + base_fmt,
        },
        'prod': {
            '()': commonware.log.Formatter,
            'datefmt': '%H:%M:%s',
            'format': '%s: [%%(REMOTE_ADDR)s] %s' % (settings.SYSLOG_TAG,
                                                     base_fmt),
        },
        'prod2': {
            '()': commonware.log.Formatter,
            'datefmt': '%H:%M:%s',
            'format': '%s: [%%(REMOTE_ADDR)s] %s' % (settings.SYSLOG_TAG2,
                                                     base_fmt),
        },
        'csp': {
            '()': cef.SysLogFormatter,
            'datefmt': '%H:%M:%s',
            'format': '%s: %s' % (settings.SYSLOG_CSP, base_fmt),
        },
    },
    'handlers': {
        'console': {
            '()': logging.StreamHandler,
            'formatter': 'debug',
        },
        'syslog': {
            '()': logging.handlers.SysLogHandler,
            'facility': logging.handlers.SysLogHandler.LOG_LOCAL7,
            'formatter': 'prod',
        },
        'syslog2': {
            '()': logging.handlers.SysLogHandler,
            'facility': logging.handlers.SysLogHandler.LOG_LOCAL7,
            'formatter': 'prod2',
        },
        'syslog_csp': {
            '()': logging.handlers.SysLogHandler,
            'facility': logging.handlers.SysLogHandler.LOG_LOCAL4,
            'formatter': 'csp',
        },
        'null': {
            '()': NullHandler,
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'admin_log.AdminEmailHandler'
        },
    },
    'loggers': {
        'z': {},
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': False,
        },
    },
    'root': {},
}

for key, value in settings.LOGGING.items():
    cfg[key].update(value)


USE_SYSLOG = settings.HAS_SYSLOG and not settings.DEBUG


if USE_SYSLOG:
    cfg['loggers']['z.timer'] = {'handlers': ['syslog2']}
    cfg['loggers']['z.csp'] = {'handlers': ['syslog_csp'], 'level':'WARNING'}

# Set the level and handlers for all loggers.
for logger in cfg['loggers'].values() + [cfg['root']]:
    if 'handlers' not in logger:
        logger['handlers'] = ['syslog' if USE_SYSLOG else 'console']
    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)

@@ -635,37 +635,53 @@
       'format': 
+(
 '%25s: 
+%5B%25%25(USERNAME)s%5D
 %5B%25%25(REMOTE_ADDR)
@@ -678,32 +678,55 @@
 MOTE_ADDR)s%5D %25s'
+%0A                      
  %25 (settings.SYS
@@ -737,61 +737,8 @@
 TAG,
-%0A                                                    
  bas
@@ -735,32 +735,33 @@
 G_TAG, base_fmt)
+)
 ,%0A        %7D,%0A   
@@ -877,21 +877,37 @@
 ormat': 
+(
 '%25s: 
+%5B%25%25(USERNAME)s%5D
 %5B%25%25(REMO
@@ -916,24 +916,47 @@
 _ADDR)s%5D %25s'
+%0A                      
  %25 (settings
@@ -972,61 +972,8 @@
 AG2,
-%0A                                                    
  bas
@@ -970,32 +970,33 @@
 _TAG2, base_fmt)
+)
 ,%0A        %7D,%0A   

add usernames to the logs (bug 583047)