gs-log/modules/api/src/main/scala/gs/log/v0/Loggable.scala

35 lines
1.1 KiB
Scala

package gs.log.v0
import java.time.Instant
import java.time.LocalDate
/** Type class for data that can be logged by the `gs-log` library.
*/
trait Loggable[A]:
def renderForLogs(data: A): LogData.Value
object Loggable:
def apply[A](
using
L: Loggable[A]
): Loggable[A] = L
given Loggable[String] = LogData.Str(_)
given Loggable[Boolean] = LogData.Bool(_)
given Loggable[Int] = LogData.Int32(_)
given Loggable[Long] = LogData.Int64(_)
given Loggable[Float] = LogData.Float32(_)
given Loggable[Double] = LogData.Float64(_)
/** Default implementation for `java.time.LocalDate`. Uses the default
* `toString`, which formats the date as `uuuu-MM-dd` (ISO_LOCAL_DATE).
*/
def forDate(): Loggable[LocalDate] = data => LogData.Str(data.toString())
/** Default implementation for `java.time.Instant`. Uses the default
* `toString`, which formats the date using ISO_INSTANT. Produces strings
* that look like: `2011-12-03T10:15:30Z`
*/
def forInstant(): Loggable[Instant] = data => LogData.Str(data.toString())
end Loggable