Skip to content

Commit 6eeb3d7

Browse files
committed
fixup! Add logger with Local semantics
yikes
1 parent 542a508 commit 6eeb3d7

File tree

2 files changed

+60
-41
lines changed

2 files changed

+60
-41
lines changed

core/shared/src/main/scala/org/typelevel/log4cats/LocalLogger.scala

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,21 @@
1717
package org.typelevel.log4cats
1818

1919
import cats.mtl.{LiftKind, Local}
20-
import cats.syntax.functor.*
20+
import cats.syntax.flatMap.*
2121
import cats.{~>, Monad, Show}
2222

2323
sealed trait LocalLogger[F[_]] extends SelfAwareLogger[F] {
24+
25+
/**
26+
* @return
27+
* the given effect modified to have the provided context stored [[cats.mtl.Local locally]]
28+
*/
2429
def withAddedContext[A](ctx: Map[String, String])(fa: F[A]): F[A]
2530

31+
/**
32+
* @return
33+
* the given effect modified to have the provided context stored [[cats.mtl.Local locally]]
34+
*/
2635
def withAddedContext[A](ctx: (String, Show.Shown)*)(fa: F[A]): F[A]
2736

2837
def error(ctx: Map[String, String])(msg: => String): F[Unit]
@@ -42,6 +51,7 @@ sealed trait LocalLogger[F[_]] extends SelfAwareLogger[F] {
4251
)
4352
override def mapK[G[_]](fk: F ~> G): SelfAwareLogger[G] = super.mapK(fk)
4453

54+
/** Lifts this logger's context from `F` to `G`. */
4555
def liftTo[G[_]](implicit lift: LiftKind[F, G], G: Monad[G]): LocalLogger[G]
4656

4757
override def withModifiedString(f: String => String): LocalLogger[F]
@@ -95,126 +105,126 @@ object LocalLogger {
95105

96106
def error(message: => String): F[Unit] =
97107
F.ifM(underlying.isErrorEnabled)(
98-
for (localCtx <- localLogContext.currentLogContext)
99-
yield underlying.error(localCtx)(message),
108+
localLogContext.currentLogContext
109+
.flatMap(underlying.error(_)(message)),
100110
F.unit
101111
)
102112
def error(t: Throwable)(message: => String): F[Unit] =
103113
F.ifM(underlying.isErrorEnabled)(
104-
for (localCtx <- localLogContext.currentLogContext)
105-
yield underlying.error(localCtx, t)(message),
114+
localLogContext.currentLogContext
115+
.flatMap(underlying.error(_, t)(message)),
106116
F.unit
107117
)
108118
def error(ctx: Map[String, String])(msg: => String): F[Unit] =
109119
F.ifM(underlying.isErrorEnabled)(
110-
for (localCtx <- localLogContext.currentLogContext)
111-
yield underlying.error(localCtx ++ ctx)(msg),
120+
localLogContext.currentLogContext
121+
.flatMap(localCtx => underlying.error(localCtx ++ ctx)(msg)),
112122
F.unit
113123
)
114124
def error(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] =
115125
F.ifM(underlying.isErrorEnabled)(
116-
for (localCtx <- localLogContext.currentLogContext)
117-
yield underlying.error(localCtx ++ ctx, t)(msg),
126+
localLogContext.currentLogContext
127+
.flatMap(localCtx => underlying.error(localCtx ++ ctx, t)(msg)),
118128
F.unit
119129
)
120130

121131
def warn(message: => String): F[Unit] =
122132
F.ifM(underlying.isWarnEnabled)(
123-
for (localCtx <- localLogContext.currentLogContext)
124-
yield underlying.warn(localCtx)(message),
133+
localLogContext.currentLogContext
134+
.flatMap(underlying.warn(_)(message)),
125135
F.unit
126136
)
127137
def warn(t: Throwable)(message: => String): F[Unit] =
128138
F.ifM(underlying.isWarnEnabled)(
129-
for (localCtx <- localLogContext.currentLogContext)
130-
yield underlying.warn(localCtx, t)(message),
139+
localLogContext.currentLogContext
140+
.flatMap(underlying.warn(_, t)(message)),
131141
F.unit
132142
)
133143
def warn(ctx: Map[String, String])(msg: => String): F[Unit] =
134144
F.ifM(underlying.isWarnEnabled)(
135-
for (localCtx <- localLogContext.currentLogContext)
136-
yield underlying.warn(localCtx ++ ctx)(msg),
145+
localLogContext.currentLogContext
146+
.flatMap(localCtx => underlying.warn(localCtx ++ ctx)(msg)),
137147
F.unit
138148
)
139149
def warn(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] =
140150
F.ifM(underlying.isWarnEnabled)(
141-
for (localCtx <- localLogContext.currentLogContext)
142-
yield underlying.warn(localCtx ++ ctx, t)(msg),
151+
localLogContext.currentLogContext
152+
.flatMap(localCtx => underlying.warn(localCtx ++ ctx, t)(msg)),
143153
F.unit
144154
)
145155

146156
def info(message: => String): F[Unit] =
147157
F.ifM(underlying.isInfoEnabled)(
148-
for (localCtx <- localLogContext.currentLogContext)
149-
yield underlying.info(localCtx)(message),
158+
localLogContext.currentLogContext
159+
.flatMap(underlying.info(_)(message)),
150160
F.unit
151161
)
152162
def info(t: Throwable)(message: => String): F[Unit] =
153163
F.ifM(underlying.isInfoEnabled)(
154-
for (localCtx <- localLogContext.currentLogContext)
155-
yield underlying.info(localCtx, t)(message),
164+
localLogContext.currentLogContext
165+
.flatMap(underlying.info(_, t)(message)),
156166
F.unit
157167
)
158168
def info(ctx: Map[String, String])(msg: => String): F[Unit] =
159169
F.ifM(underlying.isInfoEnabled)(
160-
for (localCtx <- localLogContext.currentLogContext)
161-
yield underlying.info(localCtx ++ ctx)(msg),
170+
localLogContext.currentLogContext
171+
.flatMap(localCtx => underlying.info(localCtx ++ ctx)(msg)),
162172
F.unit
163173
)
164174
def info(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] =
165175
F.ifM(underlying.isInfoEnabled)(
166-
for (localCtx <- localLogContext.currentLogContext)
167-
yield underlying.info(localCtx ++ ctx, t)(msg),
176+
localLogContext.currentLogContext
177+
.flatMap(localCtx => underlying.info(localCtx ++ ctx, t)(msg)),
168178
F.unit
169179
)
170180

171181
def debug(message: => String): F[Unit] =
172182
F.ifM(underlying.isDebugEnabled)(
173-
for (localCtx <- localLogContext.currentLogContext)
174-
yield underlying.debug(localCtx)(message),
183+
localLogContext.currentLogContext
184+
.flatMap(underlying.debug(_)(message)),
175185
F.unit
176186
)
177187
def debug(t: Throwable)(message: => String): F[Unit] =
178188
F.ifM(underlying.isDebugEnabled)(
179-
for (localCtx <- localLogContext.currentLogContext)
180-
yield underlying.debug(localCtx, t)(message),
189+
localLogContext.currentLogContext
190+
.flatMap(underlying.debug(_, t)(message)),
181191
F.unit
182192
)
183193
def debug(ctx: Map[String, String])(msg: => String): F[Unit] =
184194
F.ifM(underlying.isDebugEnabled)(
185-
for (localCtx <- localLogContext.currentLogContext)
186-
yield underlying.debug(localCtx ++ ctx)(msg),
195+
localLogContext.currentLogContext
196+
.flatMap(localCtx => underlying.debug(localCtx ++ ctx)(msg)),
187197
F.unit
188198
)
189199
def debug(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] =
190200
F.ifM(underlying.isDebugEnabled)(
191-
for (localCtx <- localLogContext.currentLogContext)
192-
yield underlying.debug(localCtx ++ ctx, t)(msg),
201+
localLogContext.currentLogContext
202+
.flatMap(localCtx => underlying.debug(localCtx ++ ctx, t)(msg)),
193203
F.unit
194204
)
195205

196206
def trace(message: => String): F[Unit] =
197207
F.ifM(underlying.isTraceEnabled)(
198-
for (localCtx <- localLogContext.currentLogContext)
199-
yield underlying.trace(localCtx)(message),
208+
localLogContext.currentLogContext
209+
.flatMap(underlying.trace(_)(message)),
200210
F.unit
201211
)
202212
def trace(t: Throwable)(message: => String): F[Unit] =
203213
F.ifM(underlying.isTraceEnabled)(
204-
for (localCtx <- localLogContext.currentLogContext)
205-
yield underlying.trace(localCtx, t)(message),
214+
localLogContext.currentLogContext
215+
.flatMap(underlying.trace(_, t)(message)),
206216
F.unit
207217
)
208218
def trace(ctx: Map[String, String])(msg: => String): F[Unit] =
209219
F.ifM(underlying.isTraceEnabled)(
210-
for (localCtx <- localLogContext.currentLogContext)
211-
yield underlying.trace(localCtx ++ ctx)(msg),
220+
localLogContext.currentLogContext
221+
.flatMap(localCtx => underlying.trace(localCtx ++ ctx)(msg)),
212222
F.unit
213223
)
214224
def trace(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] =
215225
F.ifM(underlying.isTraceEnabled)(
216-
for (localCtx <- localLogContext.currentLogContext)
217-
yield underlying.trace(localCtx ++ ctx, t)(msg),
226+
localLogContext.currentLogContext
227+
.flatMap(localCtx => underlying.trace(localCtx ++ ctx, t)(msg)),
218228
F.unit
219229
)
220230
}

core/shared/src/main/scala/org/typelevel/log4cats/LocalLoggerFactory.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,19 @@ import cats.{Functor, Monad, Show}
2323
sealed trait LocalLoggerFactory[F[_]] extends LoggerFactoryGen[F] {
2424
final type LoggerType = LocalLogger[F]
2525

26+
/**
27+
* @return
28+
* the given effect modified to have the provided context stored [[cats.mtl.Local locally]]
29+
*/
2630
def withAddedContext[A](ctx: Map[String, String])(fa: F[A]): F[A]
2731

32+
/**
33+
* @return
34+
* the given effect modified to have the provided context stored [[cats.mtl.Local locally]]
35+
*/
2836
def withAddedContext[A](ctx: (String, Show.Shown)*)(fa: F[A]): F[A]
2937

38+
/** Lifts this factory's context from `F` to `G`. */
3039
def liftTo[G[_]](implicit lift: LiftKind[F, G], G: Monad[G]): LocalLoggerFactory[G]
3140

3241
def withModifiedString(f: String => String)(implicit F: Functor[F]): LocalLoggerFactory[F]

0 commit comments

Comments
 (0)