Skip to content

Commit

Permalink
Feat: update log stracture Kotlin 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
mhd999 committed Aug 19, 2022
1 parent 185e0a0 commit 35048c2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,19 @@ val logger = Rootle("ac12Cd-Aevd-12Grx-235f4", "Billing-lambda")
logger.info("Hello World")
logger.warn("Hello World")
logger.error("Hello World", rootle.Downstream(rootle.Http("GET", StatusCode.InternalServerError.code, "http://localhost:8080/invoice/123", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36", "http://localhost:8080/"),
rootle.Grpc("GetInvoice", GrpcCodes.internalError.code, "invoice", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36", "http://localhost:8080/")), "billing/user", 0)
val logger = Rootle("ac12Cd-Aevd-12Grx-235f4", "billing")
val jsonObject = JsonObject()
jsonObject.addProperty("foo", "bar")
logger.error("Error message", jsonObject.toString(), rootle.Downstream(rootle.Http("GET", StatusCode.InternalServerError.code, "http://localhost:8080/invoice/123",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
"http://localhost:8080/", jsonObject.toString()),
rootle.Grpc("GetInvoice", GrpcCodes.internalError.code, "invoice",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
"http://localhost:8080/", jsonObject.toString())), "billing/user", 0);
```
## Output example
```
Expand Down
2 changes: 1 addition & 1 deletion kotlin/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = 0.1.0
version = 0.2.0
group = no.telia
12 changes: 7 additions & 5 deletions kotlin/lib/src/main/kotlin/rootle/Library.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ enum class StatusCode(val code: Int) {

class Rootle(private val id: String, private val application: String) {

inner class Http (val method: String, val statusCode: Int, val url: String, val useragent: String, val referer: String) {}
inner class Grpc (val procedure: String, val code: Int, val service: String, val useragent: String, val referer: String) {}
inner class Http (val method: String? = null, val statusCode: Int? = null, val url: String? = null, val useragent: String? = null, val referer: String? = null, val payload: String? = null) {}
inner class Grpc (val procedure: String? = null, val code: Int? = null, val service: String? = null, val useragent: String? = null, val referer: String? = null, val payload: String? = null) {}
inner class Downstream(val http: Http? = null, val grpc: Grpc? = null) {}

private inner class Log(private val id: String, private val application: String, private val timestamp: String,
private val message: String, private val level: String,
private val downstream: Downstream? = null, private val stacktrace: String? = null, val code: Int? = null) {
private val event: String? = null, private val downstream: Downstream? = null,
private val stacktrace: String? = null, val code: Int? = null) {
}
fun info(message: String) {
val log = Log(this.id, this.application, System.currentTimeMillis().toString(), message,
Expand All @@ -114,10 +115,11 @@ class Rootle(private val id: String, private val application: String) {
println(logJsonString)
}

fun error(message: String, downstream: Downstream, stackTrace: String, code: Int) {
fun error(message: String, event: String? = null, downstream: Downstream? = null, stackTrace: String? = null, code: Int? = null) {
val log = Log(this.id, this.application, System.currentTimeMillis().toString(), message,
"ERROR", downstream, stackTrace, code)
"ERROR", event, downstream, stackTrace, code)
val logJsonString = Gson().toJson(log)
println(logJsonString)
}
}

0 comments on commit 35048c2

Please sign in to comment.