Enhance logging with use of lombok

This commit is contained in:
Malanius 2018-10-22 20:43:22 +02:00
parent 28bbf0815e
commit 1bff1b0f6e
6 changed files with 43 additions and 21 deletions

View file

@ -2,21 +2,23 @@ package cz.brmlab.wm;
import cz.brmlab.wm.utils.Exceptions.BrmException;
import cz.brmlab.wm.wekan.WekanConfiguration;
import cz.brmlab.wm.wekan.pojo.card.CardRequest;
import cz.brmlab.wm.wekan.rest.CardPost;
import cz.brmlab.wm.wekan.rest.LoginPost;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@Slf4j
@SpringBootApplication
public class Application implements CommandLineRunner {
private static final Logger LOG = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
log.trace("Starting wekan-mailer app...");
SpringApplication.run(Application.class, args);
}
@ -33,6 +35,7 @@ public class Application implements CommandLineRunner {
cardPost.postCard("Test from spring", "Test card from awesome spring app.\nAnd next line");
} catch (BrmException ex) {
log.error("Error {} encountered, shutting down!", ex.getExitCode());
System.exit(ex.getExitCode().getCode());
}
}

View file

@ -0,0 +1,17 @@
package cz.brmlab.wm.utils.LogMarker;
import lombok.Getter;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
public enum LogMarker {
SECRET("SECRET");
@Getter
private Marker marker;
LogMarker(String markerText) {
this.marker = MarkerFactory.getMarker(markerText);
}
}

View file

@ -3,20 +3,15 @@ package cz.brmlab.wm.wekan;
import cz.brmlab.wm.utils.Exceptions.BrmException;
import cz.brmlab.wm.utils.Exceptions.ExitCode;
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Slf4j
public class WekanConfiguration {
/**
* Class logger
*/
private static final Logger LOG = LoggerFactory.getLogger(WekanConfiguration.class);
//ENV variables for wekan
private static final String WEKAN_URL = "WEKAN_URL";
private static final String WEKAN_USER = "WEKAN_USER";
@ -33,7 +28,7 @@ public class WekanConfiguration {
* @throws BrmException if some of the properties is missing in ENV variables.
*/
public WekanConfiguration() throws BrmException {
LOG.trace("{}() - start.", this.getClass().getSimpleName());
log.trace("{}() - start.", this.getClass().getSimpleName());
for (String prop : properties) {
checkProp(prop);
@ -44,7 +39,7 @@ public class WekanConfiguration {
this.wekanBoard = System.getenv(WEKAN_TARGET_BOARD);
this.wekanList = System.getenv(WEKAN_TARGET_LIST);
LOG.info("Wekan config loaded successfully.");
log.info("Wekan config loaded successfully.");
}
@Getter
@ -63,11 +58,11 @@ public class WekanConfiguration {
private String wekanList;
private void checkProp(String prop) throws BrmException {
LOG.trace("checkProp({}) - start.", prop);
log.trace("checkProp({}) - start.", prop);
if (System.getenv(prop) == null) {
String message = ExitCode.CONFIGURATION_MISSING.getReason() + prop;
LOG.error(message, ExitCode.CONFIGURATION_MISSING);
log.error(message, ExitCode.CONFIGURATION_MISSING);
throw new BrmException(message, ExitCode.CONFIGURATION_MISSING);
}
}

View file

@ -1,7 +1,5 @@
package cz.brmlab.wm.wekan.pojo.card;
import cz.brmlab.wm.wekan.WekanConfiguration;
import cz.brmlab.wm.wekan.pojo.login.LoginToken;
import lombok.Data;
@Data

View file

@ -4,23 +4,24 @@ import cz.brmlab.wm.wekan.WekanConfiguration;
import cz.brmlab.wm.wekan.pojo.card.CardRequest;
import cz.brmlab.wm.wekan.pojo.card.CardResponse;
import cz.brmlab.wm.wekan.pojo.login.LoginToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;
@Slf4j
public class CardPost {
private static final Logger LOG = LoggerFactory.getLogger(CardPost.class);
private final LoginToken token;
private final WekanConfiguration configuration;
public CardPost(LoginToken token, WekanConfiguration configuration) {
log.trace("{}() - start.", this.getClass().getSimpleName());
this.token = token;
this.configuration = configuration;
}
public CardResponse postCard(String title, String description) {
log.trace("postCard({}, {}) - start.", title, description);
CardRequest cardRequest = new CardRequest();
cardRequest.setAuthorId(token.getId());
cardRequest.setTitle(title);
@ -33,9 +34,11 @@ public class CardPost {
+ configuration.getWekanList() + "cards";
RestTemplate restTemplate = new RestTemplate();
LOG.debug("Sending card: {}", cardRequest);
log.debug("Sending card: {}", cardRequest);
CardResponse cardResponse = restTemplate.postForObject(cardUrl, cardRequest, CardResponse.class);
log.info("Card {} successfully sent - cardId: {}", title, cardResponse.getId());
return cardResponse;
}
}

View file

@ -1,11 +1,14 @@
package cz.brmlab.wm.wekan.rest;
import cz.brmlab.wm.utils.LogMarker.LogMarker;
import cz.brmlab.wm.wekan.WekanConfiguration;
import cz.brmlab.wm.wekan.pojo.login.LoginRequest;
import cz.brmlab.wm.wekan.pojo.login.LoginToken;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;
@Slf4j
public class LoginPost {
private final WekanConfiguration configuration;
@ -19,16 +22,19 @@ public class LoginPost {
public void login() {
log.trace("login() - start.");
String loginUrl = configuration.getWekanUrl() + "/users/login";
log.debug("Wekan login endpoint: {}", loginUrl);
LoginRequest loginRequest = new LoginRequest();
loginRequest.setUsername(configuration.getWekanUser());
loginRequest.setPassword(configuration.getWekanPassword());
log.info("Sending login request...");
RestTemplate restTemplate = new RestTemplate();
token = restTemplate.postForObject(loginUrl, loginRequest, LoginToken.class);
System.out.println(token);
log.info("Login successful, token obtained.");
log.debug(LogMarker.SECRET.getMarker(), "Token: {}", token.toString());
}
}