Commit 09b27d17 by Oleg Marshev

Add logging to syslog.

parent 37bb8a6f
...@@ -47,61 +47,6 @@ STATIC_URL = '/static/' ...@@ -47,61 +47,6 @@ STATIC_URL = '/static/'
WSGI_APPLICATION = 'notesserver.wsgi.application' WSGI_APPLICATION = 'notesserver.wsgi.application'
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'stream': sys.stderr,
'formatter': 'standard',
},
},
'formatters': {
'standard': {
'format': '%(asctime)s %(levelname)s %(process)d [%(name)s] %(filename)s:%(lineno)d - %(message)s',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': 'ERROR',
'propagate': False,
},
'notesserver': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True
},
'notesapi.v1.views': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True
},
'elasticsearch.trace': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': False
},
'elasticsearch': {
'handlers': ['console'],
'level': 'WARNING',
'propagate': True
},
'urllib3': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True
},
'notesapi.v1.permissions': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True
},
},
}
REST_FRAMEWORK = { REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [ 'DEFAULT_PERMISSION_CLASSES': [
'notesapi.v1.permissions.HasAccessToken' 'notesapi.v1.permissions.HasAccessToken'
......
from .common import * from .common import *
from notesserver.settings.logger import get_logger_config
DEBUG = True DEBUG = True
...@@ -10,3 +11,5 @@ DATABASES = { ...@@ -10,3 +11,5 @@ DATABASES = {
'NAME': 'default.db', 'NAME': 'default.db',
} }
} }
LOGGING = get_logger_config(debug=DEBUG, dev_env=True, local_loglevel='DEBUG')
"""
Logging configuration
"""
import os
import platform
import sys
from logging.handlers import SysLogHandler
def get_logger_config(log_dir='/var/tmp',
logging_env="no_env",
edx_filename="edx.log",
dev_env=False,
debug=False,
local_loglevel='INFO',
service_variant='insights'):
"""
Return the appropriate logging config dictionary. You should assign the
result of this to the LOGGING var in your settings.
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)
if debug:
handlers = ['console']
else:
handlers = ['local']
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,
},
},
'loggers': {
'django': {
'handlers': handlers,
'propagate': True,
'level': 'INFO'
},
"elasticsearch.trace": {
'handlers': handlers,
'level': 'DEBUG',
'propagate': False,
},
'': {
'handlers': handlers,
'level': 'DEBUG',
'propagate': False
},
}
}
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:
logger_config['handlers'].update({
'local': {
'level': local_loglevel,
'class': 'logging.handlers.SysLogHandler',
# Use a different address for Mac OS X
'address': '/var/run/syslog' if sys.platform == "darwin" else '/dev/log',
'formatter': 'syslog_format',
'facility': SysLogHandler.LOG_LOCAL0,
},
})
return logger_config
from .common import * from .common import *
from notesserver.settings.logger import get_logger_config
DATABASES = { DATABASES = {
'default': { 'default': {
...@@ -12,32 +13,4 @@ INSTALLED_APPS += ('django_nose',) ...@@ -12,32 +13,4 @@ INSTALLED_APPS += ('django_nose',)
ES_INDEXES = {'default': 'notes_index_test'} ES_INDEXES = {'default': 'notes_index_test'}
LOGGING = { LOGGING = get_logger_config()
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'stream': sys.stderr,
'formatter': 'standard',
},
},
'formatters': {
'standard': {
'format': '%(asctime)s %(levelname)s %(process)d [%(name)s] %(filename)s:%(lineno)d - %(message)s',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': 'ERROR',
'propagate': False,
},
"elasticsearch.trace": {
'handlers': ['console'],
'level': 'ERROR',
'propagate': False,
}
},
}
import yaml import yaml
from .common import * # pylint: disable=unused-wildcard-import, wildcard-import from .common import * # pylint: disable=unused-wildcard-import, wildcard-import
from notesserver.settings.logger import get_logger_config
LOGGING = get_logger_config()
############################################################################### ###############################################################################
# Explicitly declare here in case someone changes common.py. # Explicitly declare here in case someone changes common.py.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment