ktlint icon indicating copy to clipboard operation
ktlint copied to clipboard

Logging output is sent to stdout instead of stderr when using --stdin

Open benkay opened this issue 3 years ago • 2 comments

When using ktlint with --stdin, it's expected that the output of stdout is only the formatted code. Instead, an info logging line is prepended to the stdout with 0.47.1. The pre-commit hook we use (based on https://github.com/hallettj/git-format-staged) relies on the --stdin functionality, and using the latest version the committed files end up with an extra line in the file

Expected Behavior

When called with -F --stdin, only the formatted code should be sent to stdout. Additonal output may be in stderr

Observed Behavior

An INFO level logging line is always output as the first line of stdout

Steps to Reproduce

> echo "class Foo {
fun foo() {}
}" | ./ktlint-0.47.1 -F --stdin 2>/dev/null

Outputs

10:02:35.047 [main] INFO com.pinterest.ktlint.internal.KtlintCommandLine - Enable default patterns [**/*.kt, **/*.kts]
class Foo {
    fun foo() {}
}

benkay avatar Sep 21 '22 09:09 benkay

I expect that sending all log output to stderr will result in other issues for other use cases. In your example it would make sesnse as you ignore all output from stderr as a result of 2>/dev/null. Maybe you can for now exclude the log line by changing your command to:

$ echo "class Foo {
fun foo() {}
}" | ktlint-0.47.1 -F --stdin 2>/dev/null | grep -v "KtlintCommandLine - Enable default patterns"
class Foo {
    fun foo() {}
}

When issue #1632 is implemented in version 0.48.x you can set the log level to something like "fatal" or "none" which effectively would mean that nothing will be logged.

paul-dingemans avatar Sep 22 '22 16:09 paul-dingemans

References in documentation to stderr should be fixed as since version 0.44 ktlint no longer writes explicitly to stdout and stderr.

paul-dingemans avatar Sep 22 '22 18:09 paul-dingemans

In ktlint 0.48.0 it seems --log-level is not respected:

$ echo "class Foo {
fun foo() {}
}" | ktlint --log-level=fatal -F --stdin 2>/dev/null
08:48:35.907 [main] INFO com.pinterest.ktlint.internal.KtlintCommandLine - Enable default patterns [**/*.kt, **/*.kts]
class Foo {
    fun foo() {}
}

As a workaround I'm using a similar grep trick to the above, but if would be nice if we could suppress this and/or log to stderr.

alex-shinn avatar Dec 26 '22 08:12 alex-shinn