<commit_before>var util = require('util'),
    winston = require('winston'),
    morgan = require('morgan');

winston.transports.FileSoleLevel = function(options) {
    this.soleLevel = options.level || 'info';
    this.realLogger = new winston.transports.File(options);
};
util.inherits(winston.transports.FileSoleLevel, winston.Transport);
winston.transports.FileSoleLevel.prototype.log = function(level, msg, meta, callback) {
    if (level !== this.soleLevel) {
        callback(null, true);
    } else {
        this.realLogger.log(level, msg, meta, callback);
    }
};

var transports = {
    'file-problems': new winston.transports.File({
        name: 'file-problems',
        level: 'warn',
        filename: './logs/emapic-problems.log',
        handleExceptions: true,
        humanReadableUnhandledException: true,
        json: true,
        maxsize: 5242880, //5MB
        maxFiles: 10,
        colorize: false
    }),
    'file-info': new winston.transports.FileSoleLevel({
        name: 'file-info',
        level: 'info',
        filename: './logs/emapic-info.log',
        handleExceptions: false,
        json: true,
        maxsize: 5242880, //5MB
        maxFiles: 10,
        colorize: false
    }),
    'file-requests': new winston.transports.File({
        name: 'file-requests',
        level: 'verbose',
        filename: './logs/emapic-requests.log',
        handleExceptions: false,
        json: true,
        maxsize: 52428800, //50MB
        maxFiles: 10,
        colorize: false
    }),
    'console': new winston.transports.Console({
        name: 'console-all',
        level: 'debug',
        handleExceptions: true,
        json: false,
        colorize: true,
        timestamp: true
    })
};

module.exports = winston.loggers.internal = new winston.Logger({
    transports: [
        transports['file-problems'],
        transports['file-info'],
        transports.console
    ],
    exitOnError: false,
    emitErrs: false
});

var requestsLogger = winston.loggers.requests = new winston.Logger({
    transports: [
        transports['file-requests'],
        transports.console
    ],
    exitOnError: false,
    emitErrs: false
});

morgan.token('user-agent', function (req, res) {
    return req.headers['user-agent'] !== null ? req.headers['user-agent'].replace(/"/g, '\\"') : null;
});

module.exports.stream = {
    write: function(message, encoding) {
        var json;
        try {
            json = JSON.parse(message);
        } catch (ex) {
            winston.loggers.internal.error('Error while parsing HTTP request: ' + ex + ' || Message: ' + message);
            return;
        }
        if (json.remote_addr !== '127.0.0.1' && json.remote_addr !== 'localhost') {
            requestsLogger.verbose('Web request info', json);
        } else {
            requestsLogger.debug('Localhost web request info', json);
        }
    }
};<commit_msg>Fix a potential null pointer in logger<commit_after>var util = require('util'),
    winston = require('winston'),
    morgan = require('morgan');

winston.transports.FileSoleLevel = function(options) {
    this.soleLevel = options.level || 'info';
    this.realLogger = new winston.transports.File(options);
};
util.inherits(winston.transports.FileSoleLevel, winston.Transport);
winston.transports.FileSoleLevel.prototype.log = function(level, msg, meta, callback) {
    if (level !== this.soleLevel) {
        callback(null, true);
    } else {
        this.realLogger.log(level, msg, meta, callback);
    }
};

var transports = {
    'file-problems': new winston.transports.File({
        name: 'file-problems',
        level: 'warn',
        filename: './logs/emapic-problems.log',
        handleExceptions: true,
        humanReadableUnhandledException: true,
        json: true,
        maxsize: 5242880, //5MB
        maxFiles: 10,
        colorize: false
    }),
    'file-info': new winston.transports.FileSoleLevel({
        name: 'file-info',
        level: 'info',
        filename: './logs/emapic-info.log',
        handleExceptions: false,
        json: true,
        maxsize: 5242880, //5MB
        maxFiles: 10,
        colorize: false
    }),
    'file-requests': new winston.transports.File({
        name: 'file-requests',
        level: 'verbose',
        filename: './logs/emapic-requests.log',
        handleExceptions: false,
        json: true,
        maxsize: 52428800, //50MB
        maxFiles: 10,
        colorize: false
    }),
    'console': new winston.transports.Console({
        name: 'console-all',
        level: 'debug',
        handleExceptions: true,
        json: false,
        colorize: true,
        timestamp: true
    })
};

module.exports = winston.loggers.internal = new winston.Logger({
    transports: [
        transports['file-problems'],
        transports['file-info'],
        transports.console
    ],
    exitOnError: false,
    emitErrs: false
});

var requestsLogger = winston.loggers.requests = new winston.Logger({
    transports: [
        transports['file-requests'],
        transports.console
    ],
    exitOnError: false,
    emitErrs: false
});

morgan.token('user-agent', function (req, res) {
    return ('user-agent' in req.headers && req.headers['user-agent'] !== null) ? req.headers['user-agent'].replace(/"/g, '\\"') : null;
});

module.exports.stream = {
    write: function(message, encoding) {
        var json;
        try {
            json = JSON.parse(message);
        } catch (ex) {
            winston.loggers.internal.error('Error while parsing HTTP request: ' + ex + ' || Message: ' + message);
            return;
        }
        if (json.remote_addr !== '127.0.0.1' && json.remote_addr !== 'localhost') {
            requestsLogger.verbose('Web request info', json);
        } else {
            requestsLogger.debug('Localhost web request info', json);
        }
    }
};
{'fraction_non_alphanumeric': 0.14295462299556466, 'fraction_numerical': 0.012964858410098942, 'mean_word_length': 2.3753598157743236, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '3987445', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1773, 'n_words': 367}