<commit_before>'use strict'

const 
  fs     =  require('fs'),
  stream =  require('stream'),
  path   =  require('path')
,
  timezoneOffset = (new Date()).getTimezoneOffset() * 60000
;

let logl, root

function toWritable (handle, rotate) {
  if (handle instanceof stream.Writable) {
    return handle
  } else {
    return fs.createWriteStream(path.resolve(root, handle), { flags: rotate ? 'w' : 'a' }) 
      .on('error', err => {
        throw err
      })
  }
}

class Log extends console.Console {
  constructor (out, err, { rotate = false } = {}) {
    // handle optional second parameter (separate file for stderr)
    super(toWritable(out, rotate), err ? toWritable(err, rotate) : undefined)
  }
  common (level, prefix, method, ...args) {
    // log-write-instant-specific options
    // date is prepended to every log write by default
    let options = { date: true }
    if (typeof args[args.length-1] === 'object') {
      Object.assign(options, args.pop())
    }
    process.env.DEBUG && console.log(...args)
    if (logl < level) return
    args.unshift(prefix, options.date ? new Date(Date.now() - timezoneOffset).toISOString() : '')
    super[method](...args)
  }
  log (...args) {
    this.common(1, 'L', 'log', ...args)
  }
  info (...args) {
    this.common(2, 'I', 'info', ...args)
  }<commit_msg>added support for log rotation<commit_after>'use strict'

const 
  fs     =  require('fs'),
  stream =  require('stream'),
  path   =  require('path')
,
  timezoneOffset = (new Date()).getTimezoneOffset() * 60000
;

let logl, root

function toWritable (handle, { rotate = false }) {
  if (handle instanceof stream.Writable) {
    return handle
  } else {
    return fs.createWriteStream(path.resolve(root, handle), { flags: rotate ? 'w' : 'a' }) 
      .on('error', err => {
        throw err
      })
  }
}

class Log extends console.Console {
  constructor (out, err, options = {}) {
    // log instant-specific options
    if (typeof err !== 'string' && !( err instanceof stream.Writable )) {
      options = err 
      err = undefined
    }
    // handle optional second parameter (separate file for stderr)
    super(toWritable(out, options), err ? toWritable(err, options) : undefined)
  }
  common (level, prefix, method, ...args) {
    // log-write instant-specific options
    // date is prepended to every log write by default
    let options = { date: true }
    if (typeof args[args.length-1] === 'object') {
      Object.assign(options, args.pop())
    }
    process.env.DEBUG && console.log(...args)
    if (logl < level) return
    args.unshift(prefix, options.date ? new Date(Date.now() - timezoneOffset).toISOString() : '')
    super[method](...args)
  }
  log (...args) {
    this.common(1, 'L', 'log', ...args)
  }
  info (...args) {
    this.common(2, 'I', 'info', ...args)
  }
{'fraction_non_alphanumeric': 0.16376554174067495, 'fraction_numerical': 0.005683836589698046, 'mean_word_length': 2.8002699055330633, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1099122', 'n_tokens_mistral': 927, 'n_tokens_neox': 905, 'n_words': 241}