<commit_before>package org.ciroque.ccr.logging

import org.ciroque.ccr.core.CommonJsonFormatters._
import org.joda.time.{DateTime, DateTimeZone}
import org.slf4j.Logger
import spray.json.{DefaultJsonProtocol, _}

object ImplicitLogging {

  private[logging] val activeLoggers = new ThreadLocal[LogEntryBuilder]

  def withImplicitLogging[R](name: String)(fx: => R)(implicit logger: Logger) = {
    activeLoggers.set(new LogEntryBuilder)
    val started: DateTime = DateTime.now(DateTimeZone.UTC)

    try {
      fx
    } finally {
      val ended = DateTime.now(DateTimeZone.UTC)
      val logEntry = getCurrentImplicitLogger.buildAsJson(name, started, ended)
      logger.info(logEntry.toString())
    }
  }

  def setResultException(cause: Throwable) = {
    getCurrentImplicitLogger.result = Exception(cause.getMessage, cause)
  }

  def setResultError(msg: String) = {
    getCurrentImplicitLogger.result = Error(msg)
  }

  def getCurrentImplicitLogger = activeLoggers.get

  def recordValue(name: String, value: String) = {
    getCurrentImplicitLogger.addValue(name, value)
  }

  trait Result

  class LogEntryBuilder() {
    var values: List[Values] = List()
    var result: Result = Success()

    def addValue(name: String, value: String): List[Values] = {
      values = values :+ Values(name, value)
      values
    }

    def buildAsJson(name: String, started: DateTime, ended: DateTime): JsValue = {
      import org.ciroque.ccr.logging.ImplicitLogging.LogEntryProtocol._
      build(name, started, ended).toJson
    }

    def build(name: String, started: DateTime, ended: DateTime): LogEntry = {
      LogEntry(name, Timing(started, ended, ended.getMillis - started.getMillis), values, result)
    }
  }

  case class LogEntry(name: String, timing: Timing, values: List[Values], result: Result = Success())<commit_msg>Switching to Scala's DynamicVariable instead oj Java's ThreadLocal<commit_after>package org.ciroque.ccr.logging

import org.ciroque.ccr.core.CommonJsonFormatters._
import org.joda.time.{DateTime, DateTimeZone}
import org.slf4j.Logger
import spray.json.{DefaultJsonProtocol, _}

import scala.util.DynamicVariable

object ImplicitLogging {

  private[logging] val activeLoggers = new DynamicVariable[LogEntryBuilder](new LogEntryBuilder)

  def withImplicitLogging[R](name: String)(fx: => R)(implicit logger: Logger) = {
//    activeLoggers.set(new LogEntryBuilder)
    val started: DateTime = DateTime.now(DateTimeZone.UTC)

    try {
      fx
    } finally {
      val ended = DateTime.now(DateTimeZone.UTC)
      val logEntry = getCurrentImplicitLogger.buildAsJson(name, started, ended)
      logger.info(logEntry.toString())
    }
  }

  def setResultException(cause: Throwable) = {
    getCurrentImplicitLogger.result = Exception(cause.getMessage, cause)
  }

  def setResultError(msg: String) = {
    getCurrentImplicitLogger.result = Error(msg)
  }

  def getCurrentImplicitLogger = activeLoggers.value

  def recordValue(name: String, value: String) = {
    getCurrentImplicitLogger.addValue(name, value)
  }

  trait Result

  class LogEntryBuilder() {
    var values: List[Values] = List()
    var result: Result = Success()

    def addValue(name: String, value: String): List[Values] = {
      values = values :+ Values(name, value)
      values
    }

    def buildAsJson(name: String, started: DateTime, ended: DateTime): JsValue = {
      import org.ciroque.ccr.logging.ImplicitLogging.LogEntryProtocol._
      build(name, started, ended).toJson
    }

    def build(name: String, started: DateTime, ended: DateTime): LogEntry = {
      LogEntry(name, Timing(started, ended, ended.getMillis - started.getMillis), values, result)
    }
  }

  case class LogEntry(name: String, timing: Timing, values: List[Values], result: Result = Success())
{'fraction_non_alphanumeric': 0.10867844895805856, 'fraction_numerical': 0.0005275652862041678, 'mean_word_length': 4.06951871657754, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7148960', 'n_tokens_mistral': 1181, 'n_tokens_neox': 1148, 'n_words': 259}