Improved logging.

This commit is contained in:
Erik C. Thauvin 2017-05-17 18:13:54 -07:00
parent 3fb93ed955
commit d68d9895d8
4 changed files with 32 additions and 17 deletions

View file

@ -40,7 +40,7 @@ To install and run from Maven, configure an artifact as follows:
<dependency> <dependency>
<groupId>net.thauvin.erik</groupId> <groupId>net.thauvin.erik</groupId>
<artifactId>pinboard-poster</artifactId> <artifactId>pinboard-poster</artifactId>
<version>0.9.0</version> <version>0.9.1</version>
</dependency> </dependency>
``` ```
@ -50,7 +50,7 @@ To install and run from Gradle, add the following to the build.gradle file:
```gradle ```gradle
dependencies { 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 ```gradle
dependencies { 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: 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 ```kotlin
with(poster.logger) { with(poster.logger) {
addHandler(ConsoleHandler().apply { level = Level.FINE }) addHandler(ConsoleHandler().apply { level = Level.FINE })
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. or using a property file.

View file

@ -16,14 +16,15 @@ import java.io.FileInputStream
import java.util.* import java.util.*
val bs = buildScript { val bs = buildScript {
plugins("net.thauvin.erik:kobalt-versioneye:") plugins("net.thauvin.erik:kobalt-versioneye:", "net.thauvin.erik:kobalt-maven-local:")
} }
val p = project { val p = project {
name = "pinboard-poster" name = "pinboard-poster"
group = "net.thauvin.erik" group = "net.thauvin.erik"
description = "Pinboard Poster for Kotlin/Java"
artifactId = name artifactId = name
version = "0.9.0" version = "0.9.1"
val localProperties = Properties().apply { val localProperties = Properties().apply {
val f = "local.properties" val f = "local.properties"

View file

@ -4,9 +4,9 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>net.thauvin.erik</groupId> <groupId>net.thauvin.erik</groupId>
<artifactId>pinboard-poster</artifactId> <artifactId>pinboard-poster</artifactId>
<version>0.9.0</version> <version>0.9.1</version>
<name>pinboard-poster</name> <name>pinboard-poster</name>
<description></description> <description>Pinboard Poster for Kotlin/Java</description>
<url>https://github.com/ethauvin/pinboard-poster</url> <url>https://github.com/ethauvin/pinboard-poster</url>
<licenses> <licenses>
<license> <license>

View file

@ -66,9 +66,9 @@ open class PinboardPoster(val apiToken: String) {
toRead: Boolean = false): Boolean { toRead: Boolean = false): Boolean {
if (validate()) { if (validate()) {
if (!validateUrl(url)) { 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()) { } else if (description.isBlank()) {
logger.log(Level.SEVERE, "Please specify a valid description.") logger.severe("Please specify a valid description.")
} else { } else {
val params = listOf( val params = listOf(
Pair("url", url), Pair("url", url),
@ -90,7 +90,7 @@ open class PinboardPoster(val apiToken: String) {
fun deletePin(url: String): Boolean { fun deletePin(url: String): Boolean {
if (validate()) { if (validate()) {
if (!validateUrl(url)) { if (!validateUrl(url)) {
logger.log(Level.SEVERE, "Please specify a valid URL to delete.") logger.severe("Please specify a valid URL to delete.")
} else { } else {
return executeMethod("posts/delete", listOf(Pair("url", url))) 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 request = Request.Builder().url(httpUrl).build()
val result = client.newCall(request).execute() 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() val response = result.body()?.string()
if (response != null) { if (response != null) {
logger.log(Level.FINE, "HTTP Response:\n$response") logHttp(method, "HTTP Response:\n$response")
if (response.contains(Constants.DONE)) { if (response.contains(Constants.DONE)) {
return true return true
} else { } else {
@ -137,9 +137,9 @@ open class PinboardPoster(val apiToken: String) {
val code = document.getElementsByTagName("result")?.item(0)?.attributes?.getNamedItem("code")?.nodeValue val code = document.getElementsByTagName("result")?.item(0)?.attributes?.getNamedItem("code")?.nodeValue
if (code != null && code.isNotBlank()) { 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 { } else {
logger.log(Level.SEVERE, "An error has occurred while executing $method.") logger.severe("An error has occurred while executing $method.")
} }
} catch(e: Exception) { } catch(e: Exception) {
logger.log(Level.SEVERE, "Could not parse $method XML response.", e) logger.log(Level.SEVERE, "Could not parse $method XML response.", e)
@ -147,7 +147,7 @@ open class PinboardPoster(val apiToken: String) {
} }
} }
} else { } else {
logger.log(Level.SEVERE, "Invalid API end point: $apiEndPoint") logger.severe("Invalid API end point: $apiEndPoint")
} }
return false return false
@ -160,13 +160,17 @@ open class PinboardPoster(val apiToken: String) {
return "$apiEndPoint/$method" 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 { private fun validate(): Boolean {
if (apiToken.isBlank() && !apiToken.contains(':')) { 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 return false
} else if (!validateUrl(apiEndPoint)) { } 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 false
} }
return true return true