Created
June 16, 2020 06:20
-
-
Save viggin543/ea4c8a4eea204bf7f03230880da496a8 to your computer and use it in GitHub Desktop.
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
| 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