logger icon indicating copy to clipboard operation
logger copied to clipboard

Add ability to not use a Global Tag

Open robl2e opened this issue 8 years ago • 2 comments

Hi, Is it possible to not use a Global Tag? I tried inserting an empty string or null like so:

FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
  .tag("")
  .build();

for the format strategy, but the final output appends a dash character with the local tag like so: "D/ -MyLocalTag"

Looking at the source I think this happens because of this method:

  private String formatTag(String tag) {
    if (!Utils.isEmpty(tag) && !Utils.equals(this.tag, tag)) {
      return this.tag + "-" + tag;
    }
    return this.tag;
  }

robl2e avatar Jun 22 '17 20:06 robl2e

I clone it and just change one line:

private String formatTag(String tag) {
    if (!Utils.isEmpty(tag) && !Utils.equals(this.tag, tag)) {
      //return this.tag + "-" + tag;
      return this.tag + tag;
    }
    return this.tag;
}

so that you can use

FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
  .tag("GlobalTag")
  .build();

I recommend not to use LocalTag, this will make the log show the code line number where you init Logger but not where it real used.

I hope that the author can modify the code as I do, even if the user wants to use -, the user can add their own rather than always have a -. like this:

FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
  .tag("GlobalTag-")
  .build();

likfe avatar Jul 26 '17 02:07 likfe

👍 I see the issue. Definitely this can be improved. For the time being, you can actually modify tag as you like by providing a custom LogStrategy

    FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
        .tag("")
        .logStrategy((priority, tag, message) -> {
          String newTag = tag.subSequence(1, tag.length()).toString();
          Log.println(priority, newTag, message);
        })
        .build();

    Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));

    Logger.t("LocalTag").d("test");

This will print out D/LocalTag:

orhanobut avatar Mar 27 '18 12:03 orhanobut