1414// specific language governing permissions and limitations
1515// under the License.
1616
17- import ballerina /jballerina .java ;
1817import ballerina /io ;
18+ import ballerina /jballerina .java ;
1919import ballerina /test ;
2020
2121const string UTF_8 = " UTF-8" ;
@@ -25,7 +25,9 @@ const string PRINT_INFO_FILE = "tests/resources/samples/print-functions/info.bal
2525const string PRINT_WARN_FILE = " tests/resources/samples/print-functions/warn.bal" ;
2626const string PRINT_DEBUG_FILE = " tests/resources/samples/print-functions/debug.bal" ;
2727const string PRINT_ERROR_FILE = " tests/resources/samples/print-functions/error.bal" ;
28+ const string PRINT_RAW_TEMPLATE_FILE = " tests/resources/samples/print-functions/raw_template.bal" ;
2829const string LOG_LEVEL_FILE = " tests/resources/samples/log-levels/main.bal" ;
30+ const string LOG_LEVEL_RAW_TEMPLATE_FILE = " tests/resources/samples/log-levels-raw-template/main.bal" ;
2931
3032const string FILE_WRITE_OUTPUT_OVERWRITE_INPUT_FILE_LOGFMT = " tests/resources/samples/file-write-output/single-file/overwrite-logfmt.bal" ;
3133const string FILE_WRITE_OUTPUT_APPEND_INPUT_FILE_LOGFMT = " tests/resources/samples/file-write-output/single-file/append-logfmt.bal" ;
@@ -51,6 +53,12 @@ const string MESSAGE_WARN_LOGFMT = " level=WARN module=\"\" message=\"warn log\"
5153const string MESSAGE_INFO_LOGFMT = " level=INFO module=\"\" message=\" info log\" " ;
5254const string MESSAGE_DEBUG_LOGFMT = " level=DEBUG module=\"\" message=\" debug log\" " ;
5355
56+ const string MESSAGE_ERROR_RAW_TEMPLATE_LOGFMT = " level=ERROR module=\"\" message=\" error: My name is Alex92 and my age is 25\" " ;
57+ const string MESSAGE_WARN_RAW_TEMPLATE_LOGFMT = " level=WARN module=\"\" message=\" warning: My name is Alex92 and my age is 25\" " ;
58+ const string MESSAGE_INFO_RAW_TEMPLATE_LOGFMT = " level=INFO module=\"\" message=\" info: My name is Alex92 and my age is 25\" " ;
59+ const string MESSAGE_DEBUG_RAW_TEMPLATE_LOGFMT = " level=DEBUG module=\"\" message=\" debug: My name is Alex92 and my age is 25\" " ;
60+ const string MESSAGE_KEY_VALUE_PAIR_LOGFMT = " level=INFO module=\"\" message=\" User details\" details=\" name: Alex92, age: 25\" actions=\" actions: action1, action2, action3\" " ;
61+
5462const string MESSAGE_ERROR_MAIN_LOGFMT = " level=ERROR module=myorg/myproject message=\" error log\\ t\\ n\\ r\\\\\\\"\" " ;
5563const string MESSAGE_WARN_MAIN_LOGFMT = " level=WARN module=myorg/myproject message=\" warn log\\ t\\ n\\ r\\\\\\\"\" " ;
5664const string MESSAGE_INFO_MAIN_LOGFMT = " level=INFO module=myorg/myproject message=\" info log\\ t\\ n\\ r\\\\\\\"\" " ;
@@ -215,6 +223,86 @@ public function testDebugLevelLogfmt() returns error? {
215223 validateLog (logLines [8 ], MESSAGE_DEBUG_LOGFMT );
216224}
217225
226+ @test :Config {}
227+ public function testErrorLevelRawTemplateLogfmt() returns error ? {
228+ Process | error execResult = exec (bal_exec_path , {BAL_CONFIG_FILES : CONFIG_ERROR_LOGFMT }, (), " run" , LOG_LEVEL_RAW_TEMPLATE_FILE );
229+ Process result = check execResult ;
230+ int _ = check result .waitForExit ();
231+ int _ = check result .exitCode ();
232+ io : ReadableByteChannel readableResult = result .stderr ();
233+ io : ReadableCharacterChannel sc = new (readableResult , UTF_8 );
234+ string outText = check sc .read (100000 );
235+ string [] logLines = re ` \n ` .split (outText .trim ());
236+ test : assertEquals (logLines .length (), 6 , INCORRECT_NUMBER_OF_LINES );
237+ validateLog (logLines [5 ], MESSAGE_ERROR_RAW_TEMPLATE_LOGFMT );
238+ }
239+
240+ @test :Config {}
241+ public function testWarnLevelRawTemplateLogfmt() returns error ? {
242+ Process | error execResult = exec (bal_exec_path , {BAL_CONFIG_FILES : CONFIG_WARN_LOGFMT }, (), " run" , LOG_LEVEL_RAW_TEMPLATE_FILE );
243+ Process result = check execResult ;
244+ int _ = check result .waitForExit ();
245+ int _ = check result .exitCode ();
246+ io : ReadableByteChannel readableResult = result .stderr ();
247+ io : ReadableCharacterChannel sc = new (readableResult , UTF_8 );
248+ string outText = check sc .read (100000 );
249+ string [] logLines = re ` \n ` .split (outText .trim ());
250+ test : assertEquals (logLines .length (), 7 , INCORRECT_NUMBER_OF_LINES );
251+ validateLog (logLines [5 ], MESSAGE_ERROR_RAW_TEMPLATE_LOGFMT );
252+ validateLog (logLines [6 ], MESSAGE_WARN_RAW_TEMPLATE_LOGFMT );
253+ }
254+
255+ @test :Config {}
256+ public function testInfoLevelRawTemplateLogfmt() returns error ? {
257+ Process | error execResult = exec (bal_exec_path , {BAL_CONFIG_FILES : CONFIG_INFO_LOGFMT }, (), " run" , LOG_LEVEL_RAW_TEMPLATE_FILE );
258+ Process result = check execResult ;
259+ int _ = check result .waitForExit ();
260+ int _ = check result .exitCode ();
261+ io : ReadableByteChannel readableResult = result .stderr ();
262+ io : ReadableCharacterChannel sc = new (readableResult , UTF_8 );
263+ string outText = check sc .read (100000 );
264+ string [] logLines = re ` \n ` .split (outText .trim ());
265+ test : assertEquals (logLines .length (), 9 , INCORRECT_NUMBER_OF_LINES );
266+ validateLog (logLines [5 ], MESSAGE_ERROR_RAW_TEMPLATE_LOGFMT );
267+ validateLog (logLines [6 ], MESSAGE_WARN_RAW_TEMPLATE_LOGFMT );
268+ validateLog (logLines [7 ], MESSAGE_INFO_RAW_TEMPLATE_LOGFMT );
269+ }
270+
271+ @test :Config {}
272+ public function testDebugLevelRawTemplateLogfmt() returns error ? {
273+ Process | error execResult = exec (bal_exec_path , {BAL_CONFIG_FILES : CONFIG_DEBUG_LOGFMT }, (), " run" , LOG_LEVEL_RAW_TEMPLATE_FILE );
274+ Process result = check execResult ;
275+ int _ = check result .waitForExit ();
276+ int _ = check result .exitCode ();
277+ io : ReadableByteChannel readableResult = result .stderr ();
278+ io : ReadableCharacterChannel sc = new (readableResult , UTF_8 );
279+ string outText = check sc .read (100000 );
280+ string [] logLines = re ` \n ` .split (outText .trim ());
281+ test : assertEquals (logLines .length (), 10 , INCORRECT_NUMBER_OF_LINES );
282+ validateLog (logLines [5 ], MESSAGE_ERROR_RAW_TEMPLATE_LOGFMT );
283+ validateLog (logLines [6 ], MESSAGE_WARN_RAW_TEMPLATE_LOGFMT );
284+ validateLog (logLines [7 ], MESSAGE_INFO_RAW_TEMPLATE_LOGFMT );
285+ validateLog (logLines [8 ], MESSAGE_DEBUG_RAW_TEMPLATE_LOGFMT );
286+ }
287+ @test :Config {}
288+ public function testRawTemplateKeyValuePair() returns error ? {
289+ Process | error execResult = exec (bal_exec_path , {BAL_CONFIG_FILES : CONFIG_DEBUG_LOGFMT }, (), " run" , LOG_LEVEL_RAW_TEMPLATE_FILE );
290+ Process result = check execResult ;
291+ int _ = check result .waitForExit ();
292+ int _ = check result .exitCode ();
293+ io : ReadableByteChannel readableResult = result .stderr ();
294+ io : ReadableCharacterChannel sc = new (readableResult , UTF_8 );
295+ string outText = check sc .read (100000 );
296+ string [] logLines = re ` \n ` .split (outText .trim ());
297+ test : assertEquals (logLines .length (), 10 , INCORRECT_NUMBER_OF_LINES );
298+ validateLog (logLines [5 ], MESSAGE_ERROR_RAW_TEMPLATE_LOGFMT );
299+ validateLog (logLines [6 ], MESSAGE_WARN_RAW_TEMPLATE_LOGFMT );
300+ validateLog (logLines [7 ], MESSAGE_INFO_RAW_TEMPLATE_LOGFMT );
301+ validateLog (logLines [8 ], MESSAGE_DEBUG_RAW_TEMPLATE_LOGFMT );
302+ validateLog (logLines [9 ], MESSAGE_KEY_VALUE_PAIR_LOGFMT );
303+ }
304+
305+
218306@test :Config {}
219307public function testProjectWithoutLogLevelLogfmt() returns error ? {
220308 Process | error execResult = exec (bal_exec_path , {}, (), " run" , temp_dir_path
@@ -475,3 +563,4 @@ function exec(@untainted string command, @untainted map<string> env = {},
475563 name : " exec" ,
476564 'class : " io.ballerina.stdlib.log.testutils.nativeimpl.Exec"
477565} external ;
566+
0 commit comments