-
-
Notifications
You must be signed in to change notification settings - Fork 79
SAM Type Interface #920
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
base: main
Are you sure you want to change the base?
SAM Type Interface #920
Changes from all commits
485b98b
8d8223f
f6e9d55
e7f0be4
7f6dc0e
9c3c243
9908c00
4fb2850
fc58670
23b2094
3f1f34c
7961c15
a5708c3
649468d
ac5aaee
53dda4f
634de7d
b7f0d10
859753b
ac1a39b
3beb5f7
44ab02b
10b0c81
4b746e2
2ad479c
842bb7e
fd6bab1
63ba4c4
5b85513
022f96b
3d00cf9
17cb902
3df4963
4602d56
f139d0c
401c5ad
f88c446
10cd36c
e8dd135
f9364f1
aafe9ba
ad0143e
bd68364
d19ffae
749fd3d
72376e8
b0ddff1
58f2b51
b606c3b
6962ff8
041375c
468114f
cb28f49
f695de3
f6bf9a3
329bc40
a32d908
809e560
1b7aeb9
2a9fe26
3a13966
0f8c152
21b3e6c
e69b27d
8bbf794
8433df6
9d4b50e
17be15b
237b14c
e369009
aa7f7a4
6a2be43
eb2fb59
1acd785
5a7ef7f
97b54b6
fd38b0a
3919e5c
c69e6fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * 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.effect.kernel.Sync | ||
|
|
||
| /** | ||
| * A simple console implementation of LoggerKernel for testing the SAM design. | ||
| */ | ||
| class ConsoleLoggerKernel[F[_], Ctx](implicit F: Sync[F]) extends LoggerKernel[F, Ctx] { | ||
|
|
||
| def log(level: KernelLogLevel, record: Log.Builder[Ctx] => Log.Builder[Ctx]): F[Unit] = { | ||
| F.delay { | ||
| val logRecord = record(Log.mutableBuilder[Ctx]()).build() | ||
|
|
||
| val timestamp = logRecord.timestamp.getOrElse(java.time.Instant.now()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use |
||
| val timeStr = timestamp.toString | ||
|
|
||
| val levelStr = logRecord.level.namePadded | ||
| val message = logRecord.message | ||
| val className = logRecord.className.map(c => s"[$c]").getOrElse("") | ||
| val fileName = | ||
| logRecord.fileName.map(f => s"($f:${logRecord.line.getOrElse(0)})").getOrElse("") | ||
|
|
||
| val contextStr = if (logRecord.context.nonEmpty) { | ||
| val contextPairs = logRecord.context.map { case (k, v) => s"$k=$v" }.mkString(", ") | ||
| s" {$contextPairs}" | ||
| } else "" | ||
|
|
||
| val throwableStr = logRecord.throwable.map(t => s"\n${t.toString}").getOrElse("") | ||
|
|
||
| val logLine = s"$timeStr $levelStr $className$fileName$contextStr $message$throwableStr" | ||
|
|
||
| println(logLine) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use |
||
| } | ||
| } | ||
| } | ||
|
|
||
| object ConsoleLoggerKernel { | ||
| def apply[F[_], Ctx](implicit F: Sync[F]): ConsoleLoggerKernel[F, Ctx] = | ||
| new ConsoleLoggerKernel[F, Ctx] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * 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 scala.concurrent.duration.FiniteDuration | ||
|
|
||
| import org.typelevel.log4cats.Context.Encoder | ||
|
|
||
| /** | ||
| * A value that can be written into a json-like construct, provided a visitor. | ||
| */ | ||
| trait Context[C] { | ||
| def capture[A](a: A)(implicit E: Encoder[A, C]): C | ||
| } | ||
|
|
||
| object Context { | ||
| trait Encoder[A, B] { | ||
| def encode(a: A): B | ||
| } | ||
|
|
||
| object Encoder { | ||
| def apply[A, B](implicit ev: Encoder[A, B]): ev.type = ev | ||
|
|
||
| // Identity encoder for when input and output types are the same | ||
| implicit def identityEncoder[A]: Encoder[A, A] = a => a | ||
|
|
||
| implicit val stringToStringEncoder: Encoder[String, String] = a => a | ||
|
|
||
| implicit val intToStringEncoder: Encoder[Int, String] = _.toString | ||
|
|
||
| implicit val longToStringEncoder: Encoder[Long, String] = _.toString | ||
|
|
||
| implicit val doubleToStringEncoder: Encoder[Double, String] = _.toString | ||
|
|
||
| implicit val booleanToStringEncoder: Encoder[Boolean, String] = if (_) "true" else "false" | ||
|
|
||
| // Removed Instant encoder for Scala Native compatibility | ||
| // implicit val instantToStringEncoder: Encoder[Instant, String] = | ||
| // DateTimeFormatter.ISO_INSTANT.format(_) | ||
|
|
||
| implicit val finiteDurationToStringEncoder: Encoder[FiniteDuration, String] = _.toString | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * 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.{Order, Show} | ||
| import cats.kernel.{Eq, Hash} | ||
|
|
||
| sealed abstract class KernelLogLevel(val name: String, val value: Int) { | ||
| def namePadded: String = name.padTo(5, ' ').mkString | ||
|
|
||
| override def toString: String = name | ||
| } | ||
|
|
||
| object KernelLogLevel { | ||
| implicit final val log4catsCatsInstances: Eq[KernelLogLevel] & Hash[KernelLogLevel] & Order[ | ||
| KernelLogLevel | ||
| ] & Show[KernelLogLevel] = | ||
| new Hash[KernelLogLevel] with Order[KernelLogLevel] with Show[KernelLogLevel] { | ||
| override def eqv(x: KernelLogLevel, y: KernelLogLevel): Boolean = x == y | ||
|
|
||
| override def hash(x: KernelLogLevel): Int = x.## | ||
|
|
||
| override def compare(x: KernelLogLevel, y: KernelLogLevel): Int = x.value.compare(y.value) | ||
|
|
||
| override def show(t: KernelLogLevel): String = t.name | ||
| } | ||
|
|
||
| case object Trace extends KernelLogLevel("TRACE", 100) | ||
| case object Debug extends KernelLogLevel("DEBUG", 200) | ||
| case object Info extends KernelLogLevel("INFO", 300) | ||
| case object Warn extends KernelLogLevel("WARN", 400) | ||
| case object Error extends KernelLogLevel("ERROR", 500) | ||
| case object Fatal extends KernelLogLevel("FATAL", 600) | ||
| } |
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.
Should the class be private? If so, we can relax the
applyresult type too: