Usage

The easiest way to use Blindsight is to import the base package and the DSL:

import com.tersesystems.blindsight._
import com.tersesystems.blindsight.DSL._

To use a Blindsight Logger:

val logger = LoggerFactory.getLogger
logger.info("I am an SLF4J-like logger")

But there’s a lot more, of course.

Examples

statement interpolation:

val dayOfWeek = "Monday"
val temp = 72 
val statement: Statement = st"It is ${dayOfWeek} and the temperature is ${temp} degrees."
logger.info(statement)

Structured logging using an internal DSL:

case class Winner(id: Long, numbers: List[Int])
case class Lotto(id: Long, winningNumbers: List[Int], winners: List[Winner], drawDate: Option[java.util.Date])

val winners = List(Winner(23, List(2, 45, 34, 23, 3, 5)), Winner(54, List(52, 3, 12, 11, 18, 22)))
val lotto = Lotto(5, List(2, 45, 34, 23, 7, 5, 3), winners, None)

import com.tersesystems.blindsight.AST._
import com.tersesystems.blindsight.DSL._
val complexArgument: BObject = "lotto" ->
    ("lotto-id" -> lotto.id) ~
      ("winning-numbers" -> lotto.winningNumbers) ~
      ("draw-date" -> lotto.drawDate.map(_.toString)) ~
      ("winners" -> lotto.winners.map(w => ("winner-id" -> w.id) ~ ("numbers" -> w.numbers)))
logger.info("Logs with an array as marker", complexArgument)

Fluent Logging:

logger.fluent.info.message("I am a fluent logger").log()

Semantic Logging:

logger.semantic[UserEvent].info(userEvent)

Flow Logging:

implicit def flowBehavior[B: ToArgument]: FlowBehavior[B] = new SimpleFlowBehavior 
val resultIsThree: Int = logger.flow.trace(1 + 2)

Conditional Logging:

logger.withCondition(booleanCondition).info("Only logs when condition is true")

logger.info.when(booleanCondition) { info => info("when true") }

Context Logging:

logger.withMarker("userId" -> userId).info("Logging with user id added as a context marker!")

Scripting:

logger.debug.when(location.here) { log =>
  log("script allows selective logging by method or by line")
}

Inspections:

import com.tersesystems.blindsight.inspection.InspectionMacros._

decorateVals(dval => logger.debug(s"${dval.name} = ${dval.value}")) {
  val a = 5
  val b = 15
  a + b
}
The source code for this page can be found here.