<commit_before>'use strict'

// Native API
const path       = require('path')
const fs         = require('fs')
const { stdout } = require('process')
const events     = require('events')

// Third-party dependencies
const strftime   = require('strftime')
const { indexOf, concat, isObject, has, isString, pull, keys, isUndefined } = require('lodash')


class Log {
  constructor(opts={}) {
    // Internal properties
    this._req_levels = [
        'emergency'
      , 'alert'
      , 'critical'
      , 'error'
      , 'warn'
      , 'notice'
      , 'info'
      , 'debug'
    ]
    this._custom_levels = []
    this._level = this._prefix = ''
    // Create methods for each level
    for (let i in this.getLevels())
      this._addPriorityMethod(this.getLevels()[i])
    // Configured properties
    this.level     = opts.level     || 'notice'
    this.prefix    = opts.prefix    || '%Y-%m-%d %H:%M:%S -'
    this.dir       = opts.dir       || path.join('./', 'logs')
    this.filename  = opts.filename  || `log_${strftime('%Y%m%d_%H%M%S')}.txt`
    this.showLabel = isUndefined(opts.showLabel) ? true : false
    this.stdout    = isUndefined(opts.stdout) ? true : false
    this.file      = isUndefined(opts.file) ? false : true

    events.EventEmitter.call(this)
  }

  _addPriorityMethod(level) {
    this[level] = msg => {
      if (isObject(msg)) {
        if (has(msg, this.level) && this.getLevels().includes(this.level))
          return this[level](msg[this.level])
        else if (has(msg, 'default')) {
          return this[level](msg['default'])
        }
      }
      if (indexOf(this.getLevels(), this.level) >= indexOf(this.getLevels(), level)) {
        console.log(this.showLabel)
        msg = this.showLabel ? `[${level.toUpperCase()}] ${msg}` : msg
        this.write(msg)
        // Emit event with log info
        this.emit('entry', {
           timestamp: new Date()
         , levelIndex: indexOf(this.getLevels(), level)
         , level: level.toUpperCase()
         , prefix: this.prefix
         , msg: msg
        })
        return msg
      }
    }
  }

  get level() { // Current level
    return this._level
  }

  set level(logLevel) { // Set level
    logLevel = logLevel.toLowerCase()
    if (!this.getLevels().includes(logLevel)) {
      throw new Error(`Cannot set log level to '${logLevel}'`)
    } else {
      this._level = logLevel
    }
  }

  get prefix() { // Prepended string to output<commit_msg>Deleted leftover console.log<commit_after>'use strict'

// Native API
const path       = require('path')
const fs         = require('fs')
const { stdout } = require('process')
const events     = require('events')

// Third-party dependencies
const strftime   = require('strftime')
const { indexOf, concat, isObject, has, isString, pull, keys, isUndefined } = require('lodash')


class Log {
  constructor(opts={}) {
    // Internal properties
    this._req_levels = [
        'emergency'
      , 'alert'
      , 'critical'
      , 'error'
      , 'warn'
      , 'notice'
      , 'info'
      , 'debug'
    ]
    this._custom_levels = []
    this._level = this._prefix = ''
    // Create methods for each level
    for (let i in this.getLevels())
      this._addPriorityMethod(this.getLevels()[i])
    // Configured properties
    this.level     = opts.level     || 'notice'
    this.prefix    = opts.prefix    || '%Y-%m-%d %H:%M:%S -'
    this.dir       = opts.dir       || path.join('./', 'logs')
    this.filename  = opts.filename  || `log_${strftime('%Y%m%d_%H%M%S')}.txt`
    this.showLabel = isUndefined(opts.showLabel) ? true : false
    this.stdout    = isUndefined(opts.stdout) ? true : false
    this.file      = isUndefined(opts.file) ? false : true

    events.EventEmitter.call(this)
  }

  _addPriorityMethod(level) {
    this[level] = msg => {
      if (isObject(msg)) {
        if (has(msg, this.level) && this.getLevels().includes(this.level))
          return this[level](msg[this.level])
        else if (has(msg, 'default')) {
          return this[level](msg['default'])
        }
      }
      if (indexOf(this.getLevels(), this.level) >= indexOf(this.getLevels(), level)) {
        msg = this.showLabel ? `[${level.toUpperCase()}] ${msg}` : msg
        this.write(msg)
        // Emit event with log info
        this.emit('entry', {
           timestamp: new Date()
         , levelIndex: indexOf(this.getLevels(), level)
         , level: level.toUpperCase()
         , prefix: this.prefix
         , msg: msg
        })
        return msg
      }
    }
  }

  get level() { // Current level
    return this._level
  }

  set level(logLevel) { // Set level
    logLevel = logLevel.toLowerCase()
    if (!this.getLevels().includes(logLevel)) {
      throw new Error(`Cannot set log level to '${logLevel}'`)
    } else {
      this._level = logLevel
    }
  }

  get prefix() { // Prepended string to output
{'fraction_non_alphanumeric': 0.15392498462799753, 'fraction_numerical': 0.0, 'mean_word_length': 2.3084745762711862, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2379585', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1545, 'n_words': 336}