From 6b29e8f7464ef59ec2845ad4eb31edcf3d9670e9 Mon Sep 17 00:00:00 2001 From: nyg Date: Sun, 10 Mar 2024 12:06:44 +0100 Subject: [PATCH 1/2] Add slf4j --- examples/pom.xml | 10 ++++++ .../dev/andstuff/kraken/example/Examples.java | 23 +++++++------ .../andstuff/kraken/example/JacksonTest.java | 34 ------------------- .../andstuff/kraken/example/TotalRewards.java | 15 ++++---- examples/src/main/resources/log4j2.xml | 18 ++++++++++ .../api/rest/DefaultKrakenRestRequester.java | 6 ++-- pom.xml | 23 +++++++++++++ 7 files changed, 73 insertions(+), 56 deletions(-) delete mode 100644 examples/src/main/java/dev/andstuff/kraken/example/JacksonTest.java create mode 100644 examples/src/main/resources/log4j2.xml diff --git a/examples/pom.xml b/examples/pom.xml index 269eada..216eac3 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -17,6 +17,16 @@ kraken-api ${project.version} + + + + org.apache.logging.log4j + log4j-core + + + org.apache.logging.log4j + log4j-slf4j2-impl + diff --git a/examples/src/main/java/dev/andstuff/kraken/example/Examples.java b/examples/src/main/java/dev/andstuff/kraken/example/Examples.java index 624c764..ee58966 100644 --- a/examples/src/main/java/dev/andstuff/kraken/example/Examples.java +++ b/examples/src/main/java/dev/andstuff/kraken/example/Examples.java @@ -13,39 +13,40 @@ import dev.andstuff.kraken.api.model.endpoint.market.response.AssetPair; import dev.andstuff.kraken.api.model.endpoint.market.response.ServerTime; import dev.andstuff.kraken.api.model.endpoint.market.response.SystemStatus; +import lombok.extern.slf4j.Slf4j; +@Slf4j public class Examples { public static void main(String[] args) { - /* Public endpoint examples */ KrakenAPI publicAPI = new KrakenAPI(); ServerTime serverTime = publicAPI.serverTime(); - System.out.println(serverTime); + log.info("{}", serverTime); SystemStatus systemStatus = publicAPI.systemStatus(); - System.out.println(systemStatus); + log.info("{}", systemStatus); Map assets1 = publicAPI.assetInfo(List.of("BTC", "ETH")); - System.out.println(assets1); + log.info("{}", assets1); Map assets2 = publicAPI.assetInfo(List.of("DOT", "ADA"), "currency"); - System.out.println(assets2); + log.info("{}", assets2); Map pairs1 = publicAPI.assetPairs(List.of("ETH/BTC", "ETH/USD")); - System.out.println(pairs1); + log.info("{}", pairs1); Map pairs2 = publicAPI.assetPairs(List.of("DOT/USD", "ADA/USD"), AssetPair.Info.MARGIN); - System.out.println(pairs2); + log.info("{}", pairs2); JsonNode ticker = publicAPI.query(KrakenAPI.Public.TICKER, Map.of("pair", "XBTEUR")); - System.out.println(ticker); + log.info("{}", ticker); JsonNode trades = publicAPI.queryPublic("Trades", Map.of("pair", "XBTUSD", "count", "1")); - System.out.println(trades); + log.info("{}", trades); /* Private endpoint example */ @@ -54,7 +55,7 @@ public static void main(String[] args) { KrakenAPI api = new KrakenAPI(apiKeys.getProperty("key"), apiKeys.getProperty("secret")); JsonNode balance = api.query(KrakenAPI.Private.BALANCE); - System.out.println(balance); + log.info("{}", balance); JsonNode order = api.query(KrakenAPI.Private.ADD_ORDER, Map.of( "ordertype", "limit", @@ -64,6 +65,6 @@ public static void main(String[] args) { "price", "1000", "oflags", "post,fciq", "validate", "true")); - System.out.println(order); + log.info("{}", order); } } diff --git a/examples/src/main/java/dev/andstuff/kraken/example/JacksonTest.java b/examples/src/main/java/dev/andstuff/kraken/example/JacksonTest.java deleted file mode 100644 index a9c46ed..0000000 --- a/examples/src/main/java/dev/andstuff/kraken/example/JacksonTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package dev.andstuff.kraken.example; - -import java.util.Optional; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.MapperFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.json.JsonMapper; -import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; - -public class JacksonTest { - - private static final ObjectMapper OBJECT_MAPPER; - - static { - OBJECT_MAPPER = JsonMapper.builder() - .enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS) - .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) - .addModules(new JavaTimeModule(), new Jdk8Module()) - .build(); - } - - public static void main(String[] args) throws JsonProcessingException { - MyRecord myRecord = OBJECT_MAPPER.readValue("{}", MyRecord.class); - System.out.println(myRecord); - System.out.println(myRecord.result().isEmpty()); - } - - public record MyRecord(Optional result) {} - - public class MyClass {} -} diff --git a/examples/src/main/java/dev/andstuff/kraken/example/TotalRewards.java b/examples/src/main/java/dev/andstuff/kraken/example/TotalRewards.java index d59daaa..2434fba 100644 --- a/examples/src/main/java/dev/andstuff/kraken/example/TotalRewards.java +++ b/examples/src/main/java/dev/andstuff/kraken/example/TotalRewards.java @@ -4,14 +4,11 @@ import static java.util.Arrays.asList; import static java.util.Comparator.comparing; import static java.util.stream.Collectors.groupingBy; -import static java.util.stream.Collectors.toList; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import java.math.BigDecimal; -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; import java.time.Instant; import java.util.HashMap; import java.util.Iterator; @@ -28,15 +25,15 @@ */ public class TotalRewards { - public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeyException, InterruptedException { + public static void main(String[] args) throws IOException, InterruptedException { Properties apiKeys = readFromFile("/api-keys.properties"); KrakenAPI api = new KrakenAPI(apiKeys.getProperty("key"), apiKeys.getProperty("secret")); - Map params = Map.of( - "type", "staking", - "without_count", "true", - "ofs", "0"); + Map params = new HashMap<>(); + params.put("type", "staking"); + params.put("without_count", "true"); + params.put("ofs", "0"); Map rewards = new HashMap<>(); @@ -76,7 +73,7 @@ public static void main(String[] args) throws IOException, NoSuchAlgorithmExcept List assetRewards = groupedRewards.get(asset).stream() .filter(e -> !asList("migration", "spottostaking").contains(e.findValue("subtype").textValue())) .sorted(comparing(e -> e.get("time").asInt())) - .collect(toList()); + .toList(); BigDecimal assetTotalRewardAmount = assetRewards.stream() .map(e -> new BigDecimal(e.findValue("amount").textValue()) diff --git a/examples/src/main/resources/log4j2.xml b/examples/src/main/resources/log4j2.xml new file mode 100644 index 0000000..dca53be --- /dev/null +++ b/examples/src/main/resources/log4j2.xml @@ -0,0 +1,18 @@ + + + + [%d{ISO8601}] [%c{1.1.1.2.}] [%t] [%p] %m%n + + + + + + + + + + + + + + diff --git a/library/src/main/java/dev/andstuff/kraken/api/rest/DefaultKrakenRestRequester.java b/library/src/main/java/dev/andstuff/kraken/api/rest/DefaultKrakenRestRequester.java index e35d659..f652fb3 100644 --- a/library/src/main/java/dev/andstuff/kraken/api/rest/DefaultKrakenRestRequester.java +++ b/library/src/main/java/dev/andstuff/kraken/api/rest/DefaultKrakenRestRequester.java @@ -21,10 +21,12 @@ import dev.andstuff.kraken.api.model.endpoint.priv.PostParams; import dev.andstuff.kraken.api.model.endpoint.priv.PrivateEndpoint; import dev.andstuff.kraken.api.model.endpoint.pub.PublicEndpoint; +import lombok.extern.slf4j.Slf4j; /** * {@link KrakenRestRequester} implementation using {@link HttpsURLConnection}. */ +@Slf4j public class DefaultKrakenRestRequester implements KrakenRestRequester { private static final ObjectMapper OBJECT_MAPPER; @@ -51,7 +53,7 @@ public DefaultKrakenRestRequester(String key, String secret) { public T execute(PublicEndpoint endpoint) { try { HttpsURLConnection connection = createHttpsConnection(endpoint); - System.out.printf("Fetching %s%n", connection.getURL()); + log.info("Fetching {}", connection.getURL()); return parseResponse(connection.getInputStream(), endpoint); } catch (IOException e) { @@ -72,7 +74,7 @@ public T execute(PrivateEndpoint endpoint) { try { HttpsURLConnection connection = createHttpsConnection(endpoint); - System.out.printf("Fetching %s%n", connection.getURL()); + log.info("Fetching {}", connection.getURL()); connection.addRequestProperty("API-Key", credentials.getKey()); connection.addRequestProperty("API-Sign", credentials.sign(connection.getURL(), nonce, postData)); connection.setDoOutput(true); diff --git a/pom.xml b/pom.xml index 5ed4471..9970451 100644 --- a/pom.xml +++ b/pom.xml @@ -52,6 +52,8 @@ 2.16.1 1.18.30 + 2.0.12 + 2.23.0 3.1.1 @@ -81,6 +83,23 @@ ${lombok.version} provided + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.apache.logging.log4j + log4j-core + ${log4j.version} + runtime + + + org.apache.logging.log4j + log4j-slf4j2-impl + ${log4j.version} + runtime + @@ -102,6 +121,10 @@ org.projectlombok lombok + + org.slf4j + slf4j-api + From 46cf59d29275813b9a47f939ddf9d5af59211e3b Mon Sep 17 00:00:00 2001 From: nyg Date: Sun, 10 Mar 2024 12:21:45 +0100 Subject: [PATCH 2/2] Remove log4j lib from main pom, change log pattern --- examples/pom.xml | 10 ++++++++-- .../java/dev/andstuff/kraken/example/Examples.java | 1 - examples/src/main/resources/log4j2.xml | 2 +- pom.xml | 12 ------------ 4 files changed, 9 insertions(+), 16 deletions(-) diff --git a/examples/pom.xml b/examples/pom.xml index 216eac3..3d91ebd 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -11,21 +11,27 @@ kraken-api-examples + + 2.23.0 + + dev.andstuff.kraken kraken-api ${project.version} - - org.apache.logging.log4j log4j-core + ${log4j.version} + runtime org.apache.logging.log4j log4j-slf4j2-impl + ${log4j.version} + runtime diff --git a/examples/src/main/java/dev/andstuff/kraken/example/Examples.java b/examples/src/main/java/dev/andstuff/kraken/example/Examples.java index ee58966..0d6f34f 100644 --- a/examples/src/main/java/dev/andstuff/kraken/example/Examples.java +++ b/examples/src/main/java/dev/andstuff/kraken/example/Examples.java @@ -51,7 +51,6 @@ public static void main(String[] args) { /* Private endpoint example */ Properties apiKeys = readFromFile("/api-keys.properties"); - KrakenAPI api = new KrakenAPI(apiKeys.getProperty("key"), apiKeys.getProperty("secret")); JsonNode balance = api.query(KrakenAPI.Private.BALANCE); diff --git a/examples/src/main/resources/log4j2.xml b/examples/src/main/resources/log4j2.xml index dca53be..d4aa8bc 100644 --- a/examples/src/main/resources/log4j2.xml +++ b/examples/src/main/resources/log4j2.xml @@ -1,7 +1,7 @@ - [%d{ISO8601}] [%c{1.1.1.2.}] [%t] [%p] %m%n + [%d{ISO8601}] [%-20.20c{1.}] [%-7.7t] [%-5.5p] %m%n diff --git a/pom.xml b/pom.xml index 9970451..b6c42c0 100644 --- a/pom.xml +++ b/pom.xml @@ -88,18 +88,6 @@ slf4j-api ${slf4j.version} - - org.apache.logging.log4j - log4j-core - ${log4j.version} - runtime - - - org.apache.logging.log4j - log4j-slf4j2-impl - ${log4j.version} - runtime -