-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from llfrometa89/ref-logger-test
Update docs | Add LazyLogging | Add basic example with LazyLogging
- Loading branch information
Showing
10 changed files
with
251 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
core/shared/src/main/scala/org/pure4s/logger4s/GLogger.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.pure4s.logger4s | ||
|
||
import cats.Show | ||
|
||
trait GLogger[F[_]] { | ||
|
||
def error[A: Show](msg: A): F[Unit] | ||
def error[A: Show](msg: A, err: Throwable): F[Unit] | ||
def warn[A: Show](msg: A): F[Unit] | ||
def info[A: Show](msg: A): F[Unit] | ||
def debug[A: Show](msg: A): F[Unit] | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
core/shared/src/main/scala/org/pure4s/logger4s/LazyLogging.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.pure4s.logger4s | ||
|
||
import org.slf4j | ||
import org.slf4j.LoggerFactory | ||
|
||
trait LazyLogging { | ||
implicit lazy val logger: slf4j.Logger = LoggerFactory.getLogger(getClass.getName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
core/shared/src/main/scala/org/pure4s/logger4s/SLogger.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.pure4s.logger4s | ||
|
||
trait SLogger[F[_]] { | ||
|
||
def error(msg: String): F[Unit] | ||
def error(msg: String, err: Throwable): F[Unit] | ||
def warn(msg: String): F[Unit] | ||
def info(msg: String): F[Unit] | ||
def debug(msg: String): F[Unit] | ||
|
||
} |
84 changes: 78 additions & 6 deletions
84
core/shared/src/test/scala/org/pure4s/logger4s/LoggerSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,87 @@ | ||
package org.pure4s.logger4s | ||
import org.pure4s.logger4s.Logger.showString | ||
import cats.effect.IO | ||
import org.scalatest._ | ||
|
||
class LoggerSpec extends FunSpec with Matchers { | ||
import cats.Show | ||
import cats.implicits._ | ||
import cats.effect.IO | ||
import org.mockito.Mockito._ | ||
import org.scalatest.{Matchers, _} | ||
import org.scalatest.mockito.MockitoSugar | ||
import org.slf4j.{Logger => Underlying} | ||
|
||
implicit val instance: Logger[IO] = Logger.instance[IO](classOf[LoggerSpec]) | ||
class LoggerSpec extends FunSpec with Matchers with MockitoSugar { | ||
|
||
describe("Logger[F[_]].info") { | ||
val f = fixture(_.isInfoEnabled, isEnabled = true) | ||
import f._ | ||
it(s"Success expected info message") { | ||
Logger[IO].info("Hello Word Info").unsafeRunSync() | ||
Logger[IO].info(msg).unsafeRunSync() | ||
verify(underlying).info(msg) | ||
} | ||
it(s"Success expected info generic message") { | ||
Logger[IO].info(msgObj).unsafeRunSync() | ||
verify(underlying).info(msgObj.show) | ||
} | ||
} | ||
|
||
describe("Logger[F[_]].warn") { | ||
val f = fixture(_.isWarnEnabled, isEnabled = true) | ||
import f._ | ||
it(s"Success expected warn message") { | ||
Logger[IO].warn(msg).unsafeRunSync() | ||
verify(underlying).warn(msg) | ||
} | ||
it(s"Success expected warn generic message") { | ||
Logger[IO].warn(msgObj).unsafeRunSync() | ||
verify(underlying).warn(msgObj.show) | ||
} | ||
} | ||
|
||
describe("Logger[F[_]].debug") { | ||
val f = fixture(_.isDebugEnabled, isEnabled = true) | ||
import f._ | ||
it(s"Success expected debug message") { | ||
Logger[IO].debug(msg).unsafeRunSync() | ||
verify(underlying).debug(msg) | ||
} | ||
it(s"Success expected debug generic message") { | ||
Logger[IO].debug(msgObj).unsafeRunSync() | ||
verify(underlying).debug(msgObj.show) | ||
} | ||
} | ||
|
||
describe("Logger[F[_]].error") { | ||
val f = fixture(_.isErrorEnabled, isEnabled = true) | ||
import f._ | ||
lazy val exception = new Exception | ||
it(s"Success expected error message") { | ||
Logger[IO].error(msg).unsafeRunSync() | ||
verify(underlying).error(msg) | ||
} | ||
it(s"Success expected error message with Throwable") { | ||
Logger[IO].error(msg, exception).unsafeRunSync() | ||
verify(underlying).error(msg, exception) | ||
} | ||
it(s"Success expected error generic message") { | ||
Logger[IO].error(msgObj).unsafeRunSync() | ||
verify(underlying).error(msgObj.show) | ||
} | ||
it(s"Success expected error generic message with Throwable") { | ||
Logger[IO].error(msgObj, exception).unsafeRunSync() | ||
verify(underlying).error(msgObj.show, exception) | ||
} | ||
} | ||
|
||
def fixture(p: Underlying => Boolean, isEnabled: Boolean) = | ||
new { | ||
|
||
val msg = "msg" | ||
case class Msg(value: String) | ||
implicit val showMsg = new Show[Msg] { def show(t: Msg): String = t.toString } | ||
val msgObj = Msg(msg) | ||
|
||
implicit val underlying: Underlying = mock[org.slf4j.Logger] | ||
implicit val logger: Logger[IO] = Logger.instance[IO](classOf[LoggerSpec]) | ||
|
||
when(p(underlying)).thenReturn(isEnabled) | ||
} | ||
} |
Oops, something went wrong.