Skip to content

Instantly share code, notes, and snippets.

@viggin543
Created June 16, 2020 06:20
Show Gist options
  • Save viggin543/ea4c8a4eea204bf7f03230880da496a8 to your computer and use it in GitHub Desktop.
Save viggin543/ea4c8a4eea204bf7f03230880da496a8 to your computer and use it in GitHub Desktop.
package com.company.core.logging
import org.apache.logging.log4j.core.LogEvent
import org.apache.logging.log4j.core.config.plugins.Plugin
import org.apache.logging.log4j.core.lookup.StrLookup
import java.time.Instant
@Plugin(name = "uptime", category = StrLookup.CATEGORY)
class UptimeLoockup : StrLookup {
companion object {
var startTime: Instant = Instant.now()
}
override fun lookup(key: String?): String {
return upTime(key ?: "seconds")
}
override fun lookup(event: LogEvent?, key: String?): String {
return upTime(key ?: "seconds")
}
private fun upTime(key: String): String {
return (if (key == "seconds")
startTime.epochSecond.let { (Instant.now().epochSecond - it).toString() }
else startTime.toEpochMilli().let { (Instant.now().toEpochMilli() - it).toString() })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment