Sha256: 71d3cdf02414b7012568b97b74544422edd101860706719d78a949e18a3e4bf3

Contents?: true

Size: 1.95 KB

Versions: 6

Compression:

Stored size: 1.95 KB

Contents

package s3.website

class Logger(val verboseOutput: Boolean, logMessage: (String) => Unit = println) {
  def debug(msg: String) = if (verboseOutput) log(Debug, msg)
  def info(msg: String) = log(Info, msg)
  def fail(msg: String) = log(Failure, msg)

  def info(report: SuccessReport) = log(Success, report.reportMessage)
  def info(report: FailureReport) = fail(report.reportMessage)

  def pending(msg: String) = log(Wait, msg)

  private def log(logType: LogType, msgRaw: String) {
    val msg = msgRaw.replaceAll("\\n", "\n       ") // Indent new lines, so that they arrange nicely with other log lines
    logMessage(s"[$logType] $msg")
  }

  sealed trait LogType {
    val prefix: String
    override def toString = prefix
  }
  case object Debug extends LogType {
    val prefix = "debg".cyan
  }
  case object Info extends LogType {
    val prefix = "info".blue
  }
  case object Success extends LogType {
    val prefix = "succ".green
  }
  case object Failure extends LogType {
    val prefix = "fail".red
  }
  case object Wait extends LogType {
    val prefix = "wait".yellow
  }

  /**
   * Idea copied from https://github.com/ktoso/scala-rainbow.
   */
  implicit class RainbowString(val s: String) {
    import Console._

    /** Colorize the given string foreground to ANSI black */
    def black = BLACK + s + RESET
    /** Colorize the given string foreground to ANSI red */
    def red = RED + s + RESET
    /** Colorize the given string foreground to ANSI red */
    def green = GREEN + s + RESET
    /** Colorize the given string foreground to ANSI red */
    def yellow = YELLOW + s + RESET
    /** Colorize the given string foreground to ANSI red */
    def blue = BLUE + s + RESET
    /** Colorize the given string foreground to ANSI red */
    def magenta = MAGENTA + s + RESET
    /** Colorize the given string foreground to ANSI red */
    def cyan = CYAN + s + RESET
    /** Colorize the given string foreground to ANSI red */
    def white = WHITE + s + RESET
  }
}

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
s3_website_monadic-0.0.37 src/main/scala/s3/website/Logger.scala
s3_website_monadic-0.0.36 src/main/scala/s3/website/Logger.scala
s3_website_monadic-0.0.35 src/main/scala/s3/website/Logger.scala
s3_website_monadic-0.0.34 src/main/scala/s3/website/Logger.scala
s3_website_monadic-0.0.33 src/main/scala/s3/website/Logger.scala
s3_website_monadic-0.0.32 src/main/scala/s3/website/Logger.scala