I'm running into an issue where logback using `ch....
# serialization
a
I'm running into an issue where logback using
ch.qos.logback.classic.encoder.JsonEncoder
doesn't want to serialize
kotlinx.datetime.LocalDateTime
It seems that the underlying encoder uses Jackson and I'm wondering if I have to pass something special to the encoder to make it work? Can't seem to find any info online about that and wondering if someone has run across this before. Same issue using the joda time equivalents
k
I just ran into this. You have to create a custom jackson encoder to use that. I ended up switching everything to using kotlinx-serialization instead so it would use kotlinx-datetime’s included serializers
a
how do you switch the logger to use
kotlinx-serialization
thats built into logback encoder isn't it?
k
whoops. I ran into that problem with spring boot and missed that you mentioned logback. I haven’t even looked at logback’s date management. Sorry, can’t help you much there. I’m guessing it’ll be the same thing though, you’ll need to create your own encoder.
a
wowzers thanks
m
Or maybe not use Logback? In Spring Boot you can switch in Log4j2, which is arguably superior. Or you can use Klogging with Spring Boot. (I am the author of Klogging.)
a
thanks Michael, I'ma actually running a Ktor server and I've been using https://github.com/oshai/kotlin-logging which I think I picked up from my previous project a few years ago but I'm not certain. I'll give your lib & log4j2 a gander though.
m
Klogging can be used easily with Ktor as well. I checked: Klogging serialises Kotlin LocalDateTime values without problem. Let me know how you go or if you have any questions.
a
but for outputting JSON won't it still rely on the internal logging encoder?
m
Klogging uses its own, simple JSON serialisation for log output. I started with that and haven’t need to change it so far. For
LocalDateTime
it calls
toString()
that renders in ISO8601 format. (It uses KotlinX Serialization to parse JSON configuration files.)
a
roger