Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
benkay opened this issue Sep 21, 2022 · 3 comments · Fixed by #1658
Closed

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

benkay opened this issue Sep 21, 2022 · 3 comments · Fixed by #1658

Comments

@benkay
Copy link

benkay commented Sep 21, 2022

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() {}
}
@paul-dingemans
Copy link
Collaborator

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
Copy link
Collaborator

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

paul-dingemans added a commit to paul-dingemans/ktlint that referenced this issue Sep 23, 2022
…with `--log-level=<level>` or the short version `-l=<level>.

Closes pinterest#1632

* Allow disabling logging in CLI by setting `--log-level=none` or `-l=none`.

Closes pinterest#1652
paul-dingemans added a commit that referenced this issue Oct 12, 2022
…with `--log-level=<level>` or the short version `-l=<level>. (#1658)

Closes #1652
Closes #1632
@alex-shinn
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants