<commit_before>import prettyjson from 'prettyjson'
import chalk from 'chalk'
import _ from 'lodash/fp'
import { neatJSON } from '../vendor/neat-json'
import { pd } from 'pretty-data'
import dateFormat from 'dateformat'

  const now = new Date()
export function logRequest (err, req, res, opts) {
  const console = opts.console

  function divider (text, color = chalk.cyan.dim) {
    const divLine = color(_.repeat(chalk.stripColor(text).length, '*'))
    return {
      begin: () => {
        console.log(divLine)
        console.log(text)
      },
      end: () => {
        console.log(text)
        console.log(divLine)
      }
    }
  }

  const proxyUrl = req.__CLS_PROXY_URL__ || ''
  const proxyArrow = chalk.white.bold(' --> ')
  const pathLine = `${req.method} ${req.originalUrl}`
  const div = !err
    ? divider(chalk.yellow.bold(pathLine) + (proxyUrl ? proxyArrow + chalk.yellow.bold(proxyUrl) : ''))
    : divider(chalk.red.bold(pathLine) + (proxyUrl ? proxyArrow + chalk.red.bold(proxyUrl) : '') + chalk.red.bold('  *error*'), chalk.red.dim)
  console.log()
  div.begin()
  const renderParams = (obj) => prettyjson.render(obj, { defaultIndentation: 2 }, 2)
  const headers = req.headers

  log(chalk.magenta('meta' + ':'))
  log(renderParams({
    date: dateFormat(now, opts.dateFormat || "yyyy-mm-dd'T'HH:MM:sso")
  }))
  console.log(chalk.magenta('headers' + ':'))
  console.log(renderParams(headers))

  if (_.isEmpty(req.query)) {
    console.log(chalk.magenta('query: (empty)'))
  } else {
    console.log(chalk.magenta('query:'))
    console.log(renderParams(req.query))
  }

  switch (req.bodyType) {
    case 'empty':
      console.log(chalk.magenta('body: (empty)'))
      break
    case 'raw':
      console.log(chalk.magenta('body: ') + chalk.yellow(`(parsed as raw string by console-log-server since content-type is '${headers['content-type']}'. Forgot to set it correctly?)`))
      console.log(chalk.white(req.body.toString()))
      break
    case 'json':
      console.log(chalk.magenta('body (json): '))
      console.log(chalk.green(neatJSON(req.body, {
        wrap: 40,
        aligned: true,
        afterComma: 1,
        afterColon1: 1,
        afterColonN: 1
      })))
      break
    case 'url':<commit_msg>Change divider to contain date and remove meta<commit_after>import prettyjson from 'prettyjson'
import chalk from 'chalk'
import _ from 'lodash/fp'
import { neatJSON } from '../vendor/neat-json'
import { pd } from 'pretty-data'
import dateFormat from 'dateformat'

export function logRequest (err, req, res, opts) {
  const console = opts.console
  const now = dateFormat(new Date(), opts.dateFormat)

  function divider (text, color = chalk.white.dim) {
    const divLine = color.bold(`>> [req:1] [${now}]`)
    return {
      begin: () => {
        console.log(divLine)
        console.log(text)
      },
      end: () => {
        console.log(text)
        console.log(divLine)
      }
    }
  }

  const proxyUrl = req.__CLS_PROXY_URL__ || ''
  const proxyArrow = chalk.white.bold(' --> ')
  const pathLine = `${req.method} ${req.originalUrl}`
  const div = !err
    ? divider(chalk.yellow.bold(pathLine) + (proxyUrl ? proxyArrow + chalk.yellow.bold(proxyUrl) : ''))
    : divider(chalk.red.bold(pathLine) + (proxyUrl ? proxyArrow + chalk.red.bold(proxyUrl) : '') + chalk.red.bold('  *error*'), chalk.red.dim)
  console.log()
  div.begin()
  const renderParams = (obj) => prettyjson.render(obj, { defaultIndentation: 2 }, 2)
  const headers = req.headers
  console.log(chalk.magenta('headers' + ':'))
  console.log(renderParams(headers))

  if (_.isEmpty(req.query)) {
    console.log(chalk.magenta('query: (empty)'))
  } else {
    console.log(chalk.magenta('query:'))
    console.log(renderParams(req.query))
  }

  switch (req.bodyType) {
    case 'empty':
      console.log(chalk.magenta('body: (empty)'))
      break
    case 'raw':
      console.log(chalk.magenta('body: ') + chalk.yellow(`(parsed as raw string by console-log-server since content-type is '${headers['content-type']}'. Forgot to set it correctly?)`))
      console.log(chalk.white(req.body.toString()))
      break
    case 'json':
      console.log(chalk.magenta('body (json): '))
      console.log(chalk.green(neatJSON(req.body, {
        wrap: 40,
        aligned: true,
        afterComma: 1,
        afterColon1: 1,
        afterColonN: 1
      })))
      break
    case 'url':
{'fraction_non_alphanumeric': 0.16383027002496028, 'fraction_numerical': 0.0038574994327206717, 'mean_word_length': 3.158490566037736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '1014909', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1502, 'n_words': 299}