Skip to content

Comments

Raw template logging implementation#1157

Merged
daneshk merged 19 commits intoballerina-platform:masterfrom
randilt:raw-template-logging-implementation
Jan 17, 2025
Merged

Raw template logging implementation#1157
daneshk merged 19 commits intoballerina-platform:masterfrom
randilt:raw-template-logging-implementation

Conversation

@randilt
Copy link
Contributor

@randilt randilt commented Jan 14, 2025

Purpose

Resolves: #3331

Examples

public function main() {
    string myname = "Alex92";
    int myage = 25;
    string action1 = "action1";
    string action2 = "action2";
    string action3 = "action3";
    log:printError(`error: My name is ${myname} and my age is ${myage}`);
    log:printWarn(`warning: My name is ${myname} and my age is ${myage}`);
    log:printInfo(`info: My name is ${myname} and my age is ${myage}`);
    log:printDebug(`debug: My name is ${myname} and my age is ${myage}`);
    log:printInfo("User details", details = `name: ${myname}, age: ${myage}`, actions = `actions: ${action1}, ${action2}, ${action3}`);   
}

Checklist

  • Linked to an issue
  • Updated the changelog
  • Added tests
  • Updated the spec
  • Checked native-image compatibility

@randilt
Copy link
Contributor Author

randilt commented Jan 16, 2025

I added the suggested changes, but I have an issue, why is the build failing in the workflow? all the testcases pass when I run it locally @daneshk
2 testcases are failing in the workflow, I didn't change them or anything related to them.

@daneshk
Copy link
Member

daneshk commented Jan 16, 2025

I added the suggested changes, but I have an issue, why is the build failing in the workflow? all the testcases pass when I run it locally @daneshk

2 testcases are failing in the workflow, I didn't change them or anything related to them.

Let me check

@TharmiganK
Copy link
Contributor

I added the suggested changes, but I have an issue, why is the build failing in the workflow? all the testcases pass when I run it locally @daneshk 2 testcases are failing in the workflow, I didn't change them or anything related to them.

Only the GraalVM build workflows are failing and these workflows runs with the ballerina-lang master. Some changes done to the ballerina-lang master is causing these failures. We can disable the GraalVM checks since your changes does not affect the GraalVM compatibility.

@TharmiganK TharmiganK added the Skip GraalVM Check This will skip the GraalVM compatibility check label Jan 17, 2025
@randilt
Copy link
Contributor Author

randilt commented Jan 17, 2025

I added the suggested changes, but I have an issue, why is the build failing in the workflow? all the testcases pass when I run it locally @daneshk 2 testcases are failing in the workflow, I didn't change them or anything related to them.

Only the GraalVM build workflows are failing and these workflows runs with the ballerina-lang master. Some changes done to the ballerina-lang master is causing these failures. We can disable the GraalVM checks since your changes does not affect the GraalVM compatibility.

Thanks for the clarification :)

@randilt randilt requested review from TharmiganK and daneshk January 17, 2025 04:47
@TharmiganK
Copy link
Contributor

@randilt I would like to add some insights on the issue since the implementation in the io package is out-dated.

The objective is to add raw template support in the log functions. The raw template and string template are different.

  1. Raw template - https://ballerina.io/learn/by-example/raw-templates/
    `Hello, ${name}`
    
  2. String template or Backtick template - https://ballerina.io/learn/by-example/backtick-templates/
    string `Hello, ${name}`
    

In the log package we already support string templates. The following is working with the existing log package:

import ballerina/log;

public function main() {
   string myname = "Alex92";
   int myage = 25;
   log:printError(string `error: My name is ${myname} and my age is ${myage}`);
   log:printWarn(string `warning: My name is ${myname} and my age is ${myage}`);
   log:printInfo(string `info: My name is ${myname} and my age is ${myage}`);
   log:printDebug(string `debug: My name is ${myname} and my age is ${myage}`);
   log:printInfo("User details", details = string `name: ${myname}, age: ${myage}`);   
}

What we want from this new feature is to support the following:

import ballerina/log;

public function main() {
  string myname = "Alex92";
  int myage = 25;
  log:printError(`error: My name is ${myname} and my age is ${myage}`);
  log:printWarn(`warning: My name is ${myname} and my age is ${myage}`);
  log:printInfo(`info: My name is ${myname} and my age is ${myage}`);
  log:printDebug(`debug: My name is ${myname} and my age is ${myage}`);
  log:printInfo("User details", details = `name: ${myname}, age: ${myage}`);   
}

In order to do that you need to add a raw template type to the currently supported parameter type - Value:

public type Value anydata|Valuer|PrintableRawTemplate;

Please note that the PrintableRawTemplate should be a subtype of object:RawTemplate(Please refer to the Ballerina By Example)

public type PrintableRawTemplate object {
   *object:RawTemplate;
   // Since the `object:RawTemplate` already support `string[] & readonly` as the type for `strings`
   // we do not need to change it
   
   // The default `insertions` are with type `any|error[]` but we only need to support the values
   // We are currently supporting `Value`. You can also just support `anydata|Valuer` for now
   public Value[] insertions;
};

Now, we have allowed raw templates in the log function parameters, now we need to implement the string conversion. Now we do not need this class - PrintableRawTemplateImpl, we can directly check the type and implement a function to convert it to a string. (i.e. separate the toString function of this class as a separate function)

I hope you understand the issue now :)

@randilt
Copy link
Contributor Author

randilt commented Jan 17, 2025

@randilt I would like to add some insights on the issue since the implementation in the io package is out-dated.

The objective is to add raw template support in the log functions. The raw template and string template are different.

  1. Raw template - https://ballerina.io/learn/by-example/raw-templates/
    `Hello, ${name}`
    
  2. String template or Backtick template - https://ballerina.io/learn/by-example/backtick-templates/
    string `Hello, ${name}`
    

In the log package we already support string templates. The following is working with the existing log package:

import ballerina/log;

public function main() {
   string myname = "Alex92";
   int myage = 25;
   log:printError(string `error: My name is ${myname} and my age is ${myage}`);
   log:printWarn(string `warning: My name is ${myname} and my age is ${myage}`);
   log:printInfo(string `info: My name is ${myname} and my age is ${myage}`);
   log:printDebug(string `debug: My name is ${myname} and my age is ${myage}`);
   log:printInfo("User details", details = string `name: ${myname}, age: ${myage}`);   
}

What we want from this new feature is to support the following:

import ballerina/log;

public function main() {
  string myname = "Alex92";
  int myage = 25;
  log:printError(`error: My name is ${myname} and my age is ${myage}`);
  log:printWarn(`warning: My name is ${myname} and my age is ${myage}`);
  log:printInfo(`info: My name is ${myname} and my age is ${myage}`);
  log:printDebug(`debug: My name is ${myname} and my age is ${myage}`);
  log:printInfo("User details", details = `name: ${myname}, age: ${myage}`);   
}

In order to do that you need to add a raw template type to the currently supported parameter type - Value:

public type Value anydata|Valuer|PrintableRawTemplate;

Please note that the PrintableRawTemplate should be a subtype of object:RawTemplate(Please refer to the Ballerina By Example)

public type PrintableRawTemplate object {
   *object:RawTemplate;
   // Since the `object:RawTemplate` already support `string[] & readonly` as the type for `strings`
   // we do not need to change it
   
   // The default `insertions` are with type `any|error[]` but we only need to support the values
   // We are currently supporting `Value`. You can also just support `anydata|Valuer` for now
   public Value[] insertions;
};

Now, we have allowed raw templates in the log function parameters, now we need to implement the string conversion. Now we do not need this class - PrintableRawTemplateImpl, we can directly check the type and implement a function to convert it to a string. (i.e. separate the toString function of this class as a separate function)

I hope you understand the issue now :)

Wow, thanks a lot for the clarification, I just overcomplicated it :D, I will do the necessary changes and update the PR,
Thanks again!

@randilt
Copy link
Contributor Author

randilt commented Jan 17, 2025

Please note that the PrintableRawTemplate should be a subtype of object:RawTemplate(Please refer to the Ballerina By Example)

public type PrintableRawTemplate object {
   *object:RawTemplate;
   // Since the `object:RawTemplate` already support `string[] & readonly` as the type for `strings`
   // we do not need to change it
   
   // The default `insertions` are with type `any|error[]` but we only need to support the values
   // We are currently supporting `Value`. You can also just support `anydata|Valuer` for now
   public Value[] insertions;
};

If I do not redeclare the strings and insertions in here the testcases are failing, if I keep it as it is it will work as expected.

public type PrintableRawTemplate object {
    *object:RawTemplate;
    public string[] & readonly strings;
    public Value[] insertions;
};

So for now can I keep it as this?
@TharmiganK

@TharmiganK
Copy link
Contributor

If I do not redeclare the strings and insertions in here the testcases are failing, if I keep it as it is it will work as expected.

public type PrintableRawTemplate object {
    *object:RawTemplate;
    public string[] & readonly strings;
    public Value[] insertions;
};

So for now can I keep it as this?

Yes, sure, but ideally this should work:

public type PrintableRawTemplate object {
     *object:RawTemplate;
     public Value[] insertions;
};

Can you share the error you get?

@randilt
Copy link
Contributor Author

randilt commented Jan 17, 2025

It is a runtime error:

error: java.lang.ClassCastException: class java.lang.Long cannot be cast to class io.ballerina.runtime.api.values.BString (java.lang.Long is in module java.base of loader 'bootstrap'; io.ballerina.runtime.api.values.BString is in unnamed module of loader 'app')
        at ballerina.log.2:processTemplate(natives.bal:111)
           ballerina.log.2:processMessage(natives.bal:117)
           ballerina.log.2:print(natives.bal:211)
           ballerina.log.2:printError(natives.bal:147)
           main:main(main.bal:25)


        testErrorLevelRawTemplateLogfmt: has failed.

Only the 5 newly added testcases are failing (raw template testcases), but if I override strings field and insertions field both
it works without an issue
Not sure why this is happening, therefore I will keep it as it is for now
@TharmiganK

@TharmiganK
Copy link
Contributor

TharmiganK commented Jan 17, 2025

+1, it seems to be a lang issue. I will check on that. Meanwhile lets keep it as it is

Related lang issue: ballerina-platform/ballerina-lang#43501

@randilt
Copy link
Contributor Author

randilt commented Jan 17, 2025

I have made the changes you requested, could you please check? @TharmiganK

Co-authored-by: Krishnananthalingam Tharmigan <63336800+TharmiganK@users.noreply.github.com>
@sonarqubecloud
Copy link

Copy link
Contributor

@TharmiganK TharmiganK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
@randilt Thank you for your valuable contribution 🎉

@daneshk daneshk merged commit ead1182 into ballerina-platform:master Jan 17, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Skip GraalVM Check This will skip the GraalVM compatibility check

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support logging raw template value in log APIs

4 participants