<commit_before>'use strict'

var pino = require('pino')
var maxInt = 2147483647

function pinoLogger (opts, stream) {
  if (opts && opts._writableState) {
    stream = opts
    opts = null
  }

  opts = opts || {}
  opts.serializers = opts.serializers || {}
  opts.serializers.req = opts.serializers.req || asReqValue
  opts.serializers.res = opts.serializers.res || pino.stdSerializers.res

  var logger = null

  if (opts.logger) {
    logger = opts.logger
    opts.logger = undefined
    logger = logger.child(opts)
  } else {
    logger = pino(opts, stream)
  }

  loggingMiddleware.logger = logger

  var nextReqId = 0

  stream = logger.stream

  return loggingMiddleware

  function onResFinished (err) {
    this.removeListener('finish', onResFinished)
    this.removeListener('error', onResFinished)

    var log = this.log
    var responseTime = Date.now() - this.startTime

    if (err) {
      log.error({
        res: this,
        err: err,
        responseTime: responseTime
      }, 'request errored')
      return
    }

    log.info({
      res: this,
      responseTime: responseTime
    }, 'request completed')
  }

  function loggingMiddleware (req, res, next) {
    var startTime = Date.now()
    if (req.id === undefined) {
      req.id = ++nextReqId
      nextReqId = nextReqId % maxInt
    }

    var child = logger.child({req: req})

    req.log = child
    res.log = child
    res.startTime = startTime

    res.on('finish', onResFinished)
    res.on('error', onResFinished)

    if (next) {
      next()
    }
  }
}

function asReqValue (req) {
  return {
    id: req.id,
    method: req.method,
    url: req.url,
    headers: req.headers,
    remoteAddress: req.connection.remoteAddress,
    remotePort: req.connection.remotePort
  }
}

module.exports = pinoLogger
module.exports.stdSerializers = {
  req: asReqValue,
  res: pino.stdSerializers.res
}<commit_msg>Restore the logger property into the options after instantation.<commit_after>'use strict'

var pino = require('pino')
var maxInt = 2147483647

function pinoLogger (opts, stream) {
  if (opts && opts._writableState) {
    stream = opts
    opts = null
  }

  opts = opts || {}
  opts.serializers = opts.serializers || {}
  opts.serializers.req = opts.serializers.req || asReqValue
  opts.serializers.res = opts.serializers.res || pino.stdSerializers.res

  var logger = wrapChild(opts, stream)

  loggingMiddleware.logger = logger

  var nextReqId = 0

  stream = logger.stream

  return loggingMiddleware

  function onResFinished (err) {
    this.removeListener('finish', onResFinished)
    this.removeListener('error', onResFinished)

    var log = this.log
    var responseTime = Date.now() - this.startTime

    if (err) {
      log.error({
        res: this,
        err: err,
        responseTime: responseTime
      }, 'request errored')
      return
    }

    log.info({
      res: this,
      responseTime: responseTime
    }, 'request completed')
  }

  function loggingMiddleware (req, res, next) {
    var startTime = Date.now()
    if (req.id === undefined) {
      req.id = ++nextReqId
      nextReqId = nextReqId % maxInt
    }

    var child = logger.child({req: req})

    req.log = child
    res.log = child
    res.startTime = startTime

    res.on('finish', onResFinished)
    res.on('error', onResFinished)

    if (next) {
      next()
    }
  }
}

function asReqValue (req) {
  return {
    id: req.id,
    method: req.method,
    url: req.url,
    headers: req.headers,
    remoteAddress: req.connection.remoteAddress,
    remotePort: req.connection.remotePort
  }
}

function wrapChild (opts, stream) {
  var prevLogger = opts.logger
  var logger = null

  if (prevLogger) {
    opts.logger = undefined
    logger = prevLogger.child(opts)
    opts.logger = prevLogger
  } else {
    logger = pino(opts, stream)
  }

  return logger
}

module.exports = pinoLogger
module.exports.stdSerializers = {
  req: asReqValue,
  res: pino.stdSerializers.res
}
{'fraction_non_alphanumeric': 0.11472516389309127, 'fraction_numerical': 0.005547150781643974, 'mean_word_length': 2.7389255419415646, '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': '7601784', 'n_tokens_mistral': 1321, 'n_tokens_neox': 1276, 'n_words': 305}