Created
June 23, 2021 11:19
-
-
Save daneshk/509be7abad3fdec19e9d98297ebfa7c4 to your computer and use it in GitHub Desktop.
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
import ballerina/io; | |
import ballerina/jballerina.java; | |
public function main() { | |
LogRecord logRecord = { | |
time: "2021-05-04T10:32:13.220+05:30", | |
level: "DEBUG", | |
module: "foo/bar", | |
message: "debug \n message", | |
"username": "Alex", | |
"id": 845315 | |
}; | |
string logMessage = printLogFmtExtern(logRecord); | |
io:println(logMessage); | |
} | |
type LogRecord record { | |
string time; | |
string level; | |
string module; | |
string message; | |
}; | |
isolated function printLogFmtExtern(LogRecord logRecord) returns string { | |
string message = ""; | |
foreach [string, anydata] [k, v] in logRecord.entries() { | |
string value; | |
match k { | |
"time"|"level" => { | |
value = v.toString(); | |
} | |
"module" => { | |
value = v.toString(); | |
if value == "" { | |
value = string `\"${value}\"`; | |
} | |
} | |
_ => { | |
if v is string { | |
value = string `${escape(v.toString())}`; | |
} else { | |
value = v.toString(); | |
} | |
} | |
} | |
if message == "" { | |
message = message + string `${k} = ${value}`; | |
} else { | |
message = message + string ` ${k} = ${value}`; | |
} | |
} | |
return message; | |
} | |
isolated function escape(string msg) returns string { | |
// return regex:replaceAll(msg, "\n", "\\n"); | |
handle temp = escapseExternal(java:fromString(msg), java:fromString("\n"), java:fromString("\\n")); | |
io:println(temp); | |
string? updatedString = java:toString(temp); | |
if (updatedString is string) { | |
return updatedString.toBalString(); | |
} else { | |
// Should never reach here. | |
error e = error(string `error occurred while replacing`); | |
panic e; | |
} | |
} | |
// public isolated function replaceAll(string originalString, string regex, string replacement) returns string { | |
// handle value = replaceAllExternal(java:fromString(originalString), java:fromString(regex), | |
// java:fromString(replacement)); | |
// io:println(value); | |
// string? updatedString = java:toString(value); | |
// if (updatedString is string) { | |
// return updatedString; | |
// } else { | |
// // Should never reach here. | |
// error e = error(string `error occurred while replacing ${regex} in ${originalString}`); | |
// panic e; | |
// } | |
// } | |
// isolated function replaceAllExternal(handle originalString, handle regex, handle replacement) returns handle = @java:Method { | |
// name: "replaceAll", | |
// 'class: "java.lang.String", | |
// paramTypes: ["java.lang.String", "java.lang.String"] | |
// } external; | |
public isolated function escapseExternal(handle receiver, handle target, handle replacement) returns handle = @java:Method { | |
'class: "java.lang.String", | |
name: "replace" | |
} external; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment