Skip to content

Commit fa233cf

Browse files
committed
Add documentation for Log and LogOf.
1 parent 3e9f514 commit fa233cf

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

core/src/main/scala/com/evolutiongaming/catshelper/Log.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ import org.slf4j.{Logger, MDC}
1111
import scala.annotation.tailrec
1212
import scala.collection.immutable.SortedMap
1313

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

1628
@inline def trace(msg: => String): F[Unit] = trace(msg, mdc = Log.Mdc.empty)

core/src/main/scala/com/evolutiongaming/catshelper/LogOf.scala

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,43 @@ import org.slf4j.{ILoggerFactory, LoggerFactory}
88

99
import scala.reflect.ClassTag
1010

11+
/** Factory of [[Log]] instances.
12+
*
13+
* The intented usage is to have a single instance of `LogOf` in the
14+
* application and use it to create `Log` instances for each class.
15+
*
16+
* The following could be written somewhere in the application initialization
17+
* code such as [[cats.effect.IOApp]] instance:
18+
* {{{
19+
* implicit val logOf = LogOf.slf4j[F]
20+
* }}}
21+
*
22+
* Then the typical example could look like following:
23+
* {{{
24+
* class UserService[F[_]: Monad](log: Log[F]) {
25+
*
26+
* def create(user: User): F[Unit] = {
27+
* for {
28+
* _ <- log.info(s"Creating user...")
29+
* _ <- ...
30+
* } yield ()
31+
* }
32+
* }
33+
*
34+
* object UserService {
35+
*
36+
* def of[F[_]: LogOf]: F[UserService[F]] = {
37+
* for {
38+
* log <- LogOf[F].forClass[UserService]
39+
* service = new UserService[F](log)
40+
* } yield service
41+
* }
42+
*
43+
* }
44+
* }}}
45+
*
46+
* @see [[org.slf4j.LoggerFactory]] for a typical underlying implementation.
47+
*/
1148
trait LogOf[F[_]] {
1249

1350
def apply(source: String): F[Log[F]]

0 commit comments

Comments
 (0)