File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed
core/src/main/scala/com/evolutiongaming/catshelper Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,18 @@ import org.slf4j.{Logger, MDC}
11
11
import scala .annotation .tailrec
12
12
import scala .collection .immutable .SortedMap
13
13
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
+ */
14
26
trait Log [F [_]] {
15
27
16
28
@ inline def trace (msg : => String ): F [Unit ] = trace(msg, mdc = Log .Mdc .empty)
Original file line number Diff line number Diff line change @@ -8,6 +8,43 @@ import org.slf4j.{ILoggerFactory, LoggerFactory}
8
8
9
9
import scala .reflect .ClassTag
10
10
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
+ */
11
48
trait LogOf [F [_]] {
12
49
13
50
def apply (source : String ): F [Log [F ]]
You can’t perform that action at this time.
0 commit comments