logsettings.py 5.09 KB
Newer Older
1
import os
2 3
import platform
import sys
John Jarvis committed
4
from logging.handlers import SysLogHandler
5

6
LOG_LEVELS = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
7

8

9 10
def get_logger_config(log_dir,
                      logging_env="no_env",
11 12 13
                      tracking_filename="tracking.log",
                      edx_filename="edx.log",
                      dev_env=False,
14
                      syslog_addr=None,
15
                      debug=False,
16
                      local_loglevel='INFO',
17 18
                      console_loglevel=None,
                      service_variant=None):
19

20 21 22
    """

    Return the appropriate logging config dictionary. You should assign the
23
    result of this to the LOGGING var in your settings. The reason it's done
24
    this way instead of registering directly is because I didn't want to worry
25
    about resetting the logging state if this is called multiple times when
26 27 28 29 30 31 32 33 34
    settings are extended.

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

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

    """
35

36
    # Revert to INFO if an invalid string is passed in
37
    if local_loglevel not in LOG_LEVELS:
38 39
        local_loglevel = 'INFO'

40 41 42
    if console_loglevel is None or console_loglevel not in LOG_LEVELS:
        console_loglevel = 'DEBUG' if debug else 'INFO'

43 44 45 46 47
    if service_variant is None:
        # default to a blank string so that if SERVICE_VARIANT is not
        # set we will not log to a sub directory
        service_variant = ''

48
    hostname = platform.node().split(".")[0]
49 50
    syslog_format = ("[service_variant={service_variant}]"
                     "[%(name)s][env:{logging_env}] %(levelname)s "
51
                     "[{hostname}  %(process)d] [%(filename)s:%(lineno)d] "
52 53 54
                     "- %(message)s").format(service_variant=service_variant,
                                             logging_env=logging_env,
                                             hostname=hostname)
55

56
    handlers = ['console', 'local'] if debug else ['console',
57
                                                   'syslogger-remote', 'local']
58

59
    logger_config = {
60
        'version': 1,
61
        'disable_existing_loggers': False,
62 63
        'formatters': {
            'standard': {
64 65
                'format': '%(asctime)s %(levelname)s %(process)d '
                          '[%(name)s] %(filename)s:%(lineno)d - %(message)s',
66
            },
67 68
            'syslog_format': {'format': syslog_format},
            'raw': {'format': '%(message)s'},
69
        },
70 71
        'handlers': {
            'console': {
72
                'level': console_loglevel,
73 74
                'class': 'logging.StreamHandler',
                'formatter': 'standard',
75
                'stream': sys.stderr,
76
            },
77 78 79 80 81
            'syslogger-remote': {
                'level': 'INFO',
                'class': 'logging.handlers.SysLogHandler',
                'address': syslog_addr,
                'formatter': 'syslog_format',
82
            },
83
            'newrelic': {
84
                'level': 'ERROR',
85
                'class': 'lms.lib.newrelic_logging.NewRelicHandler',
86 87
                'formatter': 'raw',
            }
88
        },
89 90 91 92 93
        'loggers': {
            'tracking': {
                'handlers': ['tracking'],
                'level': 'DEBUG',
                'propagate': False,
94
            },
95 96 97 98
            '': {
                'handlers': handlers,
                'level': 'DEBUG',
                'propagate': False
99 100
            },
        }
101
    }
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

    if dev_env:
        tracking_file_loc = os.path.join(log_dir, tracking_filename)
        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,
            },
            'tracking': {
                'level': 'DEBUG',
                'class': 'logging.handlers.RotatingFileHandler',
                'filename': tracking_file_loc,
                'formatter': 'raw',
                'maxBytes': 1024 * 1024 * 2,
                'backupCount': 5,
            },
        })
    else:
125 126 127
        # for production environments we will only
        # log INFO and up
        logger_config['loggers']['']['level'] = 'INFO'
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        logger_config['handlers'].update({
            'local': {
                'level': local_loglevel,
                'class': 'logging.handlers.SysLogHandler',
                'address': '/dev/log',
                'formatter': 'syslog_format',
                'facility': SysLogHandler.LOG_LOCAL0,
            },
            'tracking': {
                'level': 'DEBUG',
                'class': 'logging.handlers.SysLogHandler',
                'address': '/dev/log',
                'facility': SysLogHandler.LOG_LOCAL1,
                'formatter': 'raw',
            },
        })

    return logger_config