-
Notifications
You must be signed in to change notification settings - Fork 83
Add logger with Local
semantics
#909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
NthPortal
wants to merge
1
commit into
typelevel:main
Choose a base branch
from
NthPortal:local-logger/PR
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
core/shared/src/main/scala/org/typelevel/log4cats/LocalLogContext.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright 2018 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.typelevel.log4cats | ||
|
||
import cats.mtl.{Ask, Local} | ||
import cats.syntax.functor.* | ||
import cats.syntax.traverse.* | ||
import cats.{~>, Applicative, Show} | ||
import org.typelevel.otel4s.KindTransformer | ||
|
||
import scala.collection.immutable.ArraySeq | ||
|
||
sealed trait LocalLogContext[F[_]] { | ||
private[log4cats] def currentLogContext: F[Map[String, String]] | ||
|
||
private[log4cats] def withAddedContext[A](ctx: Map[String, String])(fa: F[A]): F[A] | ||
|
||
private[log4cats] final def withAddedContext[A](ctx: (String, Show.Shown)*)(fa: F[A]): F[A] = | ||
withAddedContext { | ||
ctx.view.map { case (k, v) => k -> v.toString }.toMap | ||
}(fa) | ||
|
||
def withHighPriorityAskedContext(ask: Ask[F, Map[String, String]]): LocalLogContext[F] | ||
|
||
def withLowPriorityAskedContext(ask: Ask[F, Map[String, String]]): LocalLogContext[F] | ||
|
||
def mapK[G[_]: Applicative](implicit kt: KindTransformer[F, G]): LocalLogContext[G] | ||
} | ||
|
||
object LocalLogContext { | ||
private[this] type AskContext[F[_]] = Ask[F, Map[String, String]] | ||
|
||
private[this] final class MappedKLocal[F[_], G[_], E]( | ||
localF: Local[F, E] | ||
)(implicit | ||
val applicative: Applicative[G], | ||
kt: KindTransformer[F, G] | ||
) extends Local[G, E] { | ||
def ask[E2 >: E]: G[E2] = | ||
kt.liftK(localF.ask[E2]) | ||
def local[A](ga: G[A])(f: E => E): G[A] = | ||
kt.limitedMapK(ga) { | ||
new (F ~> F) { | ||
def apply[B](fb: F[B]): F[B] = localF.local(fb)(f) | ||
} | ||
} | ||
} | ||
|
||
private[this] final class MappedKAsk[F[_], G[_], E]( | ||
askF: Ask[F, E], | ||
fk: F ~> G | ||
)(implicit val applicative: Applicative[G]) | ||
extends Ask[G, E] { | ||
def ask[E2 >: E]: G[E2] = fk(askF.ask[E2]) | ||
} | ||
|
||
private[this] final class MultiAskContext[F[_]] private[MultiAskContext] ( | ||
asks: Seq[AskContext[F]] /* never empty */ | ||
) extends AskContext[F] { | ||
implicit def applicative: Applicative[F] = asks.head.applicative | ||
def ask[E2 >: Map[String, String]]: F[E2] = | ||
asks | ||
.traverse(_.ask[Map[String, String]]) | ||
.map(_.reduceLeft(_ ++ _)) | ||
def prependLowPriority(ask: AskContext[F]): MultiAskContext[F] = | ||
new MultiAskContext(ask +: asks) | ||
def appendHighPriority(ask: AskContext[F]): MultiAskContext[F] = | ||
new MultiAskContext(asks :+ ask) | ||
} | ||
|
||
private[this] object MultiAskContext { | ||
def apply[F[_]](ask: AskContext[F]): MultiAskContext[F] = | ||
ask match { | ||
case multi: MultiAskContext[F] => multi | ||
case other => new MultiAskContext(ArraySeq(other)) | ||
} | ||
} | ||
|
||
private[this] final class Impl[F[_]]( | ||
localCtx: Local[F, Map[String, String]], | ||
askCtx: AskContext[F] | ||
) extends LocalLogContext[F] { | ||
private[log4cats] def currentLogContext: F[Map[String, String]] = | ||
askCtx.ask[Map[String, String]] | ||
private[log4cats] def withAddedContext[A](ctx: Map[String, String])(fa: F[A]): F[A] = | ||
localCtx.local(fa)(_ ++ ctx) | ||
|
||
def withHighPriorityAskedContext(ask: Ask[F, Map[String, String]]): LocalLogContext[F] = | ||
new Impl( | ||
localCtx, | ||
MultiAskContext(askCtx).appendHighPriority(ask) | ||
) | ||
|
||
def withLowPriorityAskedContext(ask: Ask[F, Map[String, String]]): LocalLogContext[F] = | ||
new Impl( | ||
localCtx, | ||
MultiAskContext(askCtx).prependLowPriority(ask) | ||
) | ||
|
||
def mapK[G[_]](implicit G: Applicative[G], kt: KindTransformer[F, G]): LocalLogContext[G] = { | ||
val localF = localCtx | ||
val askF = askCtx | ||
val localG = new MappedKLocal(localF) | ||
val askG = | ||
if (askF eq localF) localG | ||
else new MappedKAsk(askF, kt.liftK) | ||
new Impl(localG, askG) | ||
} | ||
} | ||
|
||
def fromLocal[F[_]](implicit localCtx: Local[F, Map[String, String]]): LocalLogContext[F] = | ||
new Impl(localCtx, localCtx) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these allow you to, for example, add trace and span IDs to the context. high/low priority is which wins if there's a key collision (shouldn't ever happen for span context, but could for other things)