Create a custom java rule plugin which can read a yml file or a customized analyze parameter

I am writing a custom java rule: “The logger with lower level than expected must be judged if it is enabled”. However, since the expected logger level is defined in a yml file, not in the java class itself, I need to obtain the expected logger level first before scanning and parsing java files.

I come up with 2 ideas:

  1. read and parse the yml file in the plugin to find the expected level, in sonar way or using other packages.
  2. use a customized analyze parameter to tell the plugin which level is expected.

I wonder if it is possible to implement these ideas. If not, how can I write this rule?

Thanks.

Hey there.

To me, this sounds best like a rule parameter that you configure in the Quality Profile when activating a rule.

Okay, I read the document, but it is too brief…

I guess if I add a RuleProperty annotation to a variable, then I can change its value via the settings of frontend website? Or if I specify the “key”, I can access it using analyze parameter like -Dsonar.mykey=something?

Yes! For example, here is a parameterized rule in the Web UI:

That default value of 60 can be changed when activating it in a Quality Profile.

Thank you for your help. I will try it later :slight_smile: