<commit_before>import dgram from 'dgram';
import util from 'util';
import validator from 'validator';

/**
 * https://tools.ietf.org/html/rfc5424
 *
 */
export default class Syslog {

    /**
     *
     * @param {Object} options
     */
    constructor(options) {
        options = options || {};
        this._host = options.host || '127.0.0.1';
        this._port = parseInt(options.port, 10) || 514;

        this._sender = dgram.createSocket('udp4');
        this._sender.unref();
    }

    /**
     *
     * @param {number} facility
     * @param {number} severity
     * @param {string} hostname
     * @param {string} application
     * @param {string} date
     * @param {string} message
     */
    write(facility, severity, hostname, application, date, message) { // eslint-disable-line max-params
        const priority = this.calculatePri(facility, severity);
        const msg = this.cleanMessage(message);
        const parts = [];

        // if msg after cleanup is empty, do not send anything..
        if (msg.length === 0) {
            return;
        }

        // formatting header
        const header = util.format(
            '<%d>%s',
            priority,
            date
        );

        parts.push(header);
        if (hostname.length > 0) {
            parts.push(hostname);
        }

        if (application.length > 0) {
            parts.push(util.format('%s:', application));
        }

        parts.push(msg);
        const output = util.format.apply(this, parts);

        this._write(output);
    }

    /**
     * https://tools.ietf.org/html/rfc5424#section-6.4
     * @param {string} message
     */
    cleanMessage(message) {
        return validator.stripLow(message, true);
    }

    /**
     * https://tools.ietf.org/html/rfc5424#section-6.2.1
     * (facility * 8) + severity
     *
     * @param {number} facility
     * @param {number} severity
     */
    calculatePri(facility, severity) {
        return (facility * 8 + severity);
    }

    /**
     *
     * @param {string} output
     * @private
     */
    _write(output) {
        this._sender.send(output, 0, output.length, this._port, this._host);
    }
};
<commit_msg>Recreate UDP socket in case of error<commit_after>import dgram from 'dgram';
import util from 'util';
import validator from 'validator';

/**
 * https://tools.ietf.org/html/rfc5424
 *
 */
export default class Syslog {

    /**
     *
     * @param {Object} options
     */
    constructor(options) {
        options = options || {};
        this._host = options.host || '127.0.0.1';
        this._port = parseInt(options.port, 10) || 514;

        this.initSocket();
    }

    /**
     * Initialize server socket, catch error event re-create client socket.
     */
    initSocket() {
        this._sender = dgram.createSocket('udp4');
        this._sender.unref();

        this._sender.on('error', () => {
            this._sender = null;
            this.initSocket();
        });
    }

    /**
     *
     * @param {number} facility
     * @param {number} severity
     * @param {string} hostname
     * @param {string} application
     * @param {string} date
     * @param {string} message
     */
    write(facility, severity, hostname, application, date, message) { // eslint-disable-line max-params
        const priority = this.calculatePri(facility, severity);
        const msg = this.cleanMessage(message);
        const parts = [];

        // if msg after cleanup is empty, do not send anything..
        if (msg.length === 0) {
            return;
        }

        // formatting header
        const header = util.format(
            '<%d>%s',
            priority,
            date
        );

        parts.push(header);
        if (hostname.length > 0) {
            parts.push(hostname);
        }

        if (application.length > 0) {
            parts.push(util.format('%s:', application));
        }

        parts.push(msg);
        const output = util.format.apply(this, parts);

        this._write(output);
    }

    /**
     * https://tools.ietf.org/html/rfc5424#section-6.4
     * @param {string} message
     */
    cleanMessage(message) {
        return validator.stripLow(message, true);
    }

    /**
     * https://tools.ietf.org/html/rfc5424#section-6.2.1
     * (facility * 8) + severity
     *
     * @param {number} facility
     * @param {number} severity
     */
    calculatePri(facility, severity) {
        return (facility * 8 + severity);
    }

    /**
     *
     * @param {string} output
     * @private
     */
    _write(output) {
        this._sender.send(output, 0, output.length, this._port, this._host);
    }
};

{'fraction_non_alphanumeric': 0.14340918849291542, 'fraction_numerical': 0.015027908973808502, 'mean_word_length': 1.8495412844036698, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6162232', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1406, 'n_words': 301}