Last active
February 22, 2018 05:15
-
-
Save e8kor/2c75b8c219d792262d0cf51e050b344f to your computer and use it in GitHub Desktop.
Structured logging using akka, slf4j, Logstash Logback Json Encoder
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
// Snippet | |
import akka.event.LogMarker | |
import akka.event.slf4j.Slf4jLogMarker | |
import net.logstash.logback.marker.RawJsonAppendingMarker | |
import io.circe._ | |
import io.circe.generic.extras._ | |
import io.circe.syntax._ | |
implicit private val config: Configuration = Configuration.default.withSnakeCaseMemberNames // ELK has good support for snake case json convention | |
implicit def asMarker[T: Encoder](pair: (String, T)): Marker = { | |
val (field, entity) = pair | |
val json = entity.asJson.pretty(Printer.noSpaces) | |
new RawJsonAppendingMarker(field, json) | |
} | |
type Marker = org.slf4j.Marker | |
def m(head: Marker, tail: Marker*): LogMarker = { | |
tail.foreach(v => head.add(v)) | |
new Slf4jLogMarker(head) | |
} | |
// Example of usage | |
import org.joda.time.DateTime | |
case class ExampleModel(playerId: String, date: DateTime) | |
val system: ActorSystem = ActorSystem() | |
val logger: MarkerLoggingAdapter = Logging.withMarker(system, "application") | |
val model = ExampleModel("some-player-id-1", DateTime.now()) | |
val model2 = ExampleModel("some-player-id-2", DateTime.now()) | |
logger.info(m("player_1" -> model2, "player_2" -> model), "some random data in your pile") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great, thanks! Found out your gist from google search. Are you using https://github.com/logstash/logstash-logback-encoder for logging backend?