""" Loggly handlers =============== """ import uuid from owslogger.logger import DSNHandler from masters_registry import config class CeleryDSNHandler(DSNHandler): """Custom DSN Handler for processing celery-specific logging """ def emit(self, record): """Emit a record. Update the LogRecord with correlation-id to match Orchards format and send it to Loggly Args: record (LogRecord): the record to log. """ # Try to extract correlation ID from task args first try: correlation_id = record.args[0].correlation_id except: # noqa: E722 correlation_id = str(uuid.uuid1()) record.__dict__.update( correlation_id=correlation_id) return DSNHandler.emit(self, record) def get_full_message(self, record): """Get the full message for a record. Format the record message Args: record (LogRecord): the record to log. Return: mixed: the string or an object (dictionary). """ message = DSNHandler.get_full_message(self, record) if not record.exc_info: message = self.format(record) return message def initialize_celery_logging(logger, format=None, formatter=None): """Signal handler for hooking up Loggly with Celery workers Args: logger (Logger): Logger instance format (str): format string formatter (Formatter): optional formatter callable Returns: Logger """ logger.setLevel(config.CELERY_LOGGER_LEVEL) if not config.LOGGER_DSN: return dsn_handler = CeleryDSNHandler( config.LOGGER_DSN, config.ENVIRONMENT, config.SERVICE_NAME, config.SERVICE_VERSION) if formatter: dsn_handler.setFormatter(formatter(format)) logger.addHandler(dsn_handler) return logger