diff --git a/README.md b/README.md index 1205c24..6ecb08b 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Java 1.7+ ``` git clone https://github.com/opentimestamps/java-opentimestamps cd java-opentimestamps -mvn install +mvn install -P cli ``` #### SSL errors diff --git a/pom.xml b/pom.xml index 34fbd13..a5aba33 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.eternitywall java-opentimestamps - 1.19 + 1.19.1-SNAPSHOT java-opentimestamps The java implementation of the OpenTimestamps protocol @@ -79,9 +79,15 @@ org.slf4j - slf4j-simple + slf4j-api 1.7.29 + + ch.qos.logback + logback-classic + 1.2.3 + test + org.glassfish.jaxb jaxb-runtime @@ -198,6 +204,23 @@ + + cli + + + + ${project.basedir}/src/cli/resources + + + + + + ch.qos.logback + logback-classic + 1.2.3 + + + release-sign-artifacts diff --git a/src/cli/resources/logback.xml b/src/cli/resources/logback.xml new file mode 100644 index 0000000..18f917a --- /dev/null +++ b/src/cli/resources/logback.xml @@ -0,0 +1,11 @@ + + + + %msg%n + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/eternitywall/http/Request.java b/src/main/java/com/eternitywall/http/Request.java index 4aa96ed..9e7bb67 100644 --- a/src/main/java/com/eternitywall/http/Request.java +++ b/src/main/java/com/eternitywall/http/Request.java @@ -1,6 +1,7 @@ package com.eternitywall.http; -import com.eternitywall.ots.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.DataOutputStream; import java.io.InputStream; @@ -11,15 +12,13 @@ import java.util.Map; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Callable; -import java.util.logging.Logger; import java.util.zip.GZIPInputStream; -import java.util.zip.ZipException; /** * For making an HTTP request. */ public class Request implements Callable { - private static Logger log = Utils.getLogger(Request.class.getName()); + private static final Logger log = LoggerFactory.getLogger(Request.class); private URL url; private byte[] data; @@ -89,7 +88,7 @@ public Response call() throws Exception { } response.setStream(is); } catch (Exception e) { - log.warning(url.toString() + " exception " + e); + log.warn("{} exception {}", url, e); } finally { if (queue != null) { queue.offer(response); diff --git a/src/main/java/com/eternitywall/ots/Calendar.java b/src/main/java/com/eternitywall/ots/Calendar.java index 7baa30e..5a43ed3 100644 --- a/src/main/java/com/eternitywall/ots/Calendar.java +++ b/src/main/java/com/eternitywall/ots/Calendar.java @@ -10,7 +10,6 @@ import java.net.URL; import java.util.HashMap; import java.util.Map; -import java.util.logging.Logger; /** * Class representing remote calendar server interface. @@ -20,8 +19,6 @@ public class Calendar { private String url; private ECKey key; - private static Logger log = Utils.getLogger(Calendar.class.getName()); - /** * Create a RemoteCalendar. * diff --git a/src/main/java/com/eternitywall/ots/Esplora.java b/src/main/java/com/eternitywall/ots/Esplora.java index d85dec6..69c7a4f 100644 --- a/src/main/java/com/eternitywall/ots/Esplora.java +++ b/src/main/java/com/eternitywall/ots/Esplora.java @@ -3,14 +3,15 @@ import com.eternitywall.http.Request; import com.eternitywall.http.Response; import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.net.URL; import java.util.concurrent.*; -import java.util.logging.Logger; public class Esplora { - private static final Logger log = Utils.getLogger(Esplora.class.getName()); + private static final Logger log = LoggerFactory.getLogger(Esplora.class); private static final String esploraUrl = "https://blockstream.info/api"; /** @@ -37,7 +38,7 @@ public static BlockHeader block(final String hash) throws Exception { blockHeader.setMerkleroot(merkleroot); blockHeader.setTime(time); blockHeader.setBlockHash(hash); - log.info(take.getFromUrl() + " " + blockHeader); + log.debug("{} {}", take.getFromUrl(), blockHeader); return blockHeader; //log.warning("Cannot parse merkleroot from body: " + jsonObject + ": " + e.getMessage()); } diff --git a/src/main/java/com/eternitywall/ots/OpenTimestamps.java b/src/main/java/com/eternitywall/ots/OpenTimestamps.java index 12b6507..ec5d8b8 100644 --- a/src/main/java/com/eternitywall/ots/OpenTimestamps.java +++ b/src/main/java/com/eternitywall/ots/OpenTimestamps.java @@ -12,6 +12,8 @@ import org.bitcoinj.core.DumpedPrivateKey; import org.bitcoinj.core.ECKey; import org.bitcoinj.core.NetworkParameters; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.math.BigInteger; @@ -27,7 +29,6 @@ import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.logging.Logger; /** * The main class for timestamp operations. @@ -36,7 +37,7 @@ */ public class OpenTimestamps { - private static Logger log = Utils.getLogger(OpenTimestamps.class.getName()); + private static final Logger log = LoggerFactory.getLogger(OpenTimestamps.class); /** * Show information on a detached timestamp. @@ -164,7 +165,7 @@ public static Timestamp stamp(List fileTimestamps, List calendarsUrl.size() + privateCalendarsUrl.size()) { - log.severe("m cannot be greater than available calendar neither less or equal 0"); + log.warn("m must be greater than 0 and cannot be greater than the number of available calender urls"); throw new IOException(); } @@ -221,7 +222,7 @@ private static Timestamp create(Timestamp timestamp, List calendarUrls, DumpedPrivateKey dumpedPrivateKey = new DumpedPrivateKey(NetworkParameters.prodNet(), signature); key = dumpedPrivateKey.getKey(); } catch (Exception err) { - log.severe("Invalid private key"); + log.warn("Invalid private key"); } } @@ -265,7 +266,7 @@ private static Timestamp create(Timestamp timestamp, List calendarUrls, } if (count < m) { - log.severe("Failed to create timestamp: requested " + String.valueOf(m) + " attestation" + ((m > 1) ? "s" : "") + " but received only " + String.valueOf(count)); + log.warn("Failed to create timestamp: requested {} attestation{} but received only {}", m, (m > 1) ? "s" : "", count); } //shut down the executor service now @@ -314,8 +315,7 @@ public static Timestamp makeMerkleTree(List fileTimestamp public static HashMap verify(DetachedTimestampFile ots, DetachedTimestampFile stamped) throws Exception { if (!Arrays.equals(ots.fileDigest(), stamped.fileDigest())) { - log.severe("Expected digest " + Utils.bytesToHex(ots.fileDigest()).toLowerCase()); - log.severe("File does not match original!"); + log.warn("Expected digest {}. File does not match original!", Utils.bytesToHex(ots.fileDigest()).toLowerCase()); throw new Exception("File does not match original!"); } @@ -375,7 +375,7 @@ public static HashMap verify(Timestamp timest throw e; } - log.severe(Utils.toUpperFirstLetter(text) + " verification failed: " + e.getMessage()); + log.warn("{} verification failed: {}", Utils.toUpperFirstLetter(text), e.getMessage()); throw e; } } @@ -401,7 +401,7 @@ public static Long verify(BitcoinBlockHeaderAttestation attestation, byte[] msg) BitcoinNode bitcoin = new BitcoinNode(properties); blockInfo = bitcoin.getBlockHeader(height); } catch (Exception e1) { - log.fine("There is no local node available"); + log.info("This is no local node available"); try { String blockHash = Esplora.blockHash(height); @@ -430,9 +430,9 @@ public static Long verify(LitecoinBlockHeaderAttestation attestation, byte[] msg BlockHeader blockInfo; try { - String blockHash = blockHash = Esplora.blockHash(height); + String blockHash = Esplora.blockHash(height); blockInfo = Esplora.block(blockHash); - log.info("Lite-client verification, assuming block " + blockHash + " is valid"); + log.info("Lite-client verification, assuming block {} is valid", blockHash); } catch (Exception e2) { e2.printStackTrace(); throw e2; diff --git a/src/main/java/com/eternitywall/ots/OtsCli.java b/src/main/java/com/eternitywall/ots/OtsCli.java index 06ba670..3192a0e 100644 --- a/src/main/java/com/eternitywall/ots/OtsCli.java +++ b/src/main/java/com/eternitywall/ots/OtsCli.java @@ -8,6 +8,8 @@ import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.Options; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileInputStream; @@ -22,7 +24,6 @@ import java.util.List; import java.util.Map; import java.util.Properties; -import java.util.logging.Logger; /** * The CLI for OpenTimestamps. @@ -31,7 +32,7 @@ */ public class OtsCli { - private static Logger log = Utils.getLogger(OtsCli.class.getName()); + private static final Logger log = LoggerFactory.getLogger(OtsCli.class); private static String title = "OtsCli"; private static String version = "1.0"; private static List calendarsUrl = new ArrayList<>(); @@ -215,7 +216,7 @@ public static void info(String argsOts, boolean verbose) { String infoResult = OpenTimestamps.info(detached, verbose); System.out.println(infoResult); } catch (IOException e) { - log.severe("No valid file"); + log.warn("No valid file"); } } @@ -227,7 +228,7 @@ private static void multistamp(List argsFiles, List calendarsUrl try { privateUrls = readSignature(signatureFile); } catch (Exception e) { - log.severe("No valid signature file"); + log.warn("No valid signature file"); return; } } @@ -241,12 +242,10 @@ private static void multistamp(List argsFiles, List calendarsUrl Hash hash = Hash.from(file, Hash.getOp(algorithm)._TAG()); mapFiles.put(argsFile, DetachedTimestampFile.from(hash)); } catch (IOException e) { - e.printStackTrace(); - log.severe("File read error"); + log.warn("File read error", e); return; } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - log.severe("Crypto error"); + log.warn("Crypto error", e); return; } } @@ -262,8 +261,7 @@ private static void multistamp(List argsFiles, List calendarsUrl throw new IOException(); } } catch (IOException e) { - e.printStackTrace(); - log.severe("Stamp error"); + log.warn("Stamp error", e); return; } @@ -284,8 +282,7 @@ private static void multistamp(List argsFiles, List calendarsUrl System.out.println("The timestamp proof '" + argsOts + "' has been created!"); } } catch (Exception e) { - e.printStackTrace(); - log.severe("File '" + argsOts + "' writing error"); + log.warn("File '{}' writing error: {}", argsOts, e); } } } @@ -297,7 +294,7 @@ private static void stamp(Hash hash, List calendarsUrl, Integer m, Strin try { privateUrls = readSignature(signatureFile); } catch (Exception e) { - log.severe("No valid signature file"); + log.warn("No valid signature file"); } } @@ -315,7 +312,7 @@ private static void stamp(Hash hash, List calendarsUrl, Integer m, Strin Files.write(path, stampResult.serialize()); System.out.println("The timestamp proof '" + argsOts + "' has been created!"); } catch (Exception e) { - log.severe("Invalid shasum"); + log.warn("Invalid shasum"); } } @@ -359,7 +356,7 @@ public static void verify(String argsOts, Hash hash, String argsFile) { return; } } catch (Exception e) { - log.severe("No valid file"); + log.warn("No valid file"); } } @@ -391,10 +388,9 @@ public static void upgrade(String argsOts, boolean shrink) { Files.write(pathOts, detachedOts.serialize()); } } catch (IOException e) { - log.severe("No valid file"); + log.warn("No valid file"); } catch (Exception e) { - e.printStackTrace(); - log.severe("Shrink error"); + log.warn("Shrink error", e); } } diff --git a/src/main/java/com/eternitywall/ots/StreamDeserializationContext.java b/src/main/java/com/eternitywall/ots/StreamDeserializationContext.java index 55ead82..143fdd7 100644 --- a/src/main/java/com/eternitywall/ots/StreamDeserializationContext.java +++ b/src/main/java/com/eternitywall/ots/StreamDeserializationContext.java @@ -1,11 +1,13 @@ package com.eternitywall.ots; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.Arrays; -import java.util.logging.Logger; public class StreamDeserializationContext { - private static Logger log = Utils.getLogger(StreamDeserializationContext.class.getName()); + private static final Logger log = LoggerFactory.getLogger(StreamDeserializationContext.class); byte[] buffer; int counter = 0; @@ -81,10 +83,10 @@ public byte[] readVarbytes(int maxLen, int minLen) { int l = this.readVaruint(); if ((l & 0xff) > maxLen) { - log.severe("varbytes max length exceeded;"); + log.warn("varbytes max length exceeded;"); return null; } else if ((l & 0xff) < minLen) { - log.severe("varbytes min length not met;"); + log.warn("varbytes min length not met;"); return null; } diff --git a/src/main/java/com/eternitywall/ots/StreamSerializationContext.java b/src/main/java/com/eternitywall/ots/StreamSerializationContext.java index d06ee54..e534282 100644 --- a/src/main/java/com/eternitywall/ots/StreamSerializationContext.java +++ b/src/main/java/com/eternitywall/ots/StreamSerializationContext.java @@ -3,12 +3,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.logging.Logger; public class StreamSerializationContext { - private static Logger log = Utils.getLogger(StreamSerializationContext.class.getName()); - List buffer = new ArrayList<>(); public StreamSerializationContext() { diff --git a/src/main/java/com/eternitywall/ots/Timestamp.java b/src/main/java/com/eternitywall/ots/Timestamp.java index 33044d6..02c79d8 100644 --- a/src/main/java/com/eternitywall/ots/Timestamp.java +++ b/src/main/java/com/eternitywall/ots/Timestamp.java @@ -7,10 +7,11 @@ import com.eternitywall.ots.op.OpSHA256; import org.bitcoinj.core.NetworkParameters; import org.bitcoinj.core.Transaction; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.*; import java.util.Map.Entry; -import java.util.logging.Logger; /** * Proof that one or more attestations commit to a message. @@ -20,8 +21,6 @@ */ public class Timestamp { - private static Logger log = Utils.getLogger(Timestamp.class.getName()); - public byte[] msg; public List attestations; public HashMap ops; diff --git a/src/main/java/com/eternitywall/ots/Utils.java b/src/main/java/com/eternitywall/ots/Utils.java index f6ce4cf..f95f4ea 100644 --- a/src/main/java/com/eternitywall/ots/Utils.java +++ b/src/main/java/com/eternitywall/ots/Utils.java @@ -197,22 +197,4 @@ public static byte[] toBytes(String str, String encode) { public static String toUpperFirstLetter(String string) { return string.substring(0, 1).toUpperCase() + string.substring(1).toLowerCase(); } - - // TODO: This is not the way to do logging. Fix later, possibly with slf4j annotation. Need to read up on the subject. - public static Logger getLogger(String name) { - Logger log = Logger.getLogger(name); - ConsoleHandler handler = new ConsoleHandler(); - - handler.setFormatter(new SimpleFormatter() { - @Override - public synchronized String format(LogRecord lr) { - return lr.getMessage() + "\r\n"; - } - }); - - log.setUseParentHandlers(false); - log.addHandler(handler); - - return log; - } } diff --git a/src/main/java/com/eternitywall/ots/attestation/BitcoinBlockHeaderAttestation.java b/src/main/java/com/eternitywall/ots/attestation/BitcoinBlockHeaderAttestation.java index 060deb1..b23a0ff 100644 --- a/src/main/java/com/eternitywall/ots/attestation/BitcoinBlockHeaderAttestation.java +++ b/src/main/java/com/eternitywall/ots/attestation/BitcoinBlockHeaderAttestation.java @@ -7,7 +7,6 @@ import com.eternitywall.ots.exceptions.VerificationException; import java.util.Arrays; -import java.util.logging.Logger; /** * Bitcoin Block Header Attestation. @@ -37,7 +36,6 @@ public class BitcoinBlockHeaderAttestation extends TimeAttestation { public static byte[] _TAG = {(byte) 0x05, (byte) 0x88, (byte) 0x96, (byte) 0x0d, (byte) 0x73, (byte) 0xd7, (byte) 0x19, (byte) 0x01}; - private static Logger log = Utils.getLogger(BitcoinBlockHeaderAttestation.class.getName()); public static String chain = "bitcoin"; @Override diff --git a/src/main/java/com/eternitywall/ots/attestation/EthereumBlockHeaderAttestation.java b/src/main/java/com/eternitywall/ots/attestation/EthereumBlockHeaderAttestation.java index ac58f0b..4135135 100644 --- a/src/main/java/com/eternitywall/ots/attestation/EthereumBlockHeaderAttestation.java +++ b/src/main/java/com/eternitywall/ots/attestation/EthereumBlockHeaderAttestation.java @@ -2,10 +2,8 @@ import com.eternitywall.ots.StreamDeserializationContext; import com.eternitywall.ots.StreamSerializationContext; -import com.eternitywall.ots.Utils; import java.util.Arrays; -import java.util.logging.Logger; /** * Ethereum Block Header Attestation. @@ -15,7 +13,6 @@ public class EthereumBlockHeaderAttestation extends TimeAttestation { public static byte[] _TAG = {(byte) 0x30, (byte) 0xfe, (byte) 0x80, (byte) 0x87, (byte) 0xb5, (byte) 0xc7, (byte) 0xea, (byte) 0xd7}; - private static Logger log = Utils.getLogger(EthereumBlockHeaderAttestation.class.getName()); public static String chain = "ethereum"; @Override diff --git a/src/main/java/com/eternitywall/ots/attestation/LitecoinBlockHeaderAttestation.java b/src/main/java/com/eternitywall/ots/attestation/LitecoinBlockHeaderAttestation.java index fd0b5be..27f3450 100644 --- a/src/main/java/com/eternitywall/ots/attestation/LitecoinBlockHeaderAttestation.java +++ b/src/main/java/com/eternitywall/ots/attestation/LitecoinBlockHeaderAttestation.java @@ -7,7 +7,6 @@ import com.eternitywall.ots.exceptions.VerificationException; import java.util.Arrays; -import java.util.logging.Logger; /** * Litecoin Block Header Attestation. @@ -17,7 +16,6 @@ public class LitecoinBlockHeaderAttestation extends TimeAttestation { public static byte[] _TAG = {(byte) 0x06, (byte) 0x86, (byte) 0x9a, (byte) 0x0d, (byte) 0x73, (byte) 0xd7, (byte) 0x1b, (byte) 0x45}; - private static Logger log = Utils.getLogger(LitecoinBlockHeaderAttestation.class.getName()); public static String chain = "litecoin"; @Override diff --git a/src/main/java/com/eternitywall/ots/attestation/PendingAttestation.java b/src/main/java/com/eternitywall/ots/attestation/PendingAttestation.java index 44af972..4079434 100644 --- a/src/main/java/com/eternitywall/ots/attestation/PendingAttestation.java +++ b/src/main/java/com/eternitywall/ots/attestation/PendingAttestation.java @@ -3,10 +3,11 @@ import com.eternitywall.ots.StreamDeserializationContext; import com.eternitywall.ots.StreamSerializationContext; import com.eternitywall.ots.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.nio.charset.StandardCharsets; import java.util.Arrays; -import java.util.logging.Logger; /** * Pending attestations. @@ -30,7 +31,7 @@ */ public class PendingAttestation extends TimeAttestation { - private static Logger log = Utils.getLogger(PendingAttestation.class.getName()); + private static final Logger log = LoggerFactory.getLogger(PendingAttestation.class); public static byte[] _TAG = {(byte) 0x83, (byte) 0xdf, (byte) 0xe3, (byte) 0x0d, (byte) 0x2e, (byte) 0xf9, (byte) 0x0c, (byte) 0x8e}; @@ -65,7 +66,7 @@ public static boolean checkUri(byte[] uri) { Character c = String.format("%c", uri[i]).charAt(0); if (PendingAttestation._ALLOWED_URI_CHARS.indexOf(c) < 0) { - log.severe("URI contains invalid character "); + log.warn("URI contains invalid character: {}", c); return false; } @@ -78,7 +79,7 @@ public static PendingAttestation deserialize(StreamDeserializationContext ctxPay byte[] utf8Uri = ctxPayload.readVarbytes(PendingAttestation._MAX_URI_LENGTH); if (PendingAttestation.checkUri(utf8Uri) == false) { - log.severe("Invalid URI: "); + log.warn("Invalid URI: "); return null; } diff --git a/src/main/java/com/eternitywall/ots/attestation/TimeAttestation.java b/src/main/java/com/eternitywall/ots/attestation/TimeAttestation.java index b2f52d1..6dcb410 100644 --- a/src/main/java/com/eternitywall/ots/attestation/TimeAttestation.java +++ b/src/main/java/com/eternitywall/ots/attestation/TimeAttestation.java @@ -3,17 +3,16 @@ import com.eternitywall.ots.StreamDeserializationContext; import com.eternitywall.ots.StreamSerializationContext; import com.eternitywall.ots.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Arrays; -import java.util.logging.Logger; /** * Class representing {@link com.eternitywall.ots.Timestamp} signature verification */ public abstract class TimeAttestation implements Comparable { - private static Logger log = Utils.getLogger(TimeAttestation.class.getName()); - public static int _TAG_SIZE = 8; public static int _MAX_PAYLOAD_SIZE = 8192; diff --git a/src/main/java/com/eternitywall/ots/attestation/UnknownAttestation.java b/src/main/java/com/eternitywall/ots/attestation/UnknownAttestation.java index 4d4d038..17bb8d9 100644 --- a/src/main/java/com/eternitywall/ots/attestation/UnknownAttestation.java +++ b/src/main/java/com/eternitywall/ots/attestation/UnknownAttestation.java @@ -14,8 +14,6 @@ */ public class UnknownAttestation extends TimeAttestation { - private static Logger log = Utils.getLogger(UnknownAttestation.class.getName()); - byte[] payload; public static byte[] _TAG = new byte[]{}; diff --git a/src/main/java/com/eternitywall/ots/op/Op.java b/src/main/java/com/eternitywall/ots/op/Op.java index 68b7199..a347dd3 100644 --- a/src/main/java/com/eternitywall/ots/op/Op.java +++ b/src/main/java/com/eternitywall/ots/op/Op.java @@ -2,16 +2,15 @@ import com.eternitywall.ots.StreamDeserializationContext; import com.eternitywall.ots.StreamSerializationContext; -import com.eternitywall.ots.Utils; - -import java.util.logging.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Operations are the edges in the timestamp tree, with each operation taking a message and zero or more arguments to produce a result. */ public abstract class Op implements Comparable { - private static Logger log = Utils.getLogger(Op.class.getName()); + private static final Logger log = LoggerFactory.getLogger(Op.class); /** * Maximum length of an com.eternitywall.ots.op.Op result @@ -88,7 +87,7 @@ public static Op deserializeFromTag(StreamDeserializationContext ctx, byte tag) } else if (tag == OpKECCAK256._TAG) { return OpKECCAK256.deserializeFromTag(ctx, tag); } else { - log.severe("Unknown operation tag: " + tag + " 0x" + String.format("%02x", tag)); + log.warn("Unknown operation tag: {} 0x{}", tag, String.format("%02x", tag)); return null; // TODO: Is this OK? Won't it blow up later? Better to throw? } } @@ -100,7 +99,7 @@ public static Op deserializeFromTag(StreamDeserializationContext ctx, byte tag) */ public void serialize(StreamSerializationContext ctx) { if (this._TAG() == 0x00) { - log.severe("No valid serialized Op"); + log.warn("No valid serialized Op"); // TODO: Is it OK to just log and carry on? Won't it blow up later? Better to throw? } @@ -117,14 +116,14 @@ public void serialize(StreamSerializationContext ctx) { */ public byte[] call(byte[] msg) { if (msg.length > _MAX_MSG_LENGTH) { - log.severe("Error : Message too long;"); + log.warn("Error : Message too long;"); return new byte[]{}; // TODO: Is this OK? Won't it blow up later? Better to throw? } byte[] r = this.call(msg); if (r.length > _MAX_RESULT_LENGTH) { - log.severe("Error : Result too long;"); + log.warn("Error : Result too long;"); // TODO: Is it OK to just log and carry on? Won't it blow up later? Better to throw? } diff --git a/src/main/java/com/eternitywall/ots/op/OpAppend.java b/src/main/java/com/eternitywall/ots/op/OpAppend.java index b0940af..645ed4a 100644 --- a/src/main/java/com/eternitywall/ots/op/OpAppend.java +++ b/src/main/java/com/eternitywall/ots/op/OpAppend.java @@ -13,8 +13,6 @@ */ public class OpAppend extends OpBinary { - private static Logger log = Utils.getLogger(OpAppend.class.getName()); - byte[] arg; public static byte _TAG = (byte) 0xf0; diff --git a/src/main/java/com/eternitywall/ots/op/OpBinary.java b/src/main/java/com/eternitywall/ots/op/OpBinary.java index e3dff12..aa36f0d 100644 --- a/src/main/java/com/eternitywall/ots/op/OpBinary.java +++ b/src/main/java/com/eternitywall/ots/op/OpBinary.java @@ -3,9 +3,10 @@ import com.eternitywall.ots.StreamDeserializationContext; import com.eternitywall.ots.StreamSerializationContext; import com.eternitywall.ots.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Arrays; -import java.util.logging.Logger; /** * Operations that act on a message and a single argument. @@ -14,7 +15,7 @@ */ public abstract class OpBinary extends Op implements Comparable { - private static Logger log = Utils.getLogger(OpBinary.class.getName()); + private static final Logger log = LoggerFactory.getLogger(OpBinary.class); public byte[] arg; @@ -41,7 +42,7 @@ public static Op deserializeFromTag(StreamDeserializationContext ctx, byte tag) } else if (tag == OpPrepend._TAG) { return new OpPrepend(arg); } else { - log.severe("Unknown operation tag: " + tag + " 0x" + String.format("%02x", tag)); + log.warn("Unknown operation tag: {} 0x{}", tag, String.format("%02x", tag)); return null; // TODO: Is this OK? Won't it blow up later? Better to throw? } } diff --git a/src/main/java/com/eternitywall/ots/op/OpCrypto.java b/src/main/java/com/eternitywall/ots/op/OpCrypto.java index 4ea8820..d8e3081 100644 --- a/src/main/java/com/eternitywall/ots/op/OpCrypto.java +++ b/src/main/java/com/eternitywall/ots/op/OpCrypto.java @@ -1,7 +1,8 @@ package com.eternitywall.ots.op; import com.eternitywall.ots.StreamDeserializationContext; -import com.eternitywall.ots.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileInputStream; @@ -9,7 +10,6 @@ import java.io.InputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -import java.util.logging.Logger; /** * Cryptographic transformations. @@ -21,7 +21,7 @@ */ public class OpCrypto extends OpUnary { - private static Logger log = Utils.getLogger(OpCrypto.class.getName()); + private static final Logger log = LoggerFactory.getLogger(OpCrypto.class); public String _TAG_NAME = ""; @@ -50,8 +50,7 @@ public byte[] call(byte[] msg) { return hash; } catch (NoSuchAlgorithmException e) { - log.severe("NoSuchAlgorithmException"); - e.printStackTrace(); + log.error("No such algorithm", e); return new byte[]{}; // TODO: Is this OK? Won't it blow up later? Better to throw? } diff --git a/src/main/java/com/eternitywall/ots/op/OpKECCAK256.java b/src/main/java/com/eternitywall/ots/op/OpKECCAK256.java index 87118dd..1c6dbe2 100644 --- a/src/main/java/com/eternitywall/ots/op/OpKECCAK256.java +++ b/src/main/java/com/eternitywall/ots/op/OpKECCAK256.java @@ -1,11 +1,8 @@ package com.eternitywall.ots.op; import com.eternitywall.ots.StreamDeserializationContext; -import com.eternitywall.ots.Utils; import com.eternitywall.ots.crypto.KeccakDigest; -import java.util.logging.Logger; - /** * Cryptographic Keccak256 operation. * Cryptographic operation tag numbers taken from RFC4880, although it's not @@ -15,7 +12,6 @@ */ public class OpKECCAK256 extends OpCrypto { - private static Logger log = Utils.getLogger(OpKECCAK256.class.getName()); private KeccakDigest digest = new KeccakDigest(256); public static byte _TAG = (byte) 103; diff --git a/src/main/java/com/eternitywall/ots/op/OpPrepend.java b/src/main/java/com/eternitywall/ots/op/OpPrepend.java index 153348f..7077cc7 100644 --- a/src/main/java/com/eternitywall/ots/op/OpPrepend.java +++ b/src/main/java/com/eternitywall/ots/op/OpPrepend.java @@ -4,7 +4,6 @@ import com.eternitywall.ots.Utils; import java.util.Arrays; -import java.util.logging.Logger; /** * Prepend a prefix to a message. @@ -13,8 +12,6 @@ */ public class OpPrepend extends OpBinary { - private static Logger log = Utils.getLogger(OpPrepend.class.getName()); - byte[] arg; public static byte _TAG = (byte) 0xf1; diff --git a/src/main/java/com/eternitywall/ots/op/OpRIPEMD160.java b/src/main/java/com/eternitywall/ots/op/OpRIPEMD160.java index 093656b..6564f65 100644 --- a/src/main/java/com/eternitywall/ots/op/OpRIPEMD160.java +++ b/src/main/java/com/eternitywall/ots/op/OpRIPEMD160.java @@ -1,11 +1,8 @@ package com.eternitywall.ots.op; import com.eternitywall.ots.StreamDeserializationContext; -import com.eternitywall.ots.Utils; import com.eternitywall.ots.crypto.RIPEMD160Digest; -import java.util.logging.Logger; - /** * Cryptographic RIPEMD160 operation. * Cryptographic operation tag numbers taken from RFC4880, although it's not @@ -15,8 +12,6 @@ */ public class OpRIPEMD160 extends OpCrypto { - private static Logger log = Utils.getLogger(OpRIPEMD160.class.getName()); - public static byte _TAG = 0x03; @Override diff --git a/src/main/java/com/eternitywall/ots/op/OpSHA1.java b/src/main/java/com/eternitywall/ots/op/OpSHA1.java index 4f1c194..708d6c1 100644 --- a/src/main/java/com/eternitywall/ots/op/OpSHA1.java +++ b/src/main/java/com/eternitywall/ots/op/OpSHA1.java @@ -1,9 +1,6 @@ package com.eternitywall.ots.op; import com.eternitywall.ots.StreamDeserializationContext; -import com.eternitywall.ots.Utils; - -import java.util.logging.Logger; /** * Cryptographic SHA1 operation. @@ -20,8 +17,6 @@ */ public class OpSHA1 extends OpCrypto { - private static Logger log = Utils.getLogger(OpSHA1.class.getName()); - public static byte _TAG = 0x02; @Override diff --git a/src/main/java/com/eternitywall/ots/op/OpSHA256.java b/src/main/java/com/eternitywall/ots/op/OpSHA256.java index 02635c9..3fdc624 100644 --- a/src/main/java/com/eternitywall/ots/op/OpSHA256.java +++ b/src/main/java/com/eternitywall/ots/op/OpSHA256.java @@ -1,9 +1,6 @@ package com.eternitywall.ots.op; import com.eternitywall.ots.StreamDeserializationContext; -import com.eternitywall.ots.Utils; - -import java.util.logging.Logger; /** * Cryptographic SHA256 operation. @@ -14,8 +11,6 @@ */ public class OpSHA256 extends OpCrypto { - private static Logger log = Utils.getLogger(OpSHA256.class.getName()); - public static byte _TAG = 0x08; @Override diff --git a/src/main/java/com/eternitywall/ots/op/OpUnary.java b/src/main/java/com/eternitywall/ots/op/OpUnary.java index 1f36ca0..c0e6adc 100644 --- a/src/main/java/com/eternitywall/ots/op/OpUnary.java +++ b/src/main/java/com/eternitywall/ots/op/OpUnary.java @@ -1,9 +1,9 @@ package com.eternitywall.ots.op; import com.eternitywall.ots.StreamDeserializationContext; -import com.eternitywall.ots.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import java.util.logging.Logger; /** * Operations that act on a single message. @@ -12,7 +12,7 @@ */ public abstract class OpUnary extends Op { - private static Logger log = Utils.getLogger(OpUnary.class.getName()); + private static Logger log = LoggerFactory.getLogger(OpUnary.class); @Override public String _TAG_NAME() { @@ -33,7 +33,7 @@ public static Op deserializeFromTag(StreamDeserializationContext ctx, byte tag) } else if (tag == OpKECCAK256._TAG) { return new OpKECCAK256(); } else { - log.severe("Unknown operation tag: " + tag); + log.warn("Unknown operation tag: {}", tag); return null; // TODO: Is this OK? Won't it blow up later? Better to throw? } diff --git a/src/test/java/com/eternitywall/TestBitcoin.java b/src/test/java/com/eternitywall/TestBitcoin.java index 5682ece..c320455 100644 --- a/src/test/java/com/eternitywall/TestBitcoin.java +++ b/src/test/java/com/eternitywall/TestBitcoin.java @@ -2,19 +2,19 @@ import com.eternitywall.ots.BitcoinNode; import com.eternitywall.ots.BlockHeader; -import com.eternitywall.ots.Utils; import org.json.JSONObject; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Properties; -import java.util.logging.Logger; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; public class TestBitcoin { - private static Logger log = Utils.getLogger(TestBitcoin.class.getName()); + private static final Logger log = LoggerFactory.getLogger(TestBitcoin.class); @Test public void testBitcoin() { diff --git a/src/test/java/com/eternitywall/TestCalendar.java b/src/test/java/com/eternitywall/TestCalendar.java index 8245c25..dfd3486 100644 --- a/src/test/java/com/eternitywall/TestCalendar.java +++ b/src/test/java/com/eternitywall/TestCalendar.java @@ -9,6 +9,8 @@ import org.bitcoinj.core.ECKey; import org.bitcoinj.core.NetworkParameters; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.FileInputStream; import java.math.BigInteger; @@ -23,12 +25,11 @@ import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.logging.Logger; import static org.junit.Assert.*; public class TestCalendar { - private static Logger log = Utils.getLogger(TestCalendar.class.getName()); + private static final Logger log = LoggerFactory.getLogger(TestCalendar.class); @Test public void testSingle() throws Exception { @@ -220,7 +221,7 @@ public void testMulti() throws Exception { } if (count < m) { - log.severe("Failed to create timestamp: requested " + String.valueOf(m) + " attestation" + ((m > 1) ? "s" : "") + " but received only " + String.valueOf(count)); + log.warn("Failed to create timestamp: requested {} attestation{} but received only {}", m, (m > 1) ? "s" : "", count); } assertFalse(count < m); @@ -269,7 +270,7 @@ public void testMultiWithInvalidCalendar() throws Exception { } if (count < m) { - log.severe("Failed to create timestamp: requested " + String.valueOf(m) + " attestation" + ((m > 1) ? "s" : "") + " but received only " + String.valueOf(count)); + log.warn("Failed to create timestamp: requested {} attestations but received only {}", m, count); } assertFalse(count < m); diff --git a/src/test/java/com/eternitywall/ots/TestOtsCli.java b/src/test/java/com/eternitywall/ots/TestOtsCli.java index d4d1e70..2d32f8c 100644 --- a/src/test/java/com/eternitywall/ots/TestOtsCli.java +++ b/src/test/java/com/eternitywall/ots/TestOtsCli.java @@ -1,44 +1,60 @@ package com.eternitywall.ots; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; import org.junit.Test; +import org.slf4j.LoggerFactory; -import java.io.ByteArrayOutputStream; -import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.logging.SimpleFormatter; -import java.util.logging.StreamHandler; +import java.util.List; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertThat; public class TestOtsCli { @Test - public void testCommandLineHandlesUpgradeCommandWithWrongFileName() throws Exception { - StringLoggerForTest loggerForTest = new StringLoggerForTest(); + public void testCommandLineHandlesUpgradeCommandWithWrongFileName() { + ListAppender listAppender = new ListAppender<>(); + listAppender.start(); + Logger logger = (Logger) LoggerFactory.getLogger(OtsCli.class); + logger.addAppender(listAppender); OtsCli.main(new String[]{"upgrade", "some_non_existent_file.name"}); - final String logContents = loggerForTest.contents(); - assertTrue("Upgrade with non existent file should log 'No valid file' error", - logContents.contains("No valid file")); + assertThat("Upgrade with non existing file should log 'No valid file' error", + listAppender.list, hasLogMessage("No valid file")); } - private class StringLoggerForTest { - private ByteArrayOutputStream baos; - private StreamHandler sh; + private static HasLoggingEventMessage hasLogMessage(String message) { + return new HasLoggingEventMessage(message); + } + + private static class HasLoggingEventMessage extends BaseMatcher> { + + private final String message; + + private HasLoggingEventMessage(String message) { + this.message = message; + } - StringLoggerForTest() { - Logger logger = Utils.getLogger(OtsCli.class.getName()); - this.baos = new ByteArrayOutputStream(); - this.sh = new StreamHandler(baos, new SimpleFormatter()); - this.sh.setLevel(Level.ALL); - logger.addHandler(this.sh); + @Override + public boolean matches(Object o) { + if (o instanceof List) { + for (Object event : (List) o) { + if (event instanceof ILoggingEvent + && ((ILoggingEvent) event).getFormattedMessage().equals(message)) { + return true; + } + } + } + return false; } - String contents() throws Exception { - this.sh.flush(); - this.baos.close(); - return this.baos.toString(); + @Override + public void describeTo(Description description) { + description.appendText("to have log message '" + message + "'"); } } }