generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from orangain/refactor-extract
Refactor extractions
- Loading branch information
Showing
10 changed files
with
180 additions
and
140 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true |
122 changes: 0 additions & 122 deletions
122
src/main/kotlin/io/github/orangain/prettyjsonlog/Extract.kt
This file was deleted.
Oops, something went wrong.
This file contains 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
52 changes: 52 additions & 0 deletions
52
src/main/kotlin/io/github/orangain/prettyjsonlog/logentry/Level.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.github.orangain.prettyjsonlog.logentry | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
|
||
enum class Level { | ||
TRACE, DEBUG, INFO, WARN, ERROR, FATAL; | ||
|
||
companion object { | ||
fun fromInt(level: Int): Level { | ||
// Use bunyan's level as a reference. | ||
// See: https://github.com/trentm/node-bunyan?tab=readme-ov-file#levels | ||
return when { | ||
level < 20 -> TRACE | ||
level < 30 -> DEBUG | ||
level < 40 -> INFO | ||
level < 50 -> WARN | ||
level < 60 -> ERROR | ||
else -> FATAL | ||
} | ||
} | ||
|
||
fun fromString(level: String): Level? { | ||
// Bunyan's levels: TRACE, DEBUG, INFO, WARN, ERROR, FATAL | ||
// https://github.com/trentm/node-bunyan?tab=readme-ov-file#levels | ||
// Cloud Logging's levels: DEFAULT, DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL, ALERT, EMERGENCY | ||
// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity | ||
// java.util.logging's levels: FINEST, FINER, FINE, CONFIG, INFO, WARNING, SEVERE | ||
// https://docs.oracle.com/en/java/javase/21/docs/api/java.logging/java/util/logging/Level.html | ||
return when (level.uppercase()) { | ||
"TRACE", "FINEST", "FINER", "FINE" -> TRACE | ||
"DEBUG", "CONFIG" -> DEBUG | ||
"INFO", "NOTICE" -> INFO | ||
"WARN", "WARNING" -> WARN | ||
"ERROR", "CRITICAL", "SEVERE" -> ERROR | ||
"FATAL", "ALERT", "EMERGENCY" -> FATAL | ||
else -> null // This includes "DEFAULT" | ||
} | ||
} | ||
} | ||
} | ||
|
||
private val levelKeys = listOf("level", "severity", "log.level") | ||
|
||
fun extractLevel(node: JsonNode): Level? { | ||
return levelKeys.firstNotNullOfOrNull { node.get(it) }?.let { levelNode -> | ||
if (levelNode.isNumber) { | ||
Level.fromInt(levelNode.asInt()) | ||
} else { | ||
Level.fromString(levelNode.asText()) | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/io/github/orangain/prettyjsonlog/logentry/Message.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.github.orangain.prettyjsonlog.logentry | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
|
||
private val messageKeys = listOf("message", "msg", "error.message") | ||
|
||
fun extractMessage(node: JsonNode): String? { | ||
return messageKeys.firstNotNullOfOrNull { node.get(it) }?.asText() | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/io/github/orangain/prettyjsonlog/logentry/NodeExtractor.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.github.orangain.prettyjsonlog.logentry | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
|
||
typealias NodeExtractor = (JsonNode) -> JsonNode? |
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/io/github/orangain/prettyjsonlog/logentry/StackTrace.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.github.orangain.prettyjsonlog.logentry | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
|
||
private val stackTraceNodeExtractors: List<NodeExtractor> = listOf( | ||
{ it.get("stack_trace") }, | ||
{ it.get("exception") }, | ||
{ it.get("error.stack_trace") }, | ||
{ it.get("err")?.get("stack") }, | ||
) | ||
|
||
fun extractStackTrace(node: JsonNode): String? { | ||
return stackTraceNodeExtractors.firstNotNullOfOrNull { it(node) }?.asText() | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/kotlin/io/github/orangain/prettyjsonlog/logentry/Timestamp.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.github.orangain.prettyjsonlog.logentry | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import java.time.Instant | ||
import java.time.OffsetDateTime | ||
import java.time.ZoneId | ||
import java.time.format.DateTimeFormatter | ||
import java.time.format.DateTimeParseException | ||
|
||
sealed interface Timestamp { | ||
fun format(zoneId: ZoneId, formatter: DateTimeFormatter): String | ||
|
||
data class Parsed(val value: Instant) : Timestamp { | ||
override fun format(zoneId: ZoneId, formatter: DateTimeFormatter): String { | ||
return value.atZone(zoneId).format(formatter) | ||
} | ||
} | ||
|
||
data class Fallback(val value: String) : Timestamp { | ||
override fun format(zoneId: ZoneId, formatter: DateTimeFormatter): String { | ||
return value | ||
} | ||
} | ||
|
||
companion object { | ||
fun fromEpochMilli(value: Long): Parsed { | ||
return Parsed(Instant.ofEpochMilli(value)) | ||
} | ||
|
||
fun fromString(value: String): Timestamp { | ||
return try { | ||
// Use OffsetDateTime.parse instead of Instant.parse because Instant.parse in JDK <= 11 does not support non-UTC offset like "-05:00". | ||
// See: https://stackoverflow.com/questions/68217689/how-to-use-instant-java-class-to-parse-a-date-time-with-offset-from-utc/68221614#68221614 | ||
Parsed(OffsetDateTime.parse(value).toInstant()) | ||
} catch (e: DateTimeParseException) { | ||
Fallback(value) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private val timestampKeys = listOf("timestamp", "time", "@timestamp") | ||
|
||
fun extractTimestamp(node: JsonNode): Timestamp? { | ||
|
||
return timestampKeys.firstNotNullOfOrNull { node.get(it) }?.let { timestampNode -> | ||
if (timestampNode.isNumber) { | ||
// We assume that the number is a Unix timestamp in milliseconds. | ||
Timestamp.fromEpochMilli(timestampNode.asLong()) | ||
} else { | ||
Timestamp.fromString(timestampNode.asText()) | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/test/kotlin/io/github/orangain/prettyjsonlog/json/ParseTest.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.github.orangain.prettyjsonlog.json | ||
|
||
import junit.framework.TestCase | ||
|
||
class ParseTest : TestCase() { | ||
fun testParseJsonLine() { | ||
val result = parseJson("""{"key": "value"}""") | ||
assertNotNull(result) | ||
val (node, rest) = result!! | ||
assertEquals("""{"key":"value"}""", node.toString()) | ||
assertEquals("", rest) | ||
} | ||
|
||
fun testParseJsonLineWithSpaces() { | ||
val result = parseJson(""" {"key": "value"} """) | ||
assertNotNull(result) | ||
val (node, rest) = result!! | ||
assertEquals("""{"key":"value"}""", node.toString()) | ||
assertEquals(" ", rest) | ||
} | ||
|
||
fun testParseBrokenJsonLine() { | ||
val result = parseJson("""{"key": "value" """) | ||
assertNull(result) | ||
} | ||
|
||
fun testParseEmptyString() { | ||
val result = parseJson("") | ||
assertNull(result) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...hub/orangain/prettyjsonlog/ExtractTest.kt → ...ain/prettyjsonlog/logentry/ExtractTest.kt
This file contains 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