import os
import platform
import sys
from logging.handlers import SysLogHandler


def get_logger_config(log_dir,
                      logging_env="no_env",
                      edx_filename="edx.log",
                      dev_env=False,
                      syslog_addr=None,
                      debug=False,
                      local_loglevel='INFO',
                      service_variant='xqueue'):

    """

    Return the appropriate logging config dictionary. You should assign the
    result of this to the LOGGING var in your settings. The reason it's done
    this way instead of registering directly is because I didn't want to worry
    about resetting the logging state if this is called multiple times when
    settings are extended.

    If dev_env is set to true logging will not be done via local rsyslogd,
    instead, application logs will be dropped in log_dir.

    "edx_filename" is ignored unless dev_env is set to true since otherwise logging is handled by rsyslogd.

    """

    # Revert to INFO if an invalid string is passed in
    if local_loglevel not in ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']:
        local_loglevel = 'INFO'

    hostname = platform.node().split(".")[0]
    syslog_format = ("[service_variant={service_variant}]"
                     "[%(name)s][env:{logging_env}] %(levelname)s "
                     "[{hostname}  %(process)d] [%(filename)s:%(lineno)d] "
                     "- %(message)s").format(
                        service_variant=service_variant,
                        logging_env=logging_env, hostname=hostname)

    handlers = ['console', 'local'] if debug else ['local']
    if syslog_addr:
        handlers.append('syslogger-remote')

    logger_config = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'standard': {
                'format': '%(asctime)s %(levelname)s %(process)d '
                          '[%(name)s] %(filename)s:%(lineno)d - %(message)s',
            },
            'syslog_format': {'format': syslog_format},
            'raw': {'format': '%(message)s'},
        },
        'handlers': {
            'console': {
                'level': 'DEBUG' if debug else 'INFO',
                'class': 'logging.StreamHandler',
                'formatter': 'standard',
                'stream': sys.stdout,
            },
            'syslogger-remote': {
                'level': 'INFO',
                'class': 'logging.handlers.SysLogHandler',
                'address': syslog_addr,
                'formatter': 'syslog_format',
            },
        },
        'loggers': {
            '': {
                'handlers': handlers,
                'level': 'DEBUG',
                'propagate': False
            },
            'pika': {
                'handlers': handlers,
                'level': 'WARNING',
                'propogate': True,
            }
        }
    }

    if dev_env:
        edx_file_loc = os.path.join(log_dir, edx_filename)
        logger_config['handlers'].update({
            'local': {
                'class': 'logging.handlers.RotatingFileHandler',
                'level': local_loglevel,
                'formatter': 'standard',
                'filename': edx_file_loc,
                'maxBytes': 1024 * 1024 * 2,
                'backupCount': 5,
            },
        })
    else:

        log_address = syslog_addr or '/dev/log'

        logger_config['handlers'].update({
            'local': {
                'level': local_loglevel,
                'class': 'logging.handlers.SysLogHandler',
                'address': log_address,
                'formatter': 'syslog_format',
                'facility': SysLogHandler.LOG_LOCAL0,
            },
        })

    return logger_config

@@ -2373,235 +2373,8 @@
  %7D,%0A
-            'syslogger-remote': %7B%0A                'level': 'INFO',%0A                'class': 'logging.handlers.SysLogHandler',%0A                'address': syslog_addr,%0A                'formatter': 'syslog_format',%0A            %7D,%0A
     
@@ -2372,32 +2372,32 @@
   %7D,%0A        %7D,%0A
+
         'loggers
@@ -2703,16 +2703,318 @@
 %0A    %7D%0A%0A
+    if syslog_addr:%0A        logger_config%5B'handlers'%5D.update(%7B%0A            'syslogger-remote': %7B%0A                'level': 'INFO',%0A                'class': 'logging.handlers.SysLogHandler',%0A                'address': syslog_addr,%0A                'formatter': 'syslog_format',%0A            %7D,%0A        %7D)%0A%0A
     if d
@@ -3454,58 +3454,8 @@
 se:%0A
-%0A        log_address = syslog_addr or '/dev/log'%0A%0A
     
@@ -3647,19 +3647,18 @@
 s': 
-log_address
+'/dev/log'
 ,%0A  
@@ -3693,32 +3693,32 @@
 syslog_format',%0A
-
                 
@@ -3762,32 +3762,295 @@
 %0A            %7D,%0A
+            'tracking': %7B%0A                'level': 'DEBUG',%0A                'class': 'logging.handlers.SysLogHandler',%0A                'address': '/dev/log',%0A                'facility': SysLogHandler.LOG_LOCAL1,%0A                'formatter': 'raw',%0A            %7D,%0A
         %7D)%0A%0A    

Update config