<commit_before>'use strict';

/**
 * Rhizome - The API that feeds grassroots movements
 *
 * @file logging.js
 * @description Logging helpers
 * @module System
 * @author <NAME>
 *
 * @todo Centralise this logging across the app
 * @todo Add other log targets ie to datastore
 *
 */

const logFormat = Date.ISO8601_DATETIME;
var logPrefix = module.exports.logPrefix = () => {
  Date.create().format(logFormat);
};

/**
 *
 * @param {string} string - log entry
 * @private
 */
function _log(string) {
  console.log(string);
}

/**
 * STANDARD LOGGING
 */

/**
 * @param {string} log - Text to log
 * @return {function(*)} - returns a function for chaining into a promise
 */
module.exports.log = log => {
  return res => {
    _log(`${logPrefix()} - ${log}: ${res}`);
    return res;
  };
};

/**
 * @param {string} log - Text to log
 * @param {*} val - value to test `res` against
 * @return {function(*)} - returns a function for chaining into a promise
 */
module.exports.logIf = (log, val) => {
  return res => {
    if (val === res) {
      _log(`${logPrefix()} - ${log}: ${res}`);
    }
    return res;
  };
};

/**
 * @param {string} log - Text to log
 * @param {*} val - value to test `res` against
 * @return {function(*)} - returns a function for chaining into a promise
 */
module.exports.logIfNot = (log, val) => {
  return res => {
    if (val !== res) {
      _log(`${logPrefix()} - ${log}: ${res}`);
    }
    return res;
  };
};

/**
 * PROPERTY LOGGING
 */

/**
 * @param {string} log - Text to log
 * @param {string} prop - Name of the `res` property to log
 * @return {function(*)} - returns a function for chaining into a promise
 */
module.exports.logProp = (log, prop) => {
  return res => {
    _log(`${logPrefix()} - ${log}: ${res[prop]}`);
    return res;
  };
};

/**
 * @param {string} log - Text to log
 * @param {string} prop - Name of the `res` property to log
 * @param {*} val - value to test `res` against
 * @return {function(*)} - returns a function for chaining into a promise
 */
module.exports.logPropIf = (log, prop, val) => {
  return res => {
    if (val === res[prop]) {
      _log(`${logPrefix()} - ${log}: ${res[prop]}`);
    }
    return res;
  };
};

/**
 * @param {string} log - Text to log
 * @param {string} prop - Name of the `res` property to log
 * @param {*} val - value to test `res` against
 * @return {function(*)} - returns a function for chaining into a promise
 */
module.exports.logPropIfNot = (log, prop, val) => {
  return res => {
    if (val !== res[prop]) {
      _log(`${logPrefix()} - ${log}: ${res[prop]}`);
    }
    return res;
  };
};

/**
 * ARRAY LOGGING
 */

/**
 * @param {string} log - Text to log
 * @return {function(*)} - returns a function for chaining into a promise
 */
module.exports.logArray = log => {
  return res => {
    console.log(`${logPrefix()} - ${log}: ${res.length}`);
    res.forEach(r => {
      _log(r);
    });
    return res;
  };
};

/**
 * @param {string} log - Text to log
 * @param {string} prop - Name of the `res[]` property to log
 * @return {function(*)} - returns a function for chaining into a promise
 */
module.exports.logArrayProp = (log, prop) => {
  return res => {
    console.log(`${logPrefix()} - ${log}: ${res.length}`);
    res.forEach(r => {
      _log(r[prop]);
    });<commit_msg>- UPDATED: Added log level framework.<commit_after>'use strict';

/**
 * Rhizome - The API that feeds grassroots movements
 *
 * @file logging.js
 * @description Logging helpers
 * @module System
 * @author <NAME>
 *
 * @todo Centralise this logging across the app
 * @todo Add other log targets ie to datastore
 *
 */

const logFormat = Date.ISO8601_DATETIME;
var logPrefix = module.exports.logPrefix = () => {
  return Date.create().format(logFormat);
};

var LogLevel = {
  NONE: 0,
  ERR: 1,
  WARN: 2,
  INFO: 3,
  VERBOSE: 4,
  DEFAULT: 3
};

module.exports.Constants = {
  LogLevel: LogLevel
};
var _logLevel = LogLevel.INFO;

/**
 *
 * @param {string} log - log entry
 * @param {integer} level - level to log at
 * @private
 */
function _log(log, level) {
  if (_logLevel >= level) {
    console.log(log);
  }
}

/**
 * STANDARD LOGGING
 */

module.exports.setLogLevel = level => {
  _logLevel = level;
};

/**
 * @param {string} log - Text to log
 * @param {integer} level - level to log at
 */
module.exports.stdLog = (log, level) => {
  level = level || LogLevel.DEFAULT;
  _log(`${logPrefix()} - ${log}`, level);
};

/**
 * PROMISE LOGGING
 */

/**
 * @param {string} log - Text to log
 * @param {integer} level - level to log at
 * @return {function(*)} - returns a function for chaining into a promise
 */
module.exports.log = (log, level) => {
  level = level || LogLevel.DEFAULT;
  return res => {
    _log(`${logPrefix()} - ${log}: ${res}`, level);
    return res;
  };
};

/**
 * @param {string} log - Text to log
 * @param {*} val - value to test `res` against
 * @param {integer} level - level to log at
 * @return {function(*)} - returns a function for chaining into a promise
 */
module.exports.logIf = (log, val, level) => {
  level = level || LogLevel.DEFAULT;
  return res => {
    if (val === res) {
      _log(`${logPrefix()} - ${log}: ${res}`, level);
    }
    return res;
  };
};

/**
 * @param {string} log - Text to log
 * @param {*} val - value to test `res` against
 * @param {integer} level - level to log at
 * @return {function(*)} - returns a function for chaining into a promise
 */
module.exports.logIfNot = (log, val, level) => {
  level = level || LogLevel.DEFAULT;
  return res => {
    if (val !== res) {
      _log(`${logPrefix()} - ${log}: ${res}`, level);
    }
    return res;
  };
};

/**
 * PROPERTY LOGGING
 */

/**
 * @param {string} log - Text to log
 * @param {string} prop - Name of the `res` property to log
 * @param {integer} level - level to log at
 * @return {function(*)} - returns a function for chaining into a promise
 */
module.exports.logProp = (log, prop, level) => {
  level = level || LogLevel.DEFAULT;
  return res => {
    _log(`${logPrefix()} - ${log}: ${res[prop]}`, level);
    return res;
  };
};

/**
 * @param {string} log - Text to log
 * @param {string} prop - Name of the `res` property to log
 * @param {*} val - value to test `res` against
 * @param {integer} level - level to log at
 * @return {function(*)} - returns a function for chaining into a promise
 */
module.exports.logPropIf = (log, prop, val, level) => {
  level = level || LogLevel.DEFAULT;
  return res => {
    if (val === res[prop]) {
      _log(`${logPrefix()} - ${log}: ${res[prop]}`, level);
    }
    return res;
  };
};

/**
 * @param {string} log - Text to log
 * @param {string} prop - Name of the `res` property to log
 * @param {*} val - value to test `res` against
 * @param {integer} level - level to log at
 * @return {function(*)} - returns a function for chaining into a promise
 */
module.exports.logPropIfNot = (log, prop, val, level) => {
  level = level || LogLevel.DEFAULT;
  return res => {
    if (val !== res[prop]) {
      _log(`${logPrefix()} - ${log}: ${res[prop]}`, level);
    }
    return res;
  };
};

/**
 * ARRAY LOGGING
 */

/**
 * @param {string} log - Text to log
 * @param {integer} level - level to log at
 * @return {function(*)} - returns a function for chaining into a promise
 */
module.exports.logArray = (log, level) => {
  level = level || LogLevel.DEFAULT;
  return res => {
    console.log(`${logPrefix()} - ${log}: ${res.length}`, level);
    res.forEach(r => {
      _log(r);
    });
    return res;
  };
};

/**
 * @param {string} log - Text to log
 * @param {string} prop - Name of the `res[]` property to log
 * @param {integer} level - level to log at
 * @return {function(*)} - returns a function for chaining into a promise
 */
module.exports.logArrayProp = (log, prop, level) => {
  level = level || LogLevel.DEFAULT;
  return res => {
    console.log(`${logPrefix()} - ${log}: ${res.length}`, level);
    res.forEach(r => {
      _log(r[prop]);
    });
{'fraction_non_alphanumeric': 0.18471977883890425, 'fraction_numerical': 0.0017592359889419453, 'mean_word_length': 2.9479166666666665, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 46, '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': '5681587', 'n_tokens_mistral': 2684, 'n_tokens_neox': 2672, 'n_words': 851}