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

#618 - Add ability to name specific wiremock instance #2448

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

kapishmalik
Copy link
Contributor

Added ability to name specific wiremock instance.

References

#618

Submitter checklist

  • Recommended: Join WireMock Slack to get any help in #help-contributing or a project-specific channel like #wiremock-java
  • Recommended: If you participate in Hacktoberfest 2023, make sure you're signed up there and in the WireMock form
  • The PR request is well described and justified, including the body and the references
  • The PR title represents the desired changelog entry
  • The repository's code style is followed (see the contributing guide)
  • Test coverage that demonstrates that the change works as expected
  • For new features, there's necessary documentation in this pull request or in a subsequent PR to [wiremock.org]

@kapishmalik kapishmalik requested a review from a team as a code owner October 16, 2023 16:48
@kapishmalik kapishmalik changed the title Resolves #618 - Add ability to name specific wiremock instance #618 - Add ability to name specific wiremock instance Oct 16, 2023
@tomakehurst
Copy link
Member

This is looking good so far but let's add add a test that checks the name is actually emitted in logs.

Also, what might be interesting is to also have it affect the Slf4jNotifier that's used when starting programmatically. Specifically, we could modify line 23 to make it an instance field and set the logger name to the server name, something like:

private final Logger log = LoggerFactory.getLogger(firstNonNull(serverName, "WireMock"));

@@ -15,19 +15,25 @@
*/
package com.github.tomakehurst.wiremock.common;

import static org.apache.commons.lang3.ObjectUtils.firstNonNull;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use ParameterUtils#getFirstNonNull

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Incorporated


private final boolean verbose;

public Slf4jNotifier(boolean verbose) {
this.verbose = verbose;
}

public void updateLogger(final String serverName) {
log = LoggerFactory.getLogger(getFirstNonNull(serverName, "WireMock"));
Copy link
Member

Choose a reason for hiding this comment

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

Suggest we do this in a constructor overload rather than mutating

Copy link
Member

Choose a reason for hiding this comment

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

TBH I am not site about making the logger non-static for that purpose. Maybe there is API would could invoke against a static field? Concurrency is not a problem here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done... incorporated.

Copy link
Member

@oleg-nenashev oleg-nenashev left a comment

Choose a reason for hiding this comment

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

Looks good overall, requesting changes because of unverified lass cast, and I second @tomakehurst's note about the APIs though do not feel strongly about that


private final boolean verbose;

public Slf4jNotifier(boolean verbose) {
this.verbose = verbose;
}

public void updateLogger(final String serverName) {
log = LoggerFactory.getLogger(getFirstNonNull(serverName, "WireMock"));
Copy link
Member

Choose a reason for hiding this comment

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

TBH I am not site about making the logger non-static for that purpose. Maybe there is API would could invoke against a static field? Concurrency is not a problem here

public WireMockConfiguration serverName(String serverName) {

this.serverName = serverName;
((Slf4jNotifier) notifier).updateLogger(serverName);
Copy link
Member

Choose a reason for hiding this comment

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

This unchecked cast impacts extensibility IMO. I suggest doing proper instanceof verification

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Member

@oleg-nenashev oleg-nenashev left a comment

Choose a reason for hiding this comment

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

Options#getServerName() is still a breaking change, because there is no default implementation
https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

@kapishmalik
Copy link
Contributor Author

Options#getServerName() is still a breaking change, because there is no default implementation https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

incorporated

Copy link
Member

@oleg-nenashev oleg-nenashev left a comment

Choose a reason for hiding this comment

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

My comments are addressed

public void checkServerNameIsPrintedInLogs() {

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
System.setOut(new PrintStream(byteArrayOutputStream));
Copy link
Member

Choose a reason for hiding this comment

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

We need to un-set this in an @AfterEach block, otherwise it might suppress the output from other tests when running everything.

this.verbose = verbose;
}

public void setLogger(String loggerName) {
Copy link
Member

Choose a reason for hiding this comment

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

I still think we should try to get rid of this and make the logger final.

@oleg-nenashev oleg-nenashev added the needs-tom Tom's Train Project :) label Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants