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

feature: Add OFF log level to StandardEnvironment #3904

Merged
merged 5 commits into from
May 11, 2021
Merged

feature: Add OFF log level to StandardEnvironment #3904

merged 5 commits into from
May 11, 2021

Conversation

gchaperon
Copy link
Contributor

As discussed in #3901, this PR adds a new logging level OFF(0) and modifies the printing logic in StandardEnvironment so that it is turned off if this new level is set.

I tagged this as a feature following the tag of the issue.
The contributing guidelines state that feature PR should have tests and corresponding documentation. I though this PR is maybe to small for that, but please let me know if you would like tests added and what should they cover.

Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @gchaperon!

No behavioral change is too small for a test case :). See spoon.test.logging.LogTest. It shouldn't be too hard to verify that no message gets logged for any logging level if the level is set to OFF. You can draw inspiration from the testAllLevelsForLogs test case (see how it captures the logging output).

@@ -274,7 +274,7 @@ public void report(Processor<?> processor, Level level, String message) {
}

private void print(String message, Level messageLevel) {
if (messageLevel.toInt() <= this.level.toInt()) {
if (this.level.compareTo(Level.OFF) > 0 && messageLevel.toInt() <= this.level.toInt()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps this is more clear?

Suggested change
if (this.level.compareTo(Level.OFF) > 0 && messageLevel.toInt() <= this.level.toInt()) {
if (this.level != Level.OFF && messageLevel.toInt() <= this.level.toInt()) {

I think it's a bit easier to read as it doesn't require one to go check the declaration order of the Level values. This is probably a matter of preference, though, if you prefer your solution feel free to keep it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Note that there's a tiny behavioral difference: level != Level.OFF will cause the if-block not to be entered even if the passed level is OFF, whereas your original solution will cause it to be entered (but do nothing as no case in the switch matches)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, that's better, I didn't know enums could compare like that (that's how little java I know 😅)

@gchaperon
Copy link
Contributor Author

hi!

I changed the line following your advice and added a test.
I have no idea whatsoever as to where to put the test. I saw there was a class for nonparameterized tests, so I added it there.
Also, since print is private I used (what I think is) the closest proxy for it, which is report. This method receives a Processor but does not use it, so I used whatever Processor implementation was the easiest to instantiate.

Let me know what you think.

Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

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

I think this looks very good! The placement of the test is correct. The old JUnit4 parameterization is awkward because it parameterizes the entire class, which is why a nested class is needed for the non-parameterized tests.

JUnit5 parameterization is far less confusing (very easy to apply to just a single test).

Anyway, I just had a single comment in that we typically try to put a contract at the top of the test, stating what the test should do. See for example testMavenLauncherLogs (and my comment).

Comment on lines 95 to 97
@Test
public void testLoggingOff() {
final TestLogger logger = TestLoggerFactory.getTestLogger(Launcher.class);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you add a contract as a comment at the top of the test method? I.e. a comment stating what the test should do along, like so (ofc replacing my nonsense text):

Suggested change
@Test
public void testLoggingOff() {
final TestLogger logger = TestLoggerFactory.getTestLogger(Launcher.class);
@Test
public void testLoggingOff() {
// contract: The logger should do X given Y and perhaps Z
final TestLogger logger = TestLoggerFactory.getTestLogger(Launcher.class);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

aahrg, I saw you had this convention, but then forgot to add one
I just fixed it

@gchaperon
Copy link
Contributor Author

there, I just added a contract

Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

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

Just one final thing, there are a couple of redundant imports in LogTest, probably added automatically by your IDE when you were trying stuff out. I missed them before, sorry!

After that I'm satisfied and we ping an integrator :)

Comment on lines 25 to 26
import spoon.processing.Processor;
import spoon.reflect.declaration.CtElement;
Copy link
Collaborator

Choose a reason for hiding this comment

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

These two imports seem redundant.

Suggested change
import spoon.processing.Processor;
import spoon.reflect.declaration.CtElement;

@gchaperon
Copy link
Contributor Author

That is exactly what happened, thanks for the notice.

Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

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

@gchaperon Looks very good to me!

Ping @monperrus for workflow activation

@slarse
Copy link
Collaborator

slarse commented May 10, 2021

Oh, and @gchaperon change the title from WIP to review :)

@gchaperon gchaperon changed the title WIP: feature: Add OFF log level to StandardEnvironment review: feature: Add OFF log level to StandardEnvironment May 10, 2021
@monperrus monperrus changed the title review: feature: Add OFF log level to StandardEnvironment feature: Add OFF log level to StandardEnvironment May 11, 2021
@monperrus monperrus merged commit 639dccf into INRIA:master May 11, 2021
@monperrus
Copy link
Collaborator

Thanks a lot @gchaperon for your first contribution in Spoon.

@gchaperon
Copy link
Contributor Author

also thanks for the guidance and patience!

@slarse
Copy link
Collaborator

slarse commented May 11, 2021

also thanks for the guidance and patience!

You're very welcome, we're always super happy to help new (or existing, some things are hard!) contributors in completing their PRs :)

@monperrus monperrus mentioned this pull request Aug 19, 2021
woutersmeenk pushed a commit to woutersmeenk/spoon that referenced this pull request Aug 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants