-
Notifications
You must be signed in to change notification settings - Fork 3
/
DynamicOdinLogger.scala
134 lines (117 loc) · 4.18 KB
/
DynamicOdinLogger.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright 2022 Permutive
*
* 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 com.permutive.logging.dynamic.odin
import cats.effect.kernel._
import cats.kernel.Eq
import cats.syntax.eq._
import cats.syntax.flatMap._
import cats.syntax.functor._
import cats.{Applicative, Monad}
import com.permutive.logging.dynamic.odin.DynamicOdinConsoleLogger.RuntimeConfig
import io.odin.config.enclosureRouting
import io.odin.formatter.Formatter
import io.odin.loggers.DefaultLogger
import io.odin.syntax._
import io.odin.{consoleLogger, Level, Logger, LoggerMessage}
import scala.concurrent.duration._
trait DynamicOdinConsoleLogger[F[_]] extends Logger[F] {
def update(config: RuntimeConfig): F[Boolean]
def getConfig: F[RuntimeConfig]
}
class DynamicOdinConsoleLoggerImpl[F[_]: Monad: Clock] private[odin] (
ref: Ref[F, (RuntimeConfig, Logger[F])],
level: Level
)(make: RuntimeConfig => Logger[F])(implicit eq: Eq[RuntimeConfig])
extends DefaultLogger[F](level)
with DynamicOdinConsoleLogger[F] { outer =>
protected def withLogger(f: Logger[F] => F[Unit]): F[Unit] = ref.get.flatMap {
case (_, l) => f(l)
}
override def update(config: RuntimeConfig): F[Boolean] =
ref.get
.map(_._1.neqv(config))
.flatTap(Applicative[F].whenA(_)(ref.set(config -> make(config))))
override def getConfig: F[RuntimeConfig] = ref.get.map(_._1)
override def submit(msg: LoggerMessage): F[Unit] = withLogger(_.log(msg))
override def withMinimalLevel(level: Level): Logger[F] =
new DynamicOdinConsoleLoggerImpl[F](ref, level)(make) {
override protected def withLogger(f: Logger[F] => F[Unit]): F[Unit] =
outer.withLogger(l => f(l.withMinimalLevel(level)))
}
}
object DynamicOdinConsoleLogger {
case class Config(
formatter: Formatter,
asyncTimeWindow: FiniteDuration = 1.millis,
asyncMaxBufferSize: Option[Int] = None
)
case class RuntimeConfig(
minLevel: Level,
levelMapping: Map[String, Level] = Map.empty
)
object RuntimeConfig {
implicit val eq: Eq[RuntimeConfig] = cats.derived.semiauto.eq
}
def console[F[_]: Async](config: Config, initialConfig: RuntimeConfig)(
implicit eq: Eq[RuntimeConfig]
): Resource[F, DynamicOdinConsoleLogger[F]] =
create(config, initialConfig)(c =>
consoleLogger(config.formatter, c.minLevel)
)
def create[F[_]: Async](
config: Config,
runtimeConfig: RuntimeConfig
)(
make: RuntimeConfig => Logger[F]
)(implicit
eq: Eq[RuntimeConfig]
): Resource[F, DynamicOdinConsoleLogger[F]] = {
val makeWithLevels: RuntimeConfig => Logger[F] = { config =>
val mainLogger = make(config)
if (config.levelMapping.isEmpty) mainLogger
else
enclosureRouting(
config.levelMapping.view
.mapValues(mainLogger.withMinimalLevel)
.toList: _*
)
.withFallback(mainLogger)
}
for {
ref <- Resource.eval(
Ref.of[F, (RuntimeConfig, Logger[F])](
runtimeConfig -> makeWithLevels(runtimeConfig)
)
)
underlying = new DynamicOdinConsoleLoggerImpl[F](
ref,
runtimeConfig.minLevel
)(makeWithLevels)
async <- underlying.withAsync(
config.asyncTimeWindow,
config.asyncMaxBufferSize
)
} yield new DefaultLogger[F](async.minLevel)
with DynamicOdinConsoleLogger[F] {
override def submit(msg: LoggerMessage): F[Unit] = async.log(msg)
override def update(config: RuntimeConfig): F[Boolean] =
underlying.update(config)
override def getConfig: F[RuntimeConfig] = underlying.getConfig
override def withMinimalLevel(level: Level): Logger[F] =
async.withMinimalLevel(level)
}
}
}