<commit_before>fs = require 'fs'

class Logger


  @_logLevel = 'INFO'
  @_logPath = './logs/'   # set up null to prevent log to text file
  @_logFile = ''          # calculated in _getTimestamp()


  @_pad: (number, zeroes) =>
    return ('000000' + number).slice(-zeroes)


  @_getTimestamp: =>
    now = new Date()

    day = now.getDate()
    month = now.getMonth() + 1
    date = now.getFullYear() + '-' + @_pad(month, 2) + '-' + @_pad(day, 2) 

    if @_logPath isnt null
      # Make sure the log file matches the current date
      fileName = now.getFullYear() + '_' + @_pad(month, 2) + '_' + @_pad(day, 2) + '.txt'
      if @_logFile isnt fileName
        @_logFile = fileName
    
    hours = now.getHours()
    minutes = now.getMinutes()
    seconds = now.getMinutes()
    milliseconds = now.getMilliseconds()
    time = @_pad(hours, 2) + ':' + @_pad(minutes, 2) + ':' + @_pad(seconds, 2) + '.' + @_pad(milliseconds, 3)

    timestamp = date + ' ' + time
    return timestamp


  @_doLog: (level, text) =>

    textToLog = "#{@_getTimestamp()} #{level}: #{text}"

    # Log to console
    console.log textToLog
    
    if @_logPath isnt null
      # Log to disk
      fullLogFileName = @_logPath + @_logFile
      fs.appendFile fullLogFileName, textToLog + '\r\n', (err) ->
        if err
          console.log "ERROR writting to log file #{fullLogFileName}:\r\n\t#{err}"


  @setLevel: (level) =>
    if level.toUpperCase() in ['INFO', 'WARN', 'ERROR', 'NONE']
      @_logLevel = level.toUpperCase()


  @info: (text) =>
    if @_logLevel is 'INFO'
      @_doLog 'INFO', text


  @warn: (text) =>
    if @_logLevel in ['INFO', 'WARN']
      @_doLog 'WARN', text


  @error: (text) =>
    if @_logLevel in ['INFO', 'WARN', 'ERROR']
      @_doLog 'ERROR', text


  @error: (text, exception = null) =>
    if @_logLevel in ['INFO', 'WARN', 'ERROR']
      text = text + "\r\n#{exception}" if exception?
      @_doLog 'ERROR', "#{text}"

<commit_msg>better logging<commit_after>fs = require 'fs'

class Logger


  @_logLevel = 'INFO'
  @_logPath = './logs/'   # set up null to prevent log to text file
  @_logFile = ''          # calculated in _getTimestamp()


  @_pad: (number, zeroes) =>
    return ('000000' + number).slice(-zeroes)


  @_getTimestamp: =>
    now = new Date()

    day = now.getDate()
    month = now.getMonth() + 1
    date = now.getFullYear() + '-' + @_pad(month, 2) + '-' + @_pad(day, 2) 

    if @_logPath isnt null
      # Make sure the log file matches the current date
      fileName = now.getFullYear() + '_' + @_pad(month, 2) + '_' + @_pad(day, 2) + '.txt'
      if @_logFile isnt fileName
        @_logFile = fileName
    
    hours = now.getHours()
    minutes = now.getMinutes()
    seconds = now.getMinutes()
    milliseconds = now.getMilliseconds()
    time = @_pad(hours, 2) + ':' + @_pad(minutes, 2) + ':' + @_pad(seconds, 2) + '.' + @_pad(milliseconds, 3)

    timestamp = date + ' ' + time
    return timestamp


  @_doLog: (level, text) =>

    textToLog = "#{@_getTimestamp()} #{level}: #{text}"

    # Log to console
    console.log textToLog
    
    if @_logPath isnt null
      # Log to disk
      fullLogFileName = @_logPath + @_logFile
      fs.appendFile fullLogFileName, textToLog + '\r\n', (err) ->
        if err
          console.log "ERROR writting to log file #{fullLogFileName}. Error: [#{err}]"


  @setLevel: (level) =>
    if level.toUpperCase() in ['INFO', 'WARN', 'ERROR', 'NONE']
      @_logLevel = level.toUpperCase()


  @info: (text) =>
    if @_logLevel is 'INFO'
      @_doLog 'INFO', text


  @warn: (text) =>
    if @_logLevel in ['INFO', 'WARN']
      @_doLog 'WARN', text


  @error: (text) =>
    if @_logLevel in ['INFO', 'WARN', 'ERROR']
      @_doLog 'ERROR', text


  @error: (text, exception = null) =>
    if @_logLevel in ['INFO', 'WARN', 'ERROR']
      text = text + "\r\n#{exception}" if exception?
      @_doLog 'ERROR', "#{text}"


{'fraction_non_alphanumeric': 0.17036470288191788, 'fraction_numerical': 0.0076511094108645756, 'mean_word_length': 2.6382189239332097, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '68356', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1373, 'n_words': 375}