-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds log4s logging to free and tagless (#512)
* Adds log4s logging to free and tagless * Aligned packages and objects and fix typo * Adds documentation * Setting version to 0.6.2 * Improvement build.sbt and minor changes in documentation * Fix wrong compilation tut documentation * Adds deprecated to default implicit * Sort packages in one line * Very minor grammar changes You could use either both, depending on your requirements, and the usage would be quite similar. You could use either, or both, depending on your requirements, and the usage would be quite similar. If that is not accurate, just take out the or both and change to: You could use either, depending on your requirements, and the usage would be quite similar.
- Loading branch information
Showing
13 changed files
with
345 additions
and
19 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
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,20 @@ | ||
/* | ||
* Copyright 2017-2018 47 Degrees, LLC. <http://www.47deg.com> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package freestyle.free.loggingJVM | ||
|
||
@deprecated("Use freestyle.free.loggingJVM.journal.implicits or freestyle.free.loggingJVM.log4s.implicits instead", "0.6.2") | ||
object implicits extends freestyle.tagless.loggingJVM.journal.Implicits |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright 2017-2018 47 Degrees, LLC. <http://www.47deg.com> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package freestyle.free.loggingJVM.log4s | ||
|
||
object implicits extends freestyle.tagless.loggingJVM.log4s.Implicits |
20 changes: 20 additions & 0 deletions
20
modules/logging/jvm/src/main/scala/tagless/implicits.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,20 @@ | ||
/* | ||
* Copyright 2017-2018 47 Degrees, LLC. <http://www.47deg.com> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package freestyle.tagless.loggingJVM | ||
|
||
@deprecated("Use freestyle.tagless.loggingJVM.journal.implicits or freestyle.tagless.loggingJVM.log4s.implicits instead", "0.6.2") | ||
object implicits extends freestyle.tagless.loggingJVM.journal.Implicits |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2017-2018 47 Degrees, LLC. <http://www.47deg.com> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package freestyle.tagless.loggingJVM | ||
|
||
import cats.Applicative | ||
import freestyle.logging._ | ||
import freestyle.tagless.logging._ | ||
import org.log4s._ | ||
|
||
object log4s { | ||
|
||
sealed abstract class TaglessLoggingMHandler[M[_]] extends LoggingM.Handler[M] { | ||
|
||
import sourcecode.{File, Line} | ||
|
||
protected def withLogger[A](f: Logger => A): M[A] | ||
|
||
def debug(msg: String, srcInfo: Boolean)(implicit line: Line, file: File): M[Unit] = | ||
withLogger(_.debug(formatMessage(msg, srcInfo, line, file))) | ||
|
||
def debugWithCause(msg: String, cause: Throwable, srcInfo: Boolean)( | ||
implicit | ||
line: Line, | ||
file: File): M[Unit] = | ||
withLogger(_.debug(cause)(formatMessage(msg, srcInfo, line, file))) | ||
|
||
def error(msg: String, srcInfo: Boolean)(implicit line: Line, file: File): M[Unit] = | ||
withLogger(_.error(formatMessage(msg, srcInfo, line, file))) | ||
|
||
def errorWithCause(msg: String, cause: Throwable, srcInfo: Boolean)( | ||
implicit | ||
line: Line, | ||
file: File): M[Unit] = | ||
withLogger(_.error(cause)(formatMessage(msg, srcInfo, line, file))) | ||
|
||
def info(msg: String, srcInfo: Boolean)(implicit line: Line, file: File): M[Unit] = | ||
withLogger(_.info(formatMessage(msg, srcInfo, line, file))) | ||
|
||
def infoWithCause(msg: String, cause: Throwable, srcInfo: Boolean)( | ||
implicit | ||
line: Line, | ||
file: File): M[Unit] = | ||
withLogger(_.info(cause)(formatMessage(msg, srcInfo, line, file))) | ||
|
||
def warn(msg: String, srcInfo: Boolean)(implicit line: Line, file: File): M[Unit] = | ||
withLogger(_.warn(formatMessage(msg, srcInfo, line, file))) | ||
|
||
def warnWithCause(msg: String, cause: Throwable, srcInfo: Boolean)( | ||
implicit | ||
line: Line, | ||
file: File): M[Unit] = | ||
withLogger(_.warn(cause)(formatMessage(msg, srcInfo, line, file))) | ||
|
||
} | ||
|
||
trait Implicits { | ||
implicit def taglessLoggingApplicative[M[_]: Applicative]( | ||
implicit log: Logger = getLogger("")): LoggingM.Handler[M] = new TaglessLoggingMHandler[M] { | ||
|
||
protected def withLogger[A](f: Logger => A): M[A] = Applicative[M].pure(f(log)) | ||
|
||
} | ||
} | ||
|
||
object implicits extends Implicits | ||
} |
79 changes: 79 additions & 0 deletions
79
modules/logging/jvm/src/test/scala/free/LoggingTestsJournal.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,79 @@ | ||
/* | ||
* Copyright 2017-2018 47 Degrees, LLC. <http://www.47deg.com> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package freestyle.free | ||
|
||
import cats.instances.future._ | ||
import cats.{Id, Monad} | ||
import freestyle.free.implicits._ | ||
import freestyle.free.loggingJVM.implicits._ | ||
import org.scalatest.{AsyncWordSpec, Matchers} | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
import scala.util.control.NoStackTrace | ||
|
||
class LoggingTestsJournal extends AsyncWordSpec with Matchers { | ||
|
||
implicit override def executionContext = ExecutionContext.Implicits.global | ||
|
||
import algebras._ | ||
|
||
"Logging Freestyle free integration journal" should { | ||
|
||
case object Cause extends Exception("kaboom") with NoStackTrace | ||
|
||
"allow a log message to be interleaved inside a program monadic flow" in { | ||
val program = for { | ||
a <- app.nonLogging.x | ||
_ <- app.loggingM.debug("Debug Message", sourceAndLineInfo = true) | ||
_ <- app.loggingM.debugWithCause("Debug Message", Cause) | ||
_ <- app.loggingM.error("Error Message") | ||
_ <- app.loggingM.errorWithCause("Error Message", Cause) | ||
_ <- app.loggingM.info("Info Message") | ||
_ <- app.loggingM.infoWithCause("Info Message", Cause) | ||
_ <- app.loggingM.warn("Warning Message") | ||
_ <- app.loggingM.warnWithCause("Warning Message", Cause) | ||
b <- FreeS.pure(1) | ||
} yield a + b | ||
program.interpret[Future] map { _ shouldBe 2 } | ||
} | ||
|
||
"not depend on MonadError, thus allowing use of Monads without MonadError, like Id, for test algebras" in { | ||
val program = for { | ||
a <- app.nonLogging.x | ||
_ <- app.loggingM.info("Info Message") | ||
_ <- app.loggingM.infoWithCause("Info Message", Cause) | ||
b <- FreeS.pure(1) | ||
} yield a + b | ||
program.interpret[TestAlgebra].run("configHere") shouldBe 2 | ||
} | ||
|
||
"allow injecting a Logger instance" in { | ||
val program = for { | ||
a <- FreeS.pure(1) | ||
_ <- app.loggingM.info("Info Message") | ||
_ <- app.loggingM.error("Error Message") | ||
b <- FreeS.pure(1) | ||
} yield a + b | ||
|
||
implicit val logger = journal.Logger("Potatoes") | ||
|
||
program | ||
.interpret[TestAlgebra] | ||
.run("configHere") shouldEqual 2 | ||
} | ||
} | ||
} |
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
Oops, something went wrong.