Refactor logging for better granularity
Updated logging levels in multiple classes to differentiate between debug and trace information. Improved log messages to support parameterized logs, ensuring sensitive or variable information is handled appropriately without concatenation.
This commit is contained in:
parent
f68a96c089
commit
cbb5dd99d8
|
|
@ -18,14 +18,17 @@ public class ContactController {
|
|||
|
||||
@PostMapping("/submitContactForm")
|
||||
public CompletableFuture<ResponseEntity<String>> submitForm(@Valid @RequestBody ContactFormData formData) {
|
||||
logger.debug(formData.toString());
|
||||
logger.debug("submitForm");
|
||||
logger.trace(formData.toString());
|
||||
|
||||
CompletableFuture<Integer> storeFormForVerificationCode = new StoreFormQuery().storeFormForVerificationCode(formData.email, formData);
|
||||
return storeFormForVerificationCode.thenCompose(code -> Verify.verifyEmail(formData.email, code).thenApply(verificationResult -> {
|
||||
if (verificationResult == VerificationResult.VERIFICATION_SENT) {
|
||||
//TODO if this is ok tell the user they have x min to verify if they fail to do so they have to remake the form
|
||||
logger.trace("Contact form stored and requested verification from user");
|
||||
return ResponseEntity.ok("User Data received and email verification sent.");
|
||||
} else {
|
||||
logger.trace("Failed to send verification email");
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body("Failed to send verification email. Reason: " + verificationResult.name());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,13 +61,17 @@ public class StoreFormQuery {
|
|||
}
|
||||
|
||||
public CompletableFuture<Integer> storeFormForVerificationCode(String eMail, Form form) {
|
||||
logger.debug("storeFormForVerificationCode");
|
||||
Connection connection = DatabaseConnection.getConnection();
|
||||
logger.trace("Connection: {}", connection);
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
Optional<Long> optionalFormId = insertForm(connection, form);
|
||||
logger.trace("Form id: {}", optionalFormId);
|
||||
if (optionalFormId.isEmpty()) {
|
||||
throw new RuntimeException("Failed to store form");
|
||||
}
|
||||
Optional<Integer> verificationCode = insertVerificationCodeForForm(connection, eMail, optionalFormId.get());
|
||||
logger.trace("Verification code: {}", verificationCode);
|
||||
if (verificationCode.isEmpty()) {
|
||||
throw new RuntimeException("Failed to set verification code");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ public class MailForm {
|
|||
Properties mailProperties = MailSettings.getMailProperties();
|
||||
Optional<PasswordAuthentication> accountDetails = MailSettings.getAccountDetails();
|
||||
if (accountDetails.isEmpty()) {
|
||||
logger.error("No account details, can't send email to " + receiver + " with data " + form.toString());
|
||||
logger.error("No account details, can't send email to {} with data {}", receiver, form.toString());
|
||||
return;
|
||||
}
|
||||
PasswordAuthentication passwordAuthentication = accountDetails.get();
|
||||
|
|
@ -37,9 +37,9 @@ public class MailForm {
|
|||
message.setContent(form.toHtml(), "text/html");
|
||||
try {
|
||||
Transport.send(message);
|
||||
logger.debug("Send mail to " + receiver + " containing " + form) ;
|
||||
logger.debug("Send mail to {} containing {}", receiver, form);
|
||||
} catch (MessagingException e) {
|
||||
logger.error("Unable to send mail to " + receiver + " with data " + form, e);
|
||||
logger.error("Unable to send mail to {} with data {}", receiver, form, e);
|
||||
}
|
||||
} catch (MessagingException e) {
|
||||
logger.error("Failed to create MimeMessage", e);
|
||||
|
|
|
|||
|
|
@ -16,31 +16,42 @@ public class Verify {
|
|||
private static final Logger logger = LoggerFactory.getLogger(Verify.class);
|
||||
|
||||
public static CompletableFuture<VerificationResult> verifyEmail(String address, int code) {
|
||||
logger.debug("verifyEmail");
|
||||
Properties mailProperties = MailSettings.getMailProperties();
|
||||
logger.debug("mailProperties: {}", mailProperties);
|
||||
Optional<PasswordAuthentication> accountDetails = MailSettings.getAccountDetails();
|
||||
if (accountDetails.isEmpty()) {
|
||||
logger.debug("accountDetails is empty");
|
||||
return CompletableFuture.completedFuture(VerificationResult.NO_MAIL_ACCOUNT);
|
||||
}
|
||||
PasswordAuthentication passwordAuthentication = accountDetails.get();
|
||||
logger.trace("accountDetails: {}", passwordAuthentication);
|
||||
Session session = MailSettings.getSession(mailProperties, passwordAuthentication);
|
||||
|
||||
//TODO rate limit sending mail from IP and to specific e-mail addresses (max 1 per minute and max 10 per day)
|
||||
//TODO include a link to all emails that people can click to block us from sending mail to them so no one can use us to spam ppl
|
||||
try {
|
||||
logger.trace("Creating mail");
|
||||
Message message = new MimeMessage(session);
|
||||
message.setFrom(new InternetAddress(passwordAuthentication.getUserName()));
|
||||
logger.trace("Set from");
|
||||
message.setRecipients(
|
||||
Message.RecipientType.TO,
|
||||
InternetAddress.parse(address)
|
||||
);
|
||||
logger.trace("Set recipients");
|
||||
message.setSubject("Altitude Email Verification");
|
||||
message.setText("Please verify your email by entering the following code on the page you made the form in\n" + code); //TODO pretty html stuff
|
||||
logger.trace("Set code: {}", code);
|
||||
//TODO include the form they filled in (also in pretty html stuff)
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
logger.trace("Sending mail");
|
||||
Transport.send(message);
|
||||
logger.trace("Sending mail succeeded");
|
||||
return VerificationResult.VERIFICATION_SENT;
|
||||
} catch (MessagingException e) {
|
||||
logger.error("Failed to send mail", e);
|
||||
return VerificationResult.FAILED_TO_SEND;
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -26,12 +26,12 @@ public class FormQuery {
|
|||
stmt.setString(2, eMail);
|
||||
ResultSet resultSet = stmt.executeQuery();
|
||||
if (!resultSet.next()) {
|
||||
logger.warn("A user tried to enter an invalid code: " + verificationCode + " with email: " + eMail);
|
||||
logger.warn("A user tried to enter an invalid code: {} with email: {}", verificationCode, eMail);
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(resultSet.getInt("formId"));
|
||||
} catch (SQLException e) {
|
||||
logger.error("Failed select form query for verification code: " + verificationCode + " with e-mail " + eMail, e);
|
||||
logger.error("Failed select form query for verification code: {} with e-mail {}", verificationCode, eMail, e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
|
@ -43,7 +43,7 @@ public class FormQuery {
|
|||
stmt.setInt(1, formId);
|
||||
ResultSet resultSet = stmt.executeQuery();
|
||||
if (!resultSet.next()) {
|
||||
logger.warn("Could not find form with id: " + formId);
|
||||
logger.warn("Could not find form with id: {}", formId);
|
||||
return Optional.empty();
|
||||
}
|
||||
String json = resultSet.getString("form_json");
|
||||
|
|
@ -55,7 +55,7 @@ public class FormQuery {
|
|||
return Optional.empty();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
logger.error("Failed select form query for form with id: " + formId, e);
|
||||
logger.error("Failed select form query for form with id: {}", formId, e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
|
@ -87,6 +87,7 @@ public class FormQuery {
|
|||
}
|
||||
|
||||
if (formId.isEmpty()) {
|
||||
logger.trace("Unable to find form with code: {} and email {}", code, eMail);
|
||||
return new FormQueryResult(Optional.empty(), "Unable to find form for a user with this code and e-mail");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,17 +27,20 @@ public class VerifyController {
|
|||
|
||||
@PostMapping("/form")
|
||||
public CompletableFuture<ResponseEntity<String>> validateEmailFromForm(@Valid @RequestBody VerificationData verificationData) {
|
||||
logger.debug(verificationData.toString());
|
||||
logger.debug("validateEmailFromForm");
|
||||
logger.trace("verificationData: {}", verificationData);
|
||||
return new FormQuery().getFormForCode(verificationData.code, verificationData.eMail).thenApply(result -> result.form()
|
||||
.map(form -> {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("http://51.222.255.92:8001/api/contact/submitContactForm"))//TODO get uri from form
|
||||
.uri(new URI("https://forms.alttd.com:8002/api/contact/submitContactForm"))//TODO get uri from form
|
||||
.header("Content-Type", "application/json;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(form.toJsonString(), StandardCharsets.UTF_8))
|
||||
.build();
|
||||
logger.trace("request: {}", request);
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
logger.trace("response: {}", response);
|
||||
if (response.statusCode() < 200 || response.statusCode() > 200) {
|
||||
logger.error(String.format("Failed to send form to Discord. Got status code [%d], with body\n%s", response.statusCode(), response.body()));
|
||||
//TODO handle failure
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user