|
| 1 | +/* |
| 2 | + * Copyright 2018 Typelevel |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.typelevel.log4cats |
| 18 | +package mtl |
| 19 | + |
| 20 | +import cats.FlatMap |
| 21 | +import cats.syntax.flatMap._ |
| 22 | +import cats.syntax.functor._ |
| 23 | + |
| 24 | +private[mtl] class ContextualSelfAwareStructuredLogger[F[_]: Contextual: FlatMap]( |
| 25 | + sl: SelfAwareStructuredLogger[F] |
| 26 | +) extends SelfAwareStructuredLogger[F] { |
| 27 | + |
| 28 | + private val defaultCtx: F[Map[String, String]] = Contextual[F].current |
| 29 | + |
| 30 | + private def modify(ctx: Map[String, String]): F[Map[String, String]] = |
| 31 | + defaultCtx.map(c => c ++ ctx) |
| 32 | + |
| 33 | + def isTraceEnabled: F[Boolean] = sl.isTraceEnabled |
| 34 | + def isDebugEnabled: F[Boolean] = sl.isDebugEnabled |
| 35 | + def isInfoEnabled: F[Boolean] = sl.isInfoEnabled |
| 36 | + def isWarnEnabled: F[Boolean] = sl.isWarnEnabled |
| 37 | + def isErrorEnabled: F[Boolean] = sl.isErrorEnabled |
| 38 | + |
| 39 | + def error(message: => String): F[Unit] = |
| 40 | + defaultCtx.flatMap(sl.error(_)(message)) |
| 41 | + |
| 42 | + def warn(message: => String): F[Unit] = |
| 43 | + defaultCtx.flatMap(sl.warn(_)(message)) |
| 44 | + |
| 45 | + def info(message: => String): F[Unit] = |
| 46 | + defaultCtx.flatMap(sl.info(_)(message)) |
| 47 | + |
| 48 | + def debug(message: => String): F[Unit] = |
| 49 | + defaultCtx.flatMap(sl.debug(_)(message)) |
| 50 | + |
| 51 | + def trace(message: => String): F[Unit] = |
| 52 | + defaultCtx.flatMap(sl.trace(_)(message)) |
| 53 | + |
| 54 | + def error(t: Throwable)(message: => String): F[Unit] = |
| 55 | + defaultCtx.flatMap(sl.error(_, t)(message)) |
| 56 | + |
| 57 | + def warn(t: Throwable)(message: => String): F[Unit] = |
| 58 | + defaultCtx.flatMap(sl.warn(_, t)(message)) |
| 59 | + |
| 60 | + def info(t: Throwable)(message: => String): F[Unit] = |
| 61 | + defaultCtx.flatMap(sl.info(_, t)(message)) |
| 62 | + |
| 63 | + def debug(t: Throwable)(message: => String): F[Unit] = |
| 64 | + defaultCtx.flatMap(sl.debug(_, t)(message)) |
| 65 | + |
| 66 | + def trace(t: Throwable)(message: => String): F[Unit] = |
| 67 | + defaultCtx.flatMap(sl.trace(_, t)(message)) |
| 68 | + |
| 69 | + def trace(ctx: Map[String, String])(msg: => String): F[Unit] = |
| 70 | + modify(ctx).flatMap(sl.trace(_)(msg)) |
| 71 | + |
| 72 | + def debug(ctx: Map[String, String])(msg: => String): F[Unit] = |
| 73 | + modify(ctx).flatMap(sl.debug(_)(msg)) |
| 74 | + |
| 75 | + def info(ctx: Map[String, String])(msg: => String): F[Unit] = |
| 76 | + modify(ctx).flatMap(sl.info(_)(msg)) |
| 77 | + |
| 78 | + def warn(ctx: Map[String, String])(msg: => String): F[Unit] = |
| 79 | + modify(ctx).flatMap(sl.warn(_)(msg)) |
| 80 | + |
| 81 | + def error(ctx: Map[String, String])(msg: => String): F[Unit] = |
| 82 | + modify(ctx).flatMap(sl.error(_)(msg)) |
| 83 | + |
| 84 | + def error(ctx: Map[String, String], t: Throwable)(message: => String): F[Unit] = |
| 85 | + modify(ctx).flatMap(sl.error(_, t)(message)) |
| 86 | + |
| 87 | + def warn(ctx: Map[String, String], t: Throwable)(message: => String): F[Unit] = |
| 88 | + modify(ctx).flatMap(sl.warn(_, t)(message)) |
| 89 | + |
| 90 | + def info(ctx: Map[String, String], t: Throwable)(message: => String): F[Unit] = |
| 91 | + modify(ctx).flatMap(sl.info(_, t)(message)) |
| 92 | + |
| 93 | + def debug(ctx: Map[String, String], t: Throwable)(message: => String): F[Unit] = |
| 94 | + modify(ctx).flatMap(sl.debug(_, t)(message)) |
| 95 | + |
| 96 | + def trace(ctx: Map[String, String], t: Throwable)(message: => String): F[Unit] = |
| 97 | + modify(ctx).flatMap(sl.trace(_, t)(message)) |
| 98 | +} |
| 99 | + |
| 100 | +object ContextualSelfAwareStructuredLogger { |
| 101 | + |
| 102 | + /** |
| 103 | + * Creates a new [[SelfAwareStructuredLogger]] that adds information captured by `Contextual` to |
| 104 | + * the context. |
| 105 | + * |
| 106 | + * @example |
| 107 | + * {{{ |
| 108 | + * case class LogContext(logId: String) |
| 109 | + * |
| 110 | + * implicit val toLogContext: ToContext[LogContext] = |
| 111 | + * ctx => Map("log_id" -> ctx.logId) |
| 112 | + * |
| 113 | + * implicit val askLogContext: Ask[F, LogContext] = ??? |
| 114 | + * |
| 115 | + * val logger: SelfAwareStructuredLogger[F] = ??? // the general logger, e.g. Slf4jLogger |
| 116 | + * val contextual: SelfAwareStructuredLogger[F] = ContextualSelfAwareStructuredLogger(logger) |
| 117 | + * }}} |
| 118 | + */ |
| 119 | + def apply[F[_]: Contextual: FlatMap]( |
| 120 | + logger: SelfAwareStructuredLogger[F] |
| 121 | + ): SelfAwareStructuredLogger[F] = |
| 122 | + new ContextualSelfAwareStructuredLogger[F](logger) |
| 123 | + |
| 124 | +} |
0 commit comments