<commit_before>import chalk, { Chalk, ColorSupport } from 'chalk';
import { NextFunction, Request, Response } from 'express';
import * as onFinished from 'on-finished';
import { logger } from 'winston-pnp-logger';

export class RequestLogger {
    public static ignoredUrls = ['/modules', '/images', '/fonts', '/stylesheets', '/scripts', '/favicon.ico'];
    public static arrow = chalk.white('->');

    public static logRequest(): any {
        return function log(request: Request, response: Response, next: NextFunction) {

            const requestStartTime = Date.now();

            // Runs when the request has finished.
            onFinished(response, async (_err, endResponse: Response) => {

                const ignoredMatch = RequestLogger.ignoredUrls.filter(
                    (ignoredEntry) => request.originalUrl.startsWith(ignoredEntry)).length;

                if (!ignoredMatch || (endResponse.statusCode !== 200 && endResponse.statusCode !== 304)) {

                    const statusColor = RequestLogger.getStatusColor(endResponse.statusCode);
                    const status = statusColor(`${endResponse.statusCode} ${endResponse.statusMessage}`);

                    const ip = RequestLogger.getIp(request);
                    const identifier = `${ip} (${chalk.white(request.sessionID!)})`;

                    const requestText = `${request.method} ${request.originalUrl}`;

                    const requestDuration = Date.now() - requestStartTime;
                    const arrow = RequestLogger.arrow;
                    const logContent = `${identifier} ${arrow} ${requestText} ${arrow} ${status}, ${requestDuration}ms`;
                    if (endResponse.statusCode >= 500) {
                        logger.error(logContent);
                    } else if (endResponse.statusCode >= 400) {
                        logger.warn(logContent);
                    } else {
                        logger.debug(logContent);
                    }<commit_msg>Do not log requests to static URLs and files<commit_after>import chalk, { Chalk, ColorSupport } from 'chalk';
import { NextFunction, Request, Response } from 'express';
import * as onFinished from 'on-finished';
import { logger } from 'winston-pnp-logger';

export class RequestLogger {
    public static ignoredUrls = ['/modules', '/images', '/fonts', '/stylesheets', '/scripts', '/favicon.ico'];
    public static ignoredExtension = ['.ico', '.js', '.css', '.png', '.jpg', '.svg'];
    public static arrow = chalk.white('->');

    public static logRequest(): any {
        return function log(request: Request, response: Response, next: NextFunction) {

            const requestStartTime = Date.now();

            // Runs when the request has finished.
            onFinished(response, async (_err, endResponse: Response) => {

                const ignoredUrlMatch = RequestLogger.ignoredUrls.filter(
                    (ignoredUrl) => request.originalUrl.startsWith(ignoredUrl)).length;

                const ignoredExtensionMatch = RequestLogger.ignoredExtension.filter(
                    (ignoredExtension) => request.originalUrl.endsWith(ignoredExtension)).length;

                // Do not log requests to static URLs and files unless their status code is not OK.
                if ((!ignoredExtensionMatch && !ignoredUrlMatch) || (endResponse.statusCode !== 200 && endResponse.statusCode !== 304)) {

                    const statusColor = RequestLogger.getStatusColor(endResponse.statusCode);
                    const status = statusColor(`${endResponse.statusCode} ${endResponse.statusMessage}`);

                    const ip = RequestLogger.getIp(request);
                    const identifier = `${ip} (${chalk.white(request.sessionID!)})`;

                    const requestText = `${request.method} ${request.originalUrl}`;

                    const requestDuration = Date.now() - requestStartTime;
                    const arrow = RequestLogger.arrow;
                    const logContent = `${identifier} ${arrow} ${requestText} ${arrow} ${status}, ${requestDuration}ms`;
                    if (endResponse.statusCode >= 500) {
                        logger.error(logContent);
                    } else if (endResponse.statusCode >= 400) {
                        logger.warn(logContent);
                    } else {
                        logger.debug(logContent);
                    }
{'fraction_non_alphanumeric': 0.12140430351075877, 'fraction_numerical': 0.005436013590033975, 'mean_word_length': 2.2233576642335766, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '2960495', 'n_tokens_mistral': 1123, 'n_tokens_neox': 1037, 'n_words': 275}