From d68d9895d8e7845872bb137260944639761505d8 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Wed, 17 May 2017 18:13:54 -0700 Subject: [PATCH] Improved logging. --- README.md | 16 ++++++++++--- kobalt/src/Build.kt | 5 ++-- pom.xml | 4 ++-- .../thauvin/erik/pinboard/PinboardPoster.kt | 24 +++++++++++-------- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 9704932..5c12077 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ To install and run from Maven, configure an artifact as follows: net.thauvin.erik pinboard-poster - 0.9.0 + 0.9.1 ``` @@ -50,7 +50,7 @@ To install and run from Gradle, add the following to the build.gradle file: ```gradle dependencies { - compileOnly 'net.thauvin.erik:pinboard-poster:0.9.0' + compileOnly 'net.thauvin.erik:pinboard-poster:0.9.1' } ``` @@ -60,7 +60,7 @@ To install and run from Kobalt, add the following to the Build.kt file: ```gradle dependencies { - compile("net.thauvin.erik:pinboard-poster:0.9.0") + compile("net.thauvin.erik:pinboard-poster:0.9.1") } ``` @@ -97,12 +97,22 @@ It returns `true` if the bookmark was deleted successfully, `false` otherwise. The library used [`java.util.logging`](https://docs.oracle.com/javase/8/docs/api/java/util/logging/package-summary.html) to log errors. Logging can be configured as follows: +#### Kotlin ```kotlin with(poster.logger) { addHandler(ConsoleHandler().apply { level = Level.FINE }) level = Level.FINE } ``` +#### Java +```java +final ConsoleHandler consoleHandler = new ConsoleHandler(); +consoleHandler.setLevel(Level.FINE); +final Logger logger = poster.getLogger(); +logger.addHandler(consoleHandler); +logger.setLevel(Level.FINE); +``` + or using a property file. diff --git a/kobalt/src/Build.kt b/kobalt/src/Build.kt index 28f2314..d036bc0 100644 --- a/kobalt/src/Build.kt +++ b/kobalt/src/Build.kt @@ -16,14 +16,15 @@ import java.io.FileInputStream import java.util.* val bs = buildScript { - plugins("net.thauvin.erik:kobalt-versioneye:") + plugins("net.thauvin.erik:kobalt-versioneye:", "net.thauvin.erik:kobalt-maven-local:") } val p = project { name = "pinboard-poster" group = "net.thauvin.erik" + description = "Pinboard Poster for Kotlin/Java" artifactId = name - version = "0.9.0" + version = "0.9.1" val localProperties = Properties().apply { val f = "local.properties" diff --git a/pom.xml b/pom.xml index 1e95a42..a2889ba 100644 --- a/pom.xml +++ b/pom.xml @@ -4,9 +4,9 @@ 4.0.0 net.thauvin.erik pinboard-poster - 0.9.0 + 0.9.1 pinboard-poster - + Pinboard Poster for Kotlin/Java https://github.com/ethauvin/pinboard-poster diff --git a/src/main/kotlin/net/thauvin/erik/pinboard/PinboardPoster.kt b/src/main/kotlin/net/thauvin/erik/pinboard/PinboardPoster.kt index 5f9fca5..443f0d6 100644 --- a/src/main/kotlin/net/thauvin/erik/pinboard/PinboardPoster.kt +++ b/src/main/kotlin/net/thauvin/erik/pinboard/PinboardPoster.kt @@ -66,9 +66,9 @@ open class PinboardPoster(val apiToken: String) { toRead: Boolean = false): Boolean { if (validate()) { if (!validateUrl(url)) { - logger.log(Level.SEVERE, "Please specify a valid URL to pin.") + logger.severe("Please specify a valid URL to pin.") } else if (description.isBlank()) { - logger.log(Level.SEVERE, "Please specify a valid description.") + logger.severe("Please specify a valid description.") } else { val params = listOf( Pair("url", url), @@ -90,7 +90,7 @@ open class PinboardPoster(val apiToken: String) { fun deletePin(url: String): Boolean { if (validate()) { if (!validateUrl(url)) { - logger.log(Level.SEVERE, "Please specify a valid URL to delete.") + logger.severe("Please specify a valid URL to delete.") } else { return executeMethod("posts/delete", listOf(Pair("url", url))) } @@ -114,12 +114,12 @@ open class PinboardPoster(val apiToken: String) { val request = Request.Builder().url(httpUrl).build() val result = client.newCall(request).execute() - logger.log(Level.FINE, "HTTP Result: ${result.code()}") + logHttp(method, "HTTP Result: ${result.code()}") val response = result.body()?.string() if (response != null) { - logger.log(Level.FINE, "HTTP Response:\n$response") + logHttp(method, "HTTP Response:\n$response") if (response.contains(Constants.DONE)) { return true } else { @@ -137,9 +137,9 @@ open class PinboardPoster(val apiToken: String) { val code = document.getElementsByTagName("result")?.item(0)?.attributes?.getNamedItem("code")?.nodeValue if (code != null && code.isNotBlank()) { - logger.log(Level.SEVERE, "An error has occurred while executing $method: $code") + logger.severe("An error has occurred while executing $method: $code") } else { - logger.log(Level.SEVERE, "An error has occurred while executing $method.") + logger.severe("An error has occurred while executing $method.") } } catch(e: Exception) { logger.log(Level.SEVERE, "Could not parse $method XML response.", e) @@ -147,7 +147,7 @@ open class PinboardPoster(val apiToken: String) { } } } else { - logger.log(Level.SEVERE, "Invalid API end point: $apiEndPoint") + logger.severe("Invalid API end point: $apiEndPoint") } return false @@ -160,13 +160,17 @@ open class PinboardPoster(val apiToken: String) { return "$apiEndPoint/$method" } } + + private fun logHttp(method: String, msg: String) { + logger.logp(Level.FINE, PinboardPoster::class.java.name, "executeMethod($method)", msg) + } private fun validate(): Boolean { if (apiToken.isBlank() && !apiToken.contains(':')) { - logger.log(Level.SEVERE, "Please specify a valid API token. (eg. user:TOKEN)") + logger.severe("Please specify a valid API token. (eg. user:TOKEN)") return false } else if (!validateUrl(apiEndPoint)) { - logger.log(Level.SEVERE, "Please specify a valid API end point. (eg. ${Constants.API_ENDPOINT})") + logger.severe("Please specify a valid API end point. (eg. ${Constants.API_ENDPOINT})") return false } return true