<commit_before>import logging

from .models import Hit

class StatisticsHandler(logging.Handler):
    def emit(self, record):
        hit = Hit(
            session_key = record.session_key,
            user_agent = record.user_agent,
            device_id = record.device_id,
            ip_address = record.ip_address,
            referer = record.referer,
            full_path = record.full_path,
            requested = record.requested,
            response_time = record.response_time,
            local_name = record.local_name,
            view_name = record.view_name,
            status_code = record.status_code,
            redirect_to = record.redirect_to,
            traceback = getattr(record, 'traceback', None),
        )
        hit.save()

def configure_logging(conf):
    if getattr(conf, 'log_to_database', True):
       logger = logging.getLogger('molly.stats.requests')
       logger.addHandler(StatisticsHandler())
<commit_msg>In the molly.stats app, when the request has bailed out due to a database exception saving our Hit isn't going to work. We now quietly consume any exception and log that it happened.<commit_after>import logging

from .models import Hit

logger = logging.getLogger('molly.stats.db')

class StatisticsHandler(logging.Handler):
    def emit(self, record):
        hit = Hit(
            session_key = record.session_key,
            user_agent = record.user_agent,
            device_id = record.device_id,
            ip_address = record.ip_address,
            referer = record.referer,
            full_path = record.full_path,
            requested = record.requested,
            response_time = record.response_time,
            local_name = record.local_name,
            view_name = record.view_name,
            status_code = record.status_code,
            redirect_to = record.redirect_to,
            traceback = getattr(record, 'traceback', None),
        )
        try:
            hit.save()
        except Exception, e:
            # Likely there was some sort of database exception, so this really
            # isn't going to work. We'll log it in case anyone's interested.
            logger.error("Couldn't save hit", extra={'hit': hit})

def configure_logging(conf):
    if getattr(conf, 'log_to_database', True):
       logger = logging.getLogger('molly.stats.requests')
       logger.addHandler(StatisticsHandler())

{'fraction_non_alphanumeric': 0.08664713269150272, 'fraction_numerical': 0.0, 'mean_word_length': 2.3333333333333335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6176617', 'n_tokens_mistral': 644, 'n_tokens_neox': 644, 'n_words': 167}