package io.getquill.util

import com.typesafe.scalalogging.Logger
import org.slf4j.LoggerFactory

class ContextLogger(name: String) {
  val underlying = Logger(LoggerFactory.getLogger(name))

  private val bindsDisabled = sys.props.get("quill.binds.log").contains("false")
  private val nullToken = "null"

  def logQuery(query: String, params: Seq[Any]): Unit = {
    if (bindsDisabled || params.isEmpty) underlying.debug(query)
    else {
      underlying.debug("{} - binds: {}", query, prepareParams(params))
    }
  }

  def logBatchItem(query: String, params: Seq[Any]): Unit = {
    if (!bindsDisabled) {
      underlying.debug("{} - batch item: {}", query, prepareParams(params))
    }
  }

  private def prepareParams(params: Seq[Any]): String = params.map(prepareParam).mkString("[", ", ", "]")
  private def prepareParam(param: Any): String = param match {
    case None | null => nullToken
    case Some(x)     => prepareParam(x)
    case str: String => s"'$str'"
    case _           => param.toString
  }
}

object ContextLogger {
  def apply(ctxClass: Class[_]): ContextLogger = new ContextLogger(ctxClass.getName)
}


package io.getquill.util

import com.typesafe.scalalogging.Logger
import org.slf4j.LoggerFactory

class ContextLogger(name: String) {
  val underlying = Logger(LoggerFactory.getLogger(name))

  private val bindsDisabled = sys.props.get("quill.binds.log").contains("false")
  private val nullToken = "null"

  def logQuery(query: String, params: Seq[Any]): Unit = {
    if (bindsDisabled || params.isEmpty) underlying.debug(query)
    else {
      underlying.debug("{} - binds: {}", query, prepareParams(params))
    }
  }

  def logBatchItem(query: String, params: Seq[Any]): Unit = {
    if (!bindsDisabled) {
      underlying.debug("{} - batch item: {}", query, prepareParams(params))
    }
  }

  private def prepareParams(params: Seq[Any]): String = params
    .reverse
    .map(prepareParam)
    .mkString("[", ", ", "]")

  private def prepareParam(param: Any): String = param match {
    case None | null => nullToken
    case Some(x)     => prepareParam(x)
    case str: String => s"'$str'"
    case _           => param.toString
  }
}

object ContextLogger {
  def apply(ctxClass: Class[_]): ContextLogger = new ContextLogger(ctxClass.getName)
}


Fix reversed log parameter binds
Fix reversed log parameter binds
