<commit_before>"""
Log actions to Elastic Search (via UDP)
"""

import datetime as _datetime
import json as _json
import logging as _logging
import socket as _socket

try:
    from django.conf import settings as _django_settings
except ImportError as e:
    _django_settings = None


LOG = _logging.getLogger(__name__)


DEFAULT = {
    'host': 'localhost',
    'port': 9700,
    'protocol': 'UDP',
    'index': 'log',
    'datestamp_index': False,
    'type': 'record',
    }


if _django_settings and _django_settings.configured:
    for key,value in DEFAULT.items():
        django_key = 'ELASTIC_SEARCH_LOGGING_{}'.format(key.upper())
        DEFAULT[key] = getattr(_django_settings, django_key, value)


### These have dumb names, but we might want them in some form? ###
def log_request(request):
    log(request.user, 'request', body=request.read())

def realy_quick_log(request):
    log(request.user, request.get_full_path())

def quick_log(request, what, **kwargs):
    log(request.user, what, **kwargs)



#-----------------------------------------------------------------------------#
# Real stuff
#-----------------------------------------------------------------------------#
def log(who, what, **kwargs):
    """
    This function combines in arguments, possibly setting an index,
    adds a consistant timestamp and hands it off to ElasticSearch
    """
    payload = {
        'who': who,
        'what': what,
        'when': _datetime.datetime.utcnow().isoformat()
    }

    for key, value in kwargs.iteritems():
        payload[key] = value

    emit(payload=payload, **kwargs)


def emit(payload, host=None, port=None, protocol=None,<commit_msg>performance.py: Just do timestamping in log()<commit_after>"""
Log actions to Elastic Search (via UDP)
"""

import datetime as _datetime
import json as _json
import logging as _logging
import socket as _socket

try:
    from django.conf import settings as _django_settings
except ImportError as e:
    _django_settings = None


LOG = _logging.getLogger(__name__)


DEFAULT = {
    'host': 'localhost',
    'port': 9700,
    'protocol': 'UDP',
    'index': 'log',
    'datestamp_index': False,
    'type': 'record',
    }


if _django_settings and _django_settings.configured:
    for key,value in DEFAULT.items():
        django_key = 'ELASTIC_SEARCH_LOGGING_{}'.format(key.upper())
        DEFAULT[key] = getattr(_django_settings, django_key, value)


### These have dumb names, but we might want them in some form? ###
def log_request(request):
    log(request.user, 'request', body=request.read())

def realy_quick_log(request):
    log(request.user, request.get_full_path())

def quick_log(request, what, **kwargs):
    log(request.user, what, **kwargs)



#-----------------------------------------------------------------------------#
# Real stuff
#-----------------------------------------------------------------------------#
def log(**kwargs):
    """Log an arbitrary payload dictionary to Elastic Search

    Uses the default connection configuration.  If you need to
    override any of them, build your payload dict by hand and use
    emit() instead.
    """
    kwargs['@timestamp'] = _datetime.datetime.utcnow().isoformat()
    kwargs['@version'] = 1
    emit(payload=kwargs)


def emit(payload, host=None, port=None, protocol=None,
{'fraction_non_alphanumeric': 0.203983101991551, 'fraction_numerical': 0.002715751357875679, 'mean_word_length': 2.572198275862069, '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': '1723009', 'n_tokens_mistral': 954, 'n_tokens_neox': 928, 'n_words': 283}