<commit_before>import * as path from 'path';
import * as _ from 'lodash';
import { Logger as Winston, transports, LoggerInstance, CLILoggingLevel } from 'winston';
import { Response } from 'express';

import { getTokenInfo } from './tokenHandler';
import { IExtendedRequest as Request } from './models/http';

export class Logger extends Winston {

  constructor(filePath: string) {
    const fileName = path.basename(filePath);
    super({
      transports: [
        new transports.Console({
          label: `piikki${fileName ? `-${fileName}` : ''}`,
          timestamp: true,
        }),
      ],
    });

    this.setLevelForTransports(process.env.LOG_LEVEL || 'info');
  }

  public request(req: Request, res: Response) {
    this.debug('Request', {
      request: this.getRequestMetadata(req),
      response: this.getResponseMetadata(res),
    });
  }

  public errorRequest(req: Request, res: Response, err: Error) {
    this.error('Error request', {
      request: this.getRequestMetadata(req),
      response: this.getResponseMetadata(res),
      error: this.getErrorMetadata(err),
    });
  }

  private getRequestMetadata(req: Request) {
    return {
      response_time: _.get(req, 'insights.startTime') ? (Date.now() - req.insights.startTime) : 0,
      token: getTokenInfo(req),
    };
  }

  private getResponseMetadata(res: Response) {
    return {
      status_code: res.statusCode,
    };
  }

  private getErrorMetadata(err: Error) {
    return {
      name: err.name,
      message: err.message,
    };
  }<commit_msg>Log more data about request<commit_after>import * as path from 'path';
import * as _ from 'lodash';
import { Logger as Winston, transports, LoggerInstance, CLILoggingLevel } from 'winston';
import { Response } from 'express';

import { getTokenInfo } from './tokenHandler';
import { IExtendedRequest as Request } from './models/http';

export class Logger extends Winston {

  constructor(filePath: string) {
    const fileName = path.basename(filePath);
    super({
      transports: [
        new transports.Console({
          label: `piikki${fileName ? `-${fileName}` : ''}`,
          timestamp: true,
        }),
      ],
    });

    this.setLevelForTransports(process.env.LOG_LEVEL || 'info');
  }

  public request(req: Request, res: Response) {
    this.debug('Request', {
      request: this.getRequestMetadata(req),
      response: this.getResponseMetadata(res),
    });
  }

  public errorRequest(req: Request, res: Response, err: Error) {
    this.error('Error request', {
      request: this.getRequestMetadata(req),
      response: this.getResponseMetadata(res),
      error: this.getErrorMetadata(err),
    });
  }

  private getRequestMetadata(req: Request) {
    return {
      url: req.originalUrl,
      ip: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
      response_time: _.get(req, 'insights.startTime') ? (Date.now() - req.insights.startTime) : 0,
      token: getTokenInfo(req),
    };
  }

  private getResponseMetadata(res: Response) {
    return {
      status_code: res.statusCode,
    };
  }

  private getErrorMetadata(err: Error) {
    return {
      name: err.name,
      message: err.message,
    };
  }
{'fraction_non_alphanumeric': 0.1357098186366479, 'fraction_numerical': 0.0006253908692933083, 'mean_word_length': 3.127741935483871, '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': '2916754', 'n_tokens_mistral': 983, 'n_tokens_neox': 959, 'n_words': 217}