google-java-format icon indicating copy to clipboard operation
google-java-format copied to clipboard

Inconsistent indentation of long log statements

Open joca-bt opened this issue 1 year ago • 3 comments

There seems to be an inconsistency in how long log statements are indented when they break across lines. See the example below. If there's a line break, I'd expect the output to be like the first snippet.

class Test {
  void test() {
    logger.atInfo().atInfo().atInfo().atInfo().atInfo().atInfo().atInfo().atInfo().atInfo().atInfo()
        .log(
            "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");

    logger.atInfo().atInfo().atInfo().atInfo().atInfo().atInfo().atInfo().atInfo().atInfo().atInfo()
        .atInfo().log(
        "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
  }
}

joca-bt avatar Sep 11 '24 11:09 joca-bt

Thanks for the report, I agree this is surprising.

There are some heuristics for formatting Flogger-style fluent log statements, and usually everything up to .log( fits on a single line.

There's a related internal bug b/112194879 where something similar happens with a long .withCause(...) call.

cushon avatar Sep 11 '24 21:09 cushon

I was trying to reproduce this with a regular dot with prefix but no luck so far, only with logs.

joca-bt avatar Sep 12 '24 09:09 joca-bt

I think that's expected, there's a very special case for formatting of log methods: https://github.com/google/google-java-format/blob/8a0e3b3d7cb2c4efeffe5610aa1146a85f0ffa10/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java#L1622

cushon avatar Sep 12 '24 15:09 cushon