Skip to content

Commit

Permalink
Added support for scribe-rapid
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Dec 14, 2024
1 parent 2d79738 commit c67621d
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 4 deletions.
22 changes: 18 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// Scala versions
val scala213 = "2.13.15"

val scala212 = "2.12.20"

val scala3 = "3.3.4"

val allScalaVersions = List(scala213, scala212, scala3)
val allScalaVersions = List(scala213, scala3)

name := "scribe"
ThisBuild / organization := "com.outr"
ThisBuild / version := "3.15.2"
ThisBuild / version := "3.15.3-SNAPSHOT"
ThisBuild / scalaVersion := scala213
ThisBuild / scalacOptions ++= Seq("-unchecked", "-deprecation")
ThisBuild / javacOptions ++= Seq("-source", "1.8", "-target", "1.8")
Expand Down Expand Up @@ -46,6 +44,8 @@ val collectionCompatVersion: String = "2.12.0"

val moduloadVersion: String = "1.1.7"

val rapidVersion: String = "0.2.1"

val catsEffectVersion: String = "3.5.7"

val catsEffectTestingVersion: String = "1.6.0"
Expand Down Expand Up @@ -101,6 +101,7 @@ val sourceMapSettings = List(
lazy val root = project.in(file("."))
.aggregate(
core.js, core.jvm, core.native,
rapid.js, rapid.jvm, rapid.native,
// TODO: Re-enable cats.native when cats-effect supports ScalaNative 0.5
cats.js, cats.jvm, //cats.native,
fileModule.jvm, fileModule.native,
Expand Down Expand Up @@ -147,6 +148,19 @@ lazy val core = crossProject(JVMPlatform, JSPlatform, NativePlatform)
coverageEnabled := false
)

lazy val rapid = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Full)
.settings(
name := "scribe-rapid",
crossScalaVersions := allScalaVersions,
libraryDependencies ++= Seq(
"com.outr" %%% "rapid-core" % rapidVersion,
"org.scalatest" %%% "scalatest" % scalaTestVersion % Test,
),
Test / publishArtifact := false
)
.dependsOn(core)

lazy val cats = crossProject(JVMPlatform, JSPlatform) //, NativePlatform)
.crossType(CrossType.Full)
.settings(
Expand Down
14 changes: 14 additions & 0 deletions rapid/shared/src/main/scala/scribe/rapid/RapidLoggerSupport.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package scribe.rapid

import _root_.rapid.Task
import scribe.mdc.MDC
import scribe.{Level, LogFeature, LogRecord, Logger, LoggerSupport}
import sourcecode.{FileName, Line, Name, Pkg}

trait RapidLoggerSupport extends Any with LoggerSupport[Task[Unit]] {
override def log(record: LogRecord): Task[Unit] = Task(Logger(record.className).log(record))

override def log(level: Level, mdc: MDC, features: LogFeature*)
(implicit pkg: Pkg, fileName: FileName, name: Name, line: Line): Task[Unit] =
log(LoggerSupport(level, Nil, pkg, fileName, name, line, mdc).withFeatures(features: _*))
}
14 changes: 14 additions & 0 deletions rapid/shared/src/main/scala/scribe/rapid/RapidLoggerWrapper.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package scribe.rapid

import _root_.rapid.Task
import scribe.{Level, LogFeature, LogRecord, Logger}
import scribe.mdc.MDC
import sourcecode.{FileName, Line, Name, Pkg}

class RapidLoggerWrapper(val wrapped: Logger) extends AnyVal with RapidLoggerSupport {
override def log(record: LogRecord): Task[Unit] = Task(wrapped.log(record))

override def log(level: Level, mdc: MDC, features: LogFeature*)
(implicit pkg: Pkg, fileName: FileName, name: Name, line: Line): Task[Unit] =
Task(wrapped.log(level, mdc, features: _*)(pkg, fileName, name, line))
}
7 changes: 7 additions & 0 deletions rapid/shared/src/main/scala/scribe/rapid/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package scribe

package object rapid extends RapidLoggerSupport {
implicit class LoggerExtras(val logger: Logger) extends AnyVal {
def rapid: RapidLoggerSupport = new RapidLoggerWrapper(logger)
}
}
59 changes: 59 additions & 0 deletions rapid/shared/src/test/scala/spec/ScribeRapidSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package spec

import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import rapid.Task
import scribe.handler.LogHandler
import scribe.rapid._
import scribe.{LogRecord, Logger}
import scribe.{rapid => logger}

class ScribeRapidSpec extends AnyWordSpec with Matchers {
"ScribeEffect" should {
var messages: List[String] = Nil
Logger.root
.clearHandlers()
.withHandler(new LogHandler {
override def log(record: LogRecord): Unit = synchronized {
messages = record.messages.map(_.logOutput.plainText) ::: messages
}
})
.replace()

"do rapid logging" in {
logger.info("1").map { _ =>
messages should be(List("1"))
}.sync()
}
"do instantiation logging" in {
messages = Nil

val biz = new Biz
biz.doStuff().map { s =>
messages should be(List("3"))
s should be("done")
}.sync()
}
"do reference logging" in {
messages = Nil

logger.info("4").map { _ =>
messages should be(List("4"))
}.sync()
}
"do existing logger logging" in {
messages = Nil
Logger.root.rapid.info("5").map { _ =>
messages should be(List("5"))
}.sync()
}
}

class Biz extends RapidLoggerSupport {
def doStuff(): Task[String] = for {
_ <- info("3")
} yield {
"done"
}
}
}

0 comments on commit c67621d

Please sign in to comment.