Skip to content

Commit

Permalink
Merge pull request #9 from llfrometa89/master
Browse files Browse the repository at this point in the history
Refactoring structure in modules [core|cats|scalaz]
  • Loading branch information
llfrometa89 authored Mar 3, 2019
2 parents 7d5a64a + a511713 commit ac6aa7d
Show file tree
Hide file tree
Showing 20 changed files with 601 additions and 106 deletions.
22 changes: 22 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
style = defaultWithAlign
maxColumn = 120

continuationIndent.callSite = 2

newlines {
sometimesBeforeColonInMethodReturnType = false
}

align {
arrowEnumeratorGenerator = false
ifWhileOpenParen = false
openParenCallSite = false
openParenDefnSite = false
}

docstrings = JavaDoc

rewrite {
rules = [SortImports, RedundantBraces, RedundantParens]
redundantBraces.maxLines = 1
}
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ jobs:
script: sbt scalafmt::test
- stage: test
script:
- sbt ++$TRAVIS_SCALA_VERSION clean coverage +coreJVM/test coverageReport
- sbt ++$TRAVIS_SCALA_VERSION clean coverage coreJVM/test catsJVM/test scalazJVM/test coverageReport
- sbt validateScalafmt
- sbt example/compile
- sbt exampleCats/compile exampleScalaz/compile
- stage: release
script: sbt ci-release

Expand Down
52 changes: 40 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Logger4s is a wrapping [SLF4J](https://www.slf4j.org/) purely functional library for Scala.
It's easy to use and does not force a specific target context.
You can run your computations in any type `F[_]` that has an instance of cats-effect's `Sync[F]`.
You can run your computations in any type `F[_]`.

## Prerequisites ##

Expand All @@ -16,41 +16,56 @@ Logger4s is published to Sonatype OSS and Maven Central:

- Group id / organization: *org.pure4s*
- Artifact id / name: *logger4s*
- Latest version is 0.2.0
- Latest version is 0.3.0

Usage with SBT, adding a dependency to the latest version of Logger4s to your `build.sbt`:

```scala
// For Scala 2.11, or 2.12
libraryDependencies += "org.pure4s" %% "logger4s" % "0.2.0"
libraryDependencies += "org.pure4s" %% "logger4s-core" % "0.3.0" // Only if you want to support any backend
libraryDependencies += "org.pure4s" %% "logger4s-cats" % "0.3.0" // Cats ecosystem (cats-effect)
libraryDependencies += "org.pure4s" %% "logger4s-scalaz" % "0.3.0" // Scalaz ecosystem (scalaz-zio)
```

## Using Logger4s ##

Basic example:
Example with LazyLogging and Cats:
```scala
import cats.effect.IO
import org.pure4s.logger4s.LazyLogging
import org.pure4s.logger4s.cats.Logger
import org.pure4s.logger4s.cats.Logger._

object Main extends App with LazyLogging {
Logger[IO].info(s"Hello word, functional logger").unsafeRunSync()
//2019-03-03 21:34:04.880 [BasicExampleMain$][INFO ] Hello word, purely functional logger
}
```

Example without LazyLogging and Cats:
```scala
import cats.effect.{IO, Sync}
import cats.implicits._
import org.pure4s.logger4s.Logger
import org.pure4s.logger4s.cats.Logger

case class User(email: String)

class UserService[F[_] : Sync : Logger] {
def findByEmail(email: String): F[Option[User]] = {
Logger[F].info(s"Hello word, functional logger ($email)") *> Option(User(email)).pure[F]
Logger[F].info(s"User email is $email") *> Option(User(email)).pure[F]
}
}

object BasicExampleMain extends App {
object Main extends App {
implicit val instance: Logger[IO] = Logger.instance[IO](classOf[UserService[IO]])

val service = new UserService[IO]
service.findByEmail("example@example.com").unsafeRunSync()
//2019-01-27 21:40:40.557 [UserService][INFO] - Hello word, functional logger (example@example.com)
//2019-03-03 21:35:35.286 [UserService][INFO ] User email is example@example.com
}
```

Basic example with LazyLogging
Example for comprehensions with LazyLogging and Cats
```scala
import cats.effect.{IO, Sync}
import cats.implicits._
Expand Down Expand Up @@ -78,15 +93,15 @@ class AuthService[F[_] : Sync] extends LazyLogging {
}
}

object BasicLazyLoggingExampleMain extends App {
object Main extends App {
val service = new AuthService[IO]
service.login("example@example.com","123").unsafeRunSync()
//2019-01-27 21:40:40.557 [AuthService][INFO] - Login with email = example@example.com and password = 123
//2019-01-27 21:40:40.557 [AuthService][INFO] - Success login with session = Session(example@example.com,token)
}
```

Complex example:
Advance example, custom `Show` with LazyLogging and Cats:
```scala
import cats.Show
import cats.effect.{IO, Sync}
Expand Down Expand Up @@ -115,13 +130,26 @@ class ClientService[F[_] : Sync] extends LazyLogging{
}
}

object ComplexExampleMain extends App {
object Main extends App {
val service = new ClientService[IO]
service.findByEmail("example@example.com").unsafeRunSync()
//2019-01-27 21:25:26.150 [ClientService][INFO] - {"email":"example@example.com"}
}
```

Example with LazyLogging and Scalaz:
```scala
import org.pure4s.logger4s.LazyLogging
import scalaz.zio.{IO, RTS}
import org.pure4s.logger4s.scalaz.Logger

object Main extends App with RTS with LazyLogging {

unsafeRun(Logger[IO[Nothing, ?]].info(s"Hello word, purely functional logger"))
//2019-03-03 21:49:59.905 [BasicExampleMain$][INFO ] Hello word, purely functional logger
}
```

## Code of conduct

People are expected to follow the [conduct-code] when discussing the project on the available communication channels.
Expand Down
65 changes: 57 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ lazy val V = new {
val logbackClassicVersion = "1.2.3"
val json4sVersion = "3.6.4"
val mockitoVersion = "1.10.19"
val scalazZIOVersion = "0.6.3"
val scalazCoreVersion = "7.2.27"
}

val noPublishSettings = Seq(
Expand All @@ -48,10 +50,17 @@ val buildSettings = Seq(

val commonDependencies = Seq(
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-core" % V.catsVersion,
"org.typelevel" %% "cats-effect" % V.catsEffectVersion,
"org.scalatest" %% "scalatest" % V.scalaTestVersion % Test,
"org.mockito" % "mockito-all" % V.mockitoVersion % Test
"org.scalatest" %% "scalatest" % V.scalaTestVersion % Test,
"org.mockito" % "mockito-all" % V.mockitoVersion % Test
)
)

val compilerPlugins = Seq(
libraryDependencies ++= Seq(
compilerPlugin(
"org.scalamacros" %% "paradise" % V.macroParadiseVersion cross CrossVersion.full),
compilerPlugin(
"org.spire-math" %% "kind-projector" % V.kindProjectorVersion)
)
)

Expand All @@ -65,7 +74,7 @@ lazy val logger4s = project
lazy val core = crossProject(JVMPlatform)
.crossType(CrossType.Full)
.in(file("core"))
.settings(moduleName := "logger4s")
.settings(moduleName := "logger4s-core")
.settings(buildSettings)
.settings(commonDependencies)
.jvmSettings(libraryDependencies ++= Seq(
Expand All @@ -74,16 +83,56 @@ lazy val core = crossProject(JVMPlatform)

lazy val coreJVM = core.jvm

lazy val example = project
.in(file("example"))
lazy val cats = crossProject(JVMPlatform)
.crossType(CrossType.Pure)
.in(file("logger4s-cats"))
.settings(moduleName := "logger4s-cats")
.settings(buildSettings)
.settings(commonDependencies)
.settings(compilerPlugins)
.settings(libraryDependencies ++= Seq(
"org.typelevel" %% "cats-core" % V.catsVersion,
"org.typelevel" %% "cats-effect" % V.catsEffectVersion,
))
.dependsOn(core)

lazy val catsJVM = cats.jvm

lazy val scalaz = crossProject(JVMPlatform)
.crossType(CrossType.Pure)
.in(file("logger4s-scalaz"))
.settings(moduleName := "logger4s-scalaz")
.settings(buildSettings)
.settings(commonDependencies)
.settings(compilerPlugins)
.settings(libraryDependencies ++= Seq(
"org.scalaz" %% "scalaz-core" % V.scalazCoreVersion,
"org.scalaz" %% "scalaz-zio" % V.scalazZIOVersion
))
.dependsOn(core)

lazy val scalazJVM = scalaz.jvm

lazy val exampleCats = project
.in(file("example-cats"))
.settings(buildSettings)
.settings(noPublishSettings)
.dependsOn(coreJVM)
.dependsOn(coreJVM, catsJVM)
.settings(
libraryDependencies ++= Seq(
"org.json4s" %% "json4s-native" % V.json4sVersion
))

lazy val exampleScalaz = project
.in(file("example-scalaz"))
.settings(buildSettings)
.settings(noPublishSettings)
.settings(compilerPlugins)
.dependsOn(coreJVM, scalazJVM)
.settings(libraryDependencies ++= Seq(
"org.json4s" %% "json4s-native" % V.json4sVersion
))

addCommandAlias(
"validateScalafmt",
";sbt:scalafmt::test;test:scalafmt::test;compile:scalafmt::test")
13 changes: 0 additions & 13 deletions core/shared/src/main/scala/org/pure4s/logger4s/GLogger.scala

This file was deleted.

49 changes: 6 additions & 43 deletions core/shared/src/main/scala/org/pure4s/logger4s/Logger.scala
Original file line number Diff line number Diff line change
@@ -1,46 +1,9 @@
package org.pure4s.logger4s

import cats.Show
import cats.effect.Sync
import org.slf4j
import org.slf4j.LoggerFactory
import cats.implicits._

trait Logger[F[_]] extends SLogger[F] with GLogger[F]

object Logger {

def apply[F[_]](implicit F: Logger[F]): Logger[F] = F

def instance[F[_] : Sync](clazz: Class[_])
(implicit logger: slf4j.Logger = LoggerFactory.getLogger(clazz)): Logger[F] = new Logger[F] {

def error(msg: String): F[Unit] = Sync[F].delay(logger.error(msg))
def error(msg: String, err: Throwable): F[Unit] = Sync[F].delay(logger.error(msg, err))
def warn(msg: String): F[Unit] = Sync[F].delay(logger.warn(msg))
def info(msg: String): F[Unit] = Sync[F].delay(logger.info(msg))
def debug(msg: String): F[Unit] = Sync[F].delay(logger.debug(msg))

def error[A: Show](msg: A): F[Unit] = error(msg.show)
def error[A: Show](msg: A, err: Throwable): F[Unit] = error(msg.show, err)
def warn[A: Show](msg: A): F[Unit] = warn(msg.show)
def info[A: Show](msg: A): F[Unit] = info(msg.show)
def debug[A: Show](msg: A): F[Unit] = debug(msg.show)

}

implicit def instance[F[_] : Sync](implicit logger: slf4j.Logger): Logger[F] = new Logger[F] {

def error(msg: String): F[Unit] = Sync[F].delay(logger.error(msg))
def error(msg: String, err: Throwable): F[Unit] = Sync[F].delay(logger.error(msg, err))
def warn(msg: String): F[Unit] = Sync[F].delay(logger.warn(msg))
def info(msg: String): F[Unit] = Sync[F].delay(logger.info(msg))
def debug(msg: String): F[Unit] = Sync[F].delay(logger.debug(msg))

def error[A: Show](msg: A): F[Unit] = error(msg.show)
def error[A: Show](msg: A, err: Throwable): F[Unit] = error(msg.show, err)
def warn[A: Show](msg: A): F[Unit] = warn(msg.show)
def info[A: Show](msg: A): F[Unit] = info(msg.show)
def debug[A: Show](msg: A): F[Unit] = debug(msg.show)
}
trait Logger[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]
}
11 changes: 0 additions & 11 deletions core/shared/src/main/scala/org/pure4s/logger4s/SLogger.scala

This file was deleted.

File renamed without changes.
8 changes: 8 additions & 0 deletions example-cats/src/main/scala/BasicExampleMain.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import cats.effect.IO
import org.pure4s.logger4s.LazyLogging
import org.pure4s.logger4s.cats.Logger
import org.pure4s.logger4s.cats.Logger._

object BasicExampleMain extends App with LazyLogging {
Logger[IO].info(s"Hello word, purely functional logger").unsafeRunSync()
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import cats.effect.{IO, Sync}
import cats.implicits._
import org.pure4s.logger4s.{LazyLogging, Logger}
import org.pure4s.logger4s.Logger._
import org.pure4s.logger4s.LazyLogging
import org.pure4s.logger4s.cats.Logger
import org.pure4s.logger4s.cats.Logger._

case class Session(email: String, token: String)

class AuthService[F[_] : Sync] extends LazyLogging {
class AuthService[F[_]: Sync] extends LazyLogging {

def login(email: String, password: String): F[Session] = {

Expand All @@ -27,5 +28,5 @@ class AuthService[F[_] : Sync] extends LazyLogging {

object BasicLazyLoggingExampleMain extends App {
val service = new AuthService[IO]
service.login("example@example.com","123").unsafeRunSync()
service.login("example@example.com", "123").unsafeRunSync()
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import cats.Show
import cats.effect.{IO, Sync}
import cats.implicits._
import org.json4s.{Formats, NoTypeHints}
import org.json4s.native.Serialization
import org.pure4s.logger4s.{LazyLogging, Logger}
import org.pure4s.logger4s.Logger._
import org.json4s.native.Serialization.write
import org.json4s.{Formats, NoTypeHints}
import org.pure4s.logger4s.LazyLogging
import org.pure4s.logger4s.cats.Logger
import org.pure4s.logger4s.cats.Logger._

case class Client(email: String)

Expand All @@ -16,7 +17,7 @@ object Client {
}
}

class ClientService[F[_] : Sync] extends LazyLogging{
class ClientService[F[_]: Sync] extends LazyLogging {
import Client._

def findByEmail(email: String): F[Option[Client]] = {
Expand Down
Loading

0 comments on commit ac6aa7d

Please sign in to comment.