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

Send log messages to log4j systems instead of system out / error #3231

Merged

Conversation

DarshitChanpura
Copy link
Member

@DarshitChanpura DarshitChanpura commented Aug 22, 2023

Description

A lot of files (mostly tests) have sysout's which do nothing but degrade the performance. This PR removes all sysouts from tests, replace sysouts with logger in project files.
Also removes all e.printStacktrace(); calls.

  • tools/ package - no sysouts were removed from here
  • HTTPSpnegoAuthenticator.java - a portion was not modified since it enabled debugging on console.

Check List

  • New functionality includes testing
  • New functionality has been documented
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@DarshitChanpura DarshitChanpura changed the title Cleans up sysouts and replaces with logger statements where necessary Clean up sysouts and replace with logger statements where necessary Aug 22, 2023
@codecov
Copy link

codecov bot commented Aug 22, 2023

Codecov Report

Merging #3231 (17d83a1) into main (75f529a) will increase coverage by 0.03%.
The diff coverage is 50.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #3231      +/-   ##
============================================
+ Coverage     62.43%   62.47%   +0.03%     
+ Complexity     3352     3350       -2     
============================================
  Files           254      254              
  Lines         19748    19737      -11     
  Branches       3334     3334              
============================================
+ Hits          12330    12331       +1     
+ Misses         5789     5777      -12     
  Partials       1629     1629              
Files Changed Coverage Δ
...ic/auth/http/kerberos/HTTPSpnegoAuthenticator.java 0.00% <0.00%> (ø)
...ch/security/securityconf/DynamicConfigFactory.java 54.76% <ø> (+0.64%) ⬆️
...arch/security/ssl/OpenSearchSecuritySSLPlugin.java 85.11% <ø> (+1.55%) ⬆️
...opensearch/security/ssl/util/SSLRequestHelper.java 67.85% <0.00%> (+0.60%) ⬆️
...opensearch/security/tools/AuditConfigMigrater.java 0.00% <ø> (ø)
...n/java/org/opensearch/security/tools/Migrater.java 0.00% <ø> (ø)
...a/org/opensearch/security/tools/SecurityAdmin.java 44.50% <ø> (+0.09%) ⬆️
...g/opensearch/security/auditlog/sink/DebugSink.java 100.00% <100.00%> (ø)

@DarshitChanpura
Copy link
Member Author

Taking a look at the test failure

Copy link
Member

@peternied peternied left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice - thanks for keeping the console output cleaner. Can you see about adding an rule to prevent system.out/system.err usage to keep the codebase programmatically cleaned?

Note; I think adding exceptions is fine for a couple places we do want console output

@DarshitChanpura
Copy link
Member Author

Nice - thanks for keeping the console output cleaner. Can you see about adding an rule to prevent system.out/system.err usage to keep the codebase programmatically cleaned?

Note; I think adding exceptions is fine for a couple places we do want console output

I was contemplating between adding such check and not adding it, but your comment helped decide :D. I have added a github check.

@@ -83,3 +83,17 @@ jobs:
fi
done
exit $exit_code

check-println:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a new check, what about leveraging checkstyle's RegexpSingleline, below was how the 'extension' filter was added, maybe we could add entries for system.out.print & system.err.print that would be easier to maintain

  <module name="RegexpSingleline">
    <property name="format" value="extension"/>
    <property name="ignoreCase" value="true"/>
    <property name="message" value="Extension should only be used sparingly to keep implementations as generic as possible" />
    <property name="severity" value="error"/>
  </module>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that but didn't have a working solution, I'll invest some more time to see if I can make it work

Copy link
Member Author

@DarshitChanpura DarshitChanpura Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not able to have it working with:

  <module name="RegexpSingleline">
    <property name="format" value="System\.out\.println" />
    <property name="message" value="Do not use System.out.println" />
    <!-- Exception list of files and directories -->
    <property name="fileExtensions" value="java" />
    <property name="exclude" value="./src/main/java/org/opensearch/security/tools, ./src/main/java/com/amazon/dlic/auth/http/kerberos/HTTPSpnegoAuthenticator.java" />
  </module>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try building off of this, works locally for me

  <module name="RegexpSingleline">
    <property name="format" value="println"/>
    <property name="ignoreCase" value="true"/>
    <property name="message" value="SYSTEM.OUT.PRINTLN NONONO!" />
    <property name="severity" value="error"/>
  </module>
./gradlew checkstyleMain 
...
> Task :checkstyleMain
[ant:checkstyle] [ERROR] /local/home/petern/git/security/src/main/java/org/opensearch/security/securityconf/DynamicConfigFactory.java:247: SYSTEM.OUT.PRINTLN NONONO! [RegexpSingleline]
[ant:checkstyle] [ERROR] /local/home/petern/git/security/src/main/java/org/opensearch/security/securityconf/DynamicConfigFactory.java:248: SYSTEM.OUT.PRINTLN NONONO! [RegexpSingleline]
[ant:checkstyle] [ERROR] /local/home/petern/git/security/src/main/java/org/opensearch/security/auditlog/sink/AuditLogSink.java:63: SYSTEM.OUT.PRINTLN NONONO! [RegexpSingleline]
[ant:checkstyle] [ERROR] /local/home/petern/git/security/src/main/java/org/opensearch/security/auditlog/sink/DebugSink.java:30: SYSTEM.OUT.PRINTLN NONONO! [RegexpSingleline]
[ant:checkstyle] [ERROR] /local/home/petern/git/security/src/main/java/org/opensearch/security/ssl/OpenSearchSecuritySSLPlugin.java:161: SYSTEM.OUT.PRINTLN NONONO! [RegexpSingleline]
[ant:checkstyle] [ERROR] /local/home/petern/git/security/src/main/java/org/opensearch/security/ssl/OpenSearchSecuritySSLPlugin.java:162: SYSTEM.OUT.PRINTLN NONONO! [RegexpSingleline]
[ant:checkstyle] [ERROR] /local/home/petern/git/security/src/main/java/org/opensearch/security/ssl/OpenSearchSecuritySSLPlugin.java:228: SYSTEM.OUT.PRINTLN NONONO! [RegexpSingleline]
[ant:checkstyle] [ERROR] /local/home/petern/git/security/src/main/java/org/opensearch/security/ssl/OpenSearchSecuritySSLPlugin.java:229: SYSTEM.OUT.PRINTLN NONONO! [RegexpSingleline]
[ant:checkstyle] [ERROR] /local/home/petern/git/security/src/main/java/org/opensearch/security/tools/AuditConfigMigrater.java:94: SYSTEM.OUT.PRINTLN NONONO! [RegexpSingleline]
[ant:checkstyle] [ERROR] /local/home/petern/git/security/src/main/java/org/opensearch/security/tools/AuditConfigMigrater.java:109: SYSTEM.OUT.PRINTLN NONONO! [RegexpSingleline]
[ant:checkstyle] [ERROR] /local/home/petern/git/security/src/main/java/org/opensearch/security/tools/AuditConfigMigrater.java:112: SYSTEM.OUT.PRINTLN NONONO! [RegexpSingleline]

Copy link
Member Author

@DarshitChanpura DarshitChanpura Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but the problem is how to exclude. There is no such Property called exclude. Do you know of anything of that might work?
Closest thing I found was this: https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/sun_checks.xml#L46-L50
However this applies to whole file, so I might have to end up creating a separate xml for sysout check anyway. (didn't try it yet)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to achieve this by adding a separate file for println checks.

Copy link
Member

@peternied peternied Aug 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know but this would allow us to exclude files in one place instead of marking them individually. I felt like this was cleaner, so I went with this option. What do you think?

Signed-off-by: Darshit Chanpura <[email protected]>
@DarshitChanpura
Copy link
Member Author

Taking a look at the test failure

Fixed

@DarshitChanpura DarshitChanpura self-assigned this Aug 23, 2023
@DarshitChanpura DarshitChanpura merged commit 683c4f5 into opensearch-project:main Aug 23, 2023
@DarshitChanpura DarshitChanpura added the backport 2.x backport to 2.x branch label Aug 24, 2023
@opensearch-trigger-bot
Copy link
Contributor

The backport to 2.x failed:

The process '/usr/bin/git' failed with exit code 128

To backport manually, run these commands in your terminal:

# Navigate to the root of your repository
cd $(git rev-parse --show-toplevel)
# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add ../.worktrees/security/backport-2.x 2.x
# Navigate to the new working tree
pushd ../.worktrees/security/backport-2.x
# Create a new branch
git switch --create backport/backport-3231-to-2.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 683c4f564e89583d9e98bc2fc4eaaf62b630e2f1
# Push it to GitHub
git push --set-upstream origin backport/backport-3231-to-2.x
# Go back to the original working tree
popd
# Delete the working tree
git worktree remove ../.worktrees/security/backport-2.x

Then, create a pull request where the base branch is 2.x and the compare/head branch is backport/backport-3231-to-2.x.

DarshitChanpura added a commit to DarshitChanpura/security that referenced this pull request Aug 25, 2023
…pensearch-project#3231)

A lot of files (mostly tests) have sysout's which do nothing but degrade
the performance. This PR removes all sysouts from tests, replace sysouts
with logger in project files.
Also removes all e.printStacktrace(); calls.

- `tools/` package  - no sysouts were removed from here
- HTTPSpnegoAuthenticator.java - a portion was not modified since it
enabled debugging on console.

Signed-off-by: Darshit Chanpura <[email protected]>
(cherry picked from commit 683c4f5)
peternied pushed a commit that referenced this pull request Aug 25, 2023
…ere necessary (#3231) (#3240)

Backports #3231.

Needed manual backport because of http5 imports and server `shutdown`
(2.x) vs `awaitTermination` (main) method calls in test.

Signed-off-by: Darshit Chanpura <[email protected]>
@peternied peternied changed the title Clean up sysouts and replace with logger statements where necessary Send log messages to log4j systems instead of system out / error Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 2.x backport to 2.x branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants