<commit_before>import datetime
import json
import logging
import traceback
import zmq

VERSION = '0.0.1'

class ZMQHandlerError(Exception):
    pass

class ZMQHandler(logging.Handler):
    """A Django log handler that sends the exceptions to 0MQ"""

    # ZMQ socket
    socket = None

    def __init__(self, include_html=False):
        logging.Handler.__init__(self)
        self.include_html = include_html

        # Get socket instance
        if self.socket is None:
            self.socket = self.init_zmq_socket()

    def init_zmq_socket(self):
        """Initialize a zmq connection

        Returns a socket"""
        context = zmq.Context()

        socket = context.socket(zmq.PUB)
        socket.bind('tcp://*:5556')

        return socket

    def collect_message(self, record):
        """Collects message from record

        Returns a dictionary"""
        data = {}

        data['level'] = record.levelname

        data['datetime'] = datetime.datetime.now().isoformat()

        data['ip_address'] = record.request.META.get('REMOTE_ADDR')
        data['message'] = record.getMessage()

        if record.exc_info:
            data['stack_trace'] = '\n'.join(traceback.format_exception(*record.exc_info))
        else:
            data['stack_trace'] = 'No stack trace available'

        return data

    def format_message(self, message):
        """Formats the message to send via 0MQ

        Return a JSON string"""
        return json.dumps(message)

    def send_message(self, formatted_message):
        """Sends the message via ZMQ"""
        self.socket.send(formatted_message)

        # Message was sent successfully
        return True

    def emit(self, record):
        message = self.collect_message(record)
        formatted_message = self.format_message(message)

        message_was_sent = self.send_message(formatted_message)

        if not message_was_sent:
            error_message = 'Log could not be sent: {0}'.format(message)
            raise ZMQHandlerError(error_message)
<commit_msg>Use ipc sockets<commit_after>import datetime
import json
import logging
import traceback
import zmq

VERSION = '0.0.1'

class ZMQHandlerError(Exception):
    pass

class ZMQHandler(logging.Handler):
    """A Django log handler that sends the exceptions to 0MQ"""

    # ZMQ socket
    socket = None
    context = None

    def __init__(self, include_html=False):
        logging.Handler.__init__(self)
        self.include_html = include_html

        # Get socket instance
        if self.socket is None or self.context is None:
            self.init_zmq_socket()

    def init_zmq_socket(self):
        """Initialize a zmq connection

        Returns a socket"""
        self.context = zmq.Context()

        self.socket = self.context.socket(zmq.PUB)
        self.socket.bind('ipc:///tmp/zmqlogs')

        self.socket

    def collect_message(self, record):
        """Collects message from record

        Returns a dictionary"""
        data = {}

        data['level'] = record.levelname

        data['datetime'] = datetime.datetime.now().isoformat()

        data['ip_address'] = record.request.META.get('REMOTE_ADDR')
        data['message'] = record.getMessage()

        if record.exc_info:
            data['stack_trace'] = '\n'.join(traceback.format_exception(*record.exc_info))
        else:
            data['stack_trace'] = 'No stack trace available'

        return data

    def format_message(self, message):
        """Formats the message to send via 0MQ

        Return a JSON string"""
        return json.dumps(message)

    def send_message(self, formatted_message):
        """Sends the message via ZMQ

        Return true or false depending on error or not"""
        try:
            self.socket.send(formatted_message)
        except Exception:
            # Gotta catch them' all
            return False

        # Message was sent successfully
        return True

    def emit(self, record):
        message = self.collect_message(record)
        formatted_message = self.format_message(message)

        message_was_sent = self.send_message(formatted_message)

        if not message_was_sent:
            error_message = 'Log could not be sent: {0}'.format(message)
            raise ZMQHandlerError(error_message)

{'fraction_non_alphanumeric': 0.09680429204571962, 'fraction_numerical': 0.0037322136692325637, 'mean_word_length': 2.469255663430421, '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': '2295284', 'n_tokens_mistral': 1233, 'n_tokens_neox': 1164, 'n_words': 315}