Search code examples
javaandroidloggingandroid-studiopublic-method

Why do public methods require logging calls?


I made a public class AlertDialogManager as shown below,

public class AlertDialogManager {

/**
 * Function to display simple Alert Dialog
 * @param context - application context
 * @param title - alert dialog title
 * @param message - alert message
 * @param status - success/failure (used to set cancelable)
 * */

 public void showAlertDialog(Context context, String title, String message, Boolean status) {
     Log.i("DEBUG","Alert has been called");
     new AlertDialog.Builder(context)
             .setTitle(title)
             .setMessage(message)
             .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int which) {
                     Log.i(CommonUtilities.TAG,"ok has been clicked");
                 }
             })
             .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int which) {
                     Log.i(CommonUtilities.TAG,"Cancel has been clicked");
                 }
             })
             .setIcon(R.drawable.alert)
             .setCancelable(status)
             .show();
       }
}

So, I get this warning in android studio.

Image of the warning

As you can see the same error "'public' method 'showAlertDialog()' has no logging call Reports any public method which does not contain a logging statement. This inspection does not report simple getters and setters"

this repeats for the class AlertDialogManager, its method showAlertDialog, and for both onClick's.

By logging I thought they meant Log.i("TAG","Message") and added them accordingly.

So, I can't seem to understand this warning. So I appreciate any help in understanding it.


Solution

  • You have enabled some more lint checks - maybe all of them. Here is a screenshot how it looks like on my side: enter image description here

    So here I opened the Settings, and I simply typed logger inside the search box. You can see the selected lint check: Class without logger
    So this is inside Java -> Logging issues. In my case it is not enabled, and I am using default settings for lint checks (inspections). I assume this may be more helpful for Java applications, however I have not used such logger in pure Java, so I cannot tell for sure. Anyway I would suggest that you disable this check, unless it is mandatory for your team/company...

    The Log class that you use is part of Android SDK and I guess this could be a reason why it does not 'fix' the warning.