<commit_before>/**
 * Module dependencies
 */

var cwd = process.cwd()
var env = process.env.NODE_ENV || 'development'
var path = require('path')
var bunyan = require('express-bunyan-logger')
var ensureWritableDirectory = require('../lib/fs-utils').ensureWritableDirectory

/**
 * Export
 */

module.exports = function (options) {
  var logger
  var config = { name: 'request', streams: [] }

  if (!env.match(/test/i)) {
    var logsPath = path.join(cwd, 'logs')
    ensureWritableDirectory(logsPath)

    if (options && options.stdout) { config.streams.push({ stream: process.stdout }) }
    if (options && options.file) { config.streams.push({ path: path.join(logsPath, env + '.log') }) }

    try {
      config = require('../logger')
    } catch (e) {
      if (e.code !== 'MODULE_NOT_FOUND') { throw e }
    }
  }

  logger = bunyan(config)
  module.exports = logger
  return logger
}
<commit_msg>feat(logger): added debug and error logging to oidc, configurable in config file<commit_after>/**
 * Module dependencies
 */

var cwd = process.cwd()
var env = process.env.NODE_ENV || 'development'
var path = require('path')
var bunyan = require('express-bunyan-logger')
var ensureWritableDirectory = require('../lib/fs-utils').ensureWritableDirectory
var oidc = require('../oidc')

var FN_ARGS_SPLIT = /^[^\(]*\(\s*([^\)]*)\)/m

function getSignature (fn) {
  return fn.toString().match(FN_ARGS_SPLIT)[1]
}

function oidcLogging (addLogging) {
  for (var idx in oidc) {
    var fn = oidc[idx]
    if (getSignature(fn) === addLogging.signature) {
      // wrap function with logging
      oidc[fn.name] = addLogging(fn)
    }
  }
}

function debugLogging () {
  var addLogging = function (fn) {
    return function (req, res, next) {
      req.log.debug(fn.name)
      fn(req, res, next)
    }
  }
  addLogging.signature = 'req, res, next'
  oidcLogging(addLogging)
}

function errorLogging () {
  var addLogging = function (fn) {
    return function (err, req, res, next) {
      req.log.error(err, fn.name)
      fn(err, req, res, next)
    }
  }
  addLogging.signature = 'err, req, res, next'
  oidcLogging(addLogging)
}

/**
 * Export
 */

module.exports = function (options) {
  var logger
  var config = { name: 'request', streams: [], level: 'error' }

  if (!env.match(/test/i)) {
    var logsPath = path.join(cwd, 'logs')
    ensureWritableDirectory(logsPath)

    if (options && options.stdout) { config.streams.push({ stream: process.stdout }) }
    if (options && options.file) { config.streams.push({ path: path.join(logsPath, env + '.log') }) }
    if (options && options.level) { config.level = options.level }

    switch (config.level) {
      case 'fatal':
        break
      case 'error':
        errorLogging()
        break
      case 'warn':
        errorLogging()
        break
      case 'info':
        errorLogging()
        break
      case 'debug':
        debugLogging()
        errorLogging()
        break
      case 'trace':
        debugLogging()
        errorLogging()
        break
    }

    try {
      config = require('../logger')
    } catch (e) {
      if (e.code !== 'MODULE_NOT_FOUND') { throw e }
    }
  }

  logger = bunyan(config)

  module.exports = logger
  return logger
}

{'fraction_non_alphanumeric': 0.15370312984195847, 'fraction_numerical': 0.0003098853424233034, 'mean_word_length': 2.824644549763033, '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': '862081', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1068, 'n_words': 254}