Skip to content

Commit

Permalink
Add documentation for Log and LogOf.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtar committed Jan 7, 2025
1 parent 3e9f514 commit fa233cf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/src/main/scala/com/evolutiongaming/catshelper/Log.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ import org.slf4j.{Logger, MDC}
import scala.annotation.tailrec
import scala.collection.immutable.SortedMap

/** Context specific logger instance.
*
* Use [[LogOf]] to create the new instances of the class.
*
* The recommendation is to avoid passing `Log` instances implicitly as there
* could be multiple instances of `Log`, which could lead to confusion and
* log messages attributed to the wrong class, which leaked its own `Log`
* instances accidentially.
*
* @see [[LogOf]] for usage examples.
* @see [[org.slf4j.Logger]] for a typical underlying implementation.
*/
trait Log[F[_]] {

@inline def trace(msg: => String): F[Unit] = trace(msg, mdc = Log.Mdc.empty)
Expand Down
37 changes: 37 additions & 0 deletions core/src/main/scala/com/evolutiongaming/catshelper/LogOf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,43 @@ import org.slf4j.{ILoggerFactory, LoggerFactory}

import scala.reflect.ClassTag

/** Factory of [[Log]] instances.
*
* The intented usage is to have a single instance of `LogOf` in the
* application and use it to create `Log` instances for each class.
*
* The following could be written somewhere in the application initialization
* code such as [[cats.effect.IOApp]] instance:
* {{{
* implicit val logOf = LogOf.slf4j[F]
* }}}
*
* Then the typical example could look like following:
* {{{
* class UserService[F[_]: Monad](log: Log[F]) {
*
* def create(user: User): F[Unit] = {
* for {
* _ <- log.info(s"Creating user...")
* _ <- ...
* } yield ()
* }
* }
*
* object UserService {
*
* def of[F[_]: LogOf]: F[UserService[F]] = {
* for {
* log <- LogOf[F].forClass[UserService]
* service = new UserService[F](log)
* } yield service
* }
*
* }
* }}}
*
* @see [[org.slf4j.LoggerFactory]] for a typical underlying implementation.
*/
trait LogOf[F[_]] {

def apply(source: String): F[Log[F]]
Expand Down

0 comments on commit fa233cf

Please sign in to comment.