Only log error if severe logging level is enabled.

This commit is contained in:
Erik C. Thauvin 2021-07-27 15:01:08 -07:00
parent d5705c57aa
commit 05195d792d
6 changed files with 140 additions and 103 deletions

9
.idea/bitly-shorten.iml generated Normal file
View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="bitly-shorten" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml generated Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/bitly-shorten.iml" filepath="$PROJECT_DIR$/.idea/bitly-shorten.iml" />
</modules>
</component>
</project>

View file

@ -32,6 +32,7 @@
package net.thauvin.erik.bitly
import net.thauvin.erik.bitly.Utils.Companion.isSevereLoggable
import net.thauvin.erik.bitly.Utils.Companion.isValidUrl
import net.thauvin.erik.bitly.Utils.Companion.removeHttp
import net.thauvin.erik.bitly.Utils.Companion.toEndPoint
@ -174,10 +175,12 @@ open class Bitlinks(private val accessToken: String) {
try {
parsed = JSONObject(response.body).getString(key, default)
} catch (jse: JSONException) {
if (Utils.logger.isSevereLoggable()) {
Utils.logger.log(Level.SEVERE, "An error occurred parsing the response from Bitly.", jse)
}
}
}
}
return parsed
}
@ -202,7 +205,9 @@ open class Bitlinks(private val accessToken: String) {
): String {
var bitlink = if (toJson) Constants.EMPTY_JSON else long_url
if (!long_url.isValidUrl()) {
if (Utils.logger.isSevereLoggable()) {
Utils.logger.severe("Please specify a valid URL to shorten.")
}
} else {
val params = mutableMapOf("long_url" to long_url).apply {
if (group_guid.isNotBlank()) put("group_guid", group_guid)

View file

@ -35,7 +35,7 @@ package net.thauvin.erik.bitly
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import java.util.Properties
import java.util.*
/**
* Provides access to the [Bitly API v4](https://dev.bitly.com/api-reference).

View file

@ -50,7 +50,7 @@ import java.util.logging.Logger
open class Utils private constructor() {
companion object {
/** The logger instance. */
val logger: Logger by lazy { Logger.getLogger(Bitly::class.java.simpleName) }
val logger: Logger by lazy { Logger.getLogger(Bitly::class.java.name) }
/**
* Executes an API call.
@ -122,6 +122,7 @@ open class Utils private constructor() {
if (!result.isSuccessful && body.isNotEmpty()) {
try {
with(JSONObject(body)) {
if (logger.isSevereLoggable()) {
if (has("message")) {
logger.severe(getString("message") + " (${result.code})")
}
@ -129,7 +130,9 @@ open class Utils private constructor() {
logger.severe(getString("description"))
}
}
}
} catch (jse: JSONException) {
if (logger.isSevereLoggable()) {
logger.log(
Level.SEVERE,
"An error occurred parsing the error response from Bitly. [$endPoint]",
@ -137,11 +140,17 @@ open class Utils private constructor() {
)
}
}
}
return body
}
return Constants.EMPTY
}
/**
* Is [Level.SEVERE] logging enabled.
*/
fun Logger.isSevereLoggable(): Boolean = this.isLoggable(Level.SEVERE)
/**
* Validates a URL.
*/
@ -151,7 +160,9 @@ open class Utils private constructor() {
URL(this)
return true
} catch (e: MalformedURLException) {
logger.log(Level.FINE, "Invalid URL: $this", e)
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "Invalid URL: $this", e)
}
}
}
return false
@ -177,8 +188,12 @@ open class Utils private constructor() {
private fun validateCall(accessToken: String, endPoint: String): Boolean {
when {
endPoint.isBlank() -> logger.severe("Please specify a valid API endpoint.")
accessToken.isBlank() -> logger.severe("Please specify a valid API access token.")
endPoint.isBlank() -> {
if (logger.isSevereLoggable()) logger.severe("Please specify a valid API endpoint.")
}
accessToken.isBlank() -> {
if (logger.isSevereLoggable()) logger.severe("Please specify a valid API access token.")
}
else -> return true
}
return false