Skip to content

Commit 8d94aed

Browse files
committed
chore(logging): don't log a response from Mailgun with "info" level when it has 200 status.
Log it with "debug" level when its status is 200. Otherwise log a warning and include details.
1 parent 3856cb1 commit 8d94aed

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/main/java/ru/mystamps/web/support/mailgun/ApiMailgunEmailSendingStrategy.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.http.HttpEntity;
2525
import org.springframework.http.HttpHeaders;
2626
import org.springframework.http.HttpMethod;
27+
import org.springframework.http.HttpStatus;
2728
import org.springframework.http.MediaType;
2829
import org.springframework.http.ResponseEntity;
2930
import org.springframework.util.LinkedMultiValueMap;
@@ -130,13 +131,23 @@ public void send(MailgunEmail email) {
130131
String.class
131132
);
132133

133-
LOG.info("Mailgun response code: {}", response.getStatusCode());
134-
LOG.debug("Mailgun response headers: {}", response.getHeaders());
135-
LOG.info("Mailgun response body: {}", StringUtils.remove(response.getBody(), '\n'));
134+
boolean isWarning = response.getStatusCodeValue() == HttpStatus.OK.value();
135+
String body = StringUtils.remove(response.getBody(), '\n');
136+
logWarningOrDebug(isWarning, "Mailgun response code: {}", response.getStatusCode());
137+
logWarningOrDebug(isWarning, "Mailgun response headers: {}", response.getHeaders());
138+
logWarningOrDebug(isWarning, "Mailgun response body: {}", body);
136139

137140
} catch (UnsupportedEncodingException | RestClientException ex) {
138141
throw new EmailSendingException("Can't send mail to " + email.recipientAddress(), ex);
139142
}
140143
}
141144

145+
private static void logWarningOrDebug(boolean isWarning, String msg, Object... args) {
146+
if (isWarning) {
147+
LOG.warn(msg, args);
148+
} else {
149+
LOG.debug(msg, args);
150+
}
151+
}
152+
142153
}

0 commit comments

Comments
 (0)