1
0
mirror of https://github.com/ansible/awx.git synced 2024-10-31 23:51:09 +03:00

modularize logging config in proper Django fashion

This commit is contained in:
AlanCoding 2017-02-09 17:16:56 -05:00
parent fe29446298
commit 91031cbbc8
3 changed files with 74 additions and 33 deletions

View File

@ -2,7 +2,7 @@
from django.apps import AppConfig from django.apps import AppConfig
# from django.core import checks # from django.core import checks
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils.log import configure_logging from awx.main.utils.handlers import configure_external_logger
from django.conf import settings from django.conf import settings
@ -15,10 +15,4 @@ class ConfConfig(AppConfig):
self.module.autodiscover() self.module.autodiscover()
from .settings import SettingsWrapper from .settings import SettingsWrapper
SettingsWrapper.initialize() SettingsWrapper.initialize()
if settings.LOG_AGGREGATOR_ENABLED: configure_external_logger(settings)
LOGGING_DICT = settings.LOGGING
LOGGING_DICT['handlers']['http_receiver']['class'] = 'awx.main.utils.handlers.HTTPSHandler'
if 'awx' in settings.LOG_AGGREGATOR_LOGGERS:
if 'http_receiver' not in LOGGING_DICT['loggers']['awx']['handlers']:
LOGGING_DICT['loggers']['awx']['handlers'] += ['http_receiver']
configure_logging(settings.LOGGING_CONFIG, LOGGING_DICT)

View File

@ -32,7 +32,7 @@ import pexpect
# Celery # Celery
from celery import Task, task from celery import Task, task
from celery.signals import celeryd_init, worker_ready from celery.signals import celeryd_init, worker_process_init
from celery import current_app from celery import current_app
# Django # Django
@ -54,6 +54,7 @@ from awx.main.task_engine import TaskEnhancer
from awx.main.utils import (get_ansible_version, get_ssh_version, decrypt_field, update_scm_url, from awx.main.utils import (get_ansible_version, get_ssh_version, decrypt_field, update_scm_url,
check_proot_installed, build_proot_temp_dir, wrap_args_with_proot, check_proot_installed, build_proot_temp_dir, wrap_args_with_proot,
get_system_task_capacity, OutputEventFilter, parse_yaml_or_json) get_system_task_capacity, OutputEventFilter, parse_yaml_or_json)
from awx.main.utils.handlers import configure_external_logger
from awx.main.consumers import emit_channel_notification from awx.main.consumers import emit_channel_notification
__all__ = ['RunJob', 'RunSystemJob', 'RunProjectUpdate', 'RunInventoryUpdate', __all__ = ['RunJob', 'RunSystemJob', 'RunProjectUpdate', 'RunInventoryUpdate',
@ -86,26 +87,10 @@ def celery_startup(conf=None, **kwargs):
logger.error("Failed to rebuild schedule {}: {}".format(sch, e)) logger.error("Failed to rebuild schedule {}: {}".format(sch, e))
def _setup_tower_logger(): @worker_process_init.connect
global logger
from django.utils.log import configure_logging
LOGGING_DICT = settings.LOGGING
if settings.LOG_AGGREGATOR_ENABLED:
LOGGING_DICT['handlers']['http_receiver']['class'] = 'awx.main.utils.handlers.HTTPSHandler'
LOGGING_DICT['handlers']['http_receiver']['async'] = False
if 'awx' in settings.LOG_AGGREGATOR_LOGGERS:
if 'http_receiver' not in LOGGING_DICT['loggers']['awx']['handlers']:
LOGGING_DICT['loggers']['awx']['handlers'] += ['http_receiver']
configure_logging(settings.LOGGING_CONFIG, LOGGING_DICT)
logger = logging.getLogger('awx.main.tasks')
@worker_ready.connect
def task_set_logger_pre_run(*args, **kwargs): def task_set_logger_pre_run(*args, **kwargs):
cache.close() cache.close()
if settings.LOG_AGGREGATOR_ENABLED: configure_external_logger(settings, is_startup=False)
_setup_tower_logger()
logger.debug('Custom Tower logger configured for worker process.')
def _uwsgi_reload(): def _uwsgi_reload():
@ -121,7 +106,7 @@ def _uwsgi_reload():
def _reset_celery_logging(): def _reset_celery_logging():
# Worker logger reloaded, now send signal to restart pool # Send signal to restart thread pool
app = current_app._get_current_object() app = current_app._get_current_object()
app.control.broadcast('pool_restart', arguments={'reload': True}, app.control.broadcast('pool_restart', arguments={'reload': True},
destination=['celery@{}'.format(settings.CLUSTER_HOST_ID)], reply=False) destination=['celery@{}'.format(settings.CLUSTER_HOST_ID)], reply=False)

View File

@ -12,8 +12,11 @@ import traceback
from requests_futures.sessions import FuturesSession from requests_futures.sessions import FuturesSession
# custom # AWX
from django.conf import settings as django_settings from awx.main.utils.formatters import LogstashFormatter
__all__ = ['HTTPSNullHandler', 'BaseHTTPSHandler', 'HTTPSHandler', 'configure_external_logger']
# AWX external logging handler, generally designed to be used # AWX external logging handler, generally designed to be used
# with the accompanying LogstashHandler, derives from python-logstash library # with the accompanying LogstashHandler, derives from python-logstash library
@ -40,7 +43,7 @@ def unused_callback(sess, resp):
class HTTPSNullHandler(logging.NullHandler): class HTTPSNullHandler(logging.NullHandler):
"Placeholder null handler to allow loading without database access" "Placeholder null handler to allow loading without database access"
def __init__(self, host, **kwargs): def __init__(self, *args, **kwargs):
return super(HTTPSNullHandler, self).__init__() return super(HTTPSNullHandler, self).__init__()
@ -167,5 +170,64 @@ class BaseHTTPSHandler(logging.Handler):
class HTTPSHandler(object): class HTTPSHandler(object):
def __new__(cls, *args, **kwargs): def __new__(cls, settings_module, *args, **kwargs):
return BaseHTTPSHandler.from_django_settings(django_settings, *args, **kwargs) return BaseHTTPSHandler.from_django_settings(settings_module, *args, **kwargs)
def add_or_remove_logger(address, instance, adding=True):
specific_logger = logging.getLogger(address)
i_occurance = None
for i in range(len(specific_logger.handlers)):
if isinstance(specific_logger.handlers[i], (HTTPSNullHandler, BaseHTTPSHandler)):
i_occurance = i
break
if i_occurance is None and not adding:
return
elif i_occurance is None:
specific_logger.handlers.append(instance)
else:
specific_logger.handlers[i_occurance] = instance
def configure_external_logger(settings_module, async_flag=True, is_startup=True):
is_enabled = settings_module.LOG_AGGREGATOR_ENABLED
if is_startup and (not is_enabled):
# Pass-through if external logging not being used
return
if is_enabled:
instance = HTTPSHandler(settings_module, async=async_flag)
instance.setFormatter(LogstashFormatter())
else:
instance = HTTPSNullHandler()
add_or_remove_logger('awx.analytics', instance, adding=is_enabled)
add_or_remove_logger('awx', instance, adding=(is_enabled and 'awx' in settings_module.LOG_AGGREGATOR_LOGGERS))
def configure_external_logger_old(settings_module, async_flag=True, is_startup=True):
from django.utils.log import configure_logging
is_enabled = settings_module.LOG_AGGREGATOR_ENABLED
if is_startup and (not is_enabled):
# Pass-through if external logging not being used
return
LOGGING_DICT = settings_module.LOGGING
if is_enabled:
LOGGING_DICT['handlers']['http_receiver']['class'] = 'awx.main.utils.handlers.BaseHTTPSHandler'
if not async_flag:
LOGGING_DICT['handlers']['http_receiver']['async'] = False
else:
LOGGING_DICT['handlers']['http_receiver']['async'] = True
for param, django_setting_name in PARAM_NAMES.items():
LOGGING_DICT['handlers']['http_receiver'][param] = getattr(settings_module, django_setting_name, None)
if 'awx' in settings_module.LOG_AGGREGATOR_LOGGERS:
if 'http_receiver' not in LOGGING_DICT['loggers']['awx']['handlers']:
LOGGING_DICT['loggers']['awx']['handlers'] += ['http_receiver']
# External logging not enabled, but removal of existing configuration needed
configure_logging(settings_module.LOGGING_CONFIG, LOGGING_DICT)