diff --git a/build.gradle b/build.gradle index d2cb410..5e6e5e8 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.kotlin_version = '1.1.4' + ext.kotlin_version = '1.1.4-2' repositories { mavenCentral() @@ -18,7 +18,7 @@ apply plugin: 'maven' apply plugin: 'maven-publish' group 'ch.grisu118.js' -version '0.2.0' +version '0.3.0' repositories { mavenCentral() @@ -53,8 +53,8 @@ repositories { } bintray { - user = project.property('bintrayUser') - key = project.property('bintrayApiKey') + user = project.hasProperty('bintrayUser') ? project.property('bintrayUser') : "" + key = project.hasProperty('bintrayApiKey') ? project.property('bintrayApiKey'): "" publications = ['Bintray'] pkg { repo = 'kotlin' diff --git a/src/main/kotlin/ch/grisu118/js/api/Date.kt b/src/main/kotlin/ch/grisu118/js/api/Date.kt new file mode 100644 index 0000000..7748146 --- /dev/null +++ b/src/main/kotlin/ch/grisu118/js/api/Date.kt @@ -0,0 +1,6 @@ +package ch.grisu118.js.api + +external class Date { + fun toISOString(): String + fun toTimeString(): String +} \ No newline at end of file diff --git a/src/main/kotlin/ch/grisu118/js/logger/ALogger.kt b/src/main/kotlin/ch/grisu118/js/logger/ALogger.kt new file mode 100644 index 0000000..2a0025e --- /dev/null +++ b/src/main/kotlin/ch/grisu118/js/logger/ALogger.kt @@ -0,0 +1,37 @@ +package ch.grisu118.js.logger + +abstract class ALogger(protected val name: String, protected var level: Level) : ILogger { + override val isTrace: Boolean + get() = level >= Level.TRACE + override val isDebug: Boolean + get() = level >= Level.DEBUG + override val isInfo: Boolean + get() = level >= Level.INFO + override val isWarn: Boolean + get() = level >= Level.WARN + override val isError: Boolean + get() = level >= Level.ERROR + + protected fun log(level: Level, message: Any?) { + val msg = LoggerConfig.replace(level = level, message = message, name = name) + when (level) { + Level.TRACE, Level.DEBUG -> console.log(msg) + Level.INFO -> console.info(msg) + Level.WARN -> console.warn(msg) + Level.ERROR -> console.error(msg) + else -> throw IllegalArgumentException("Invalid level $level") + } + } + + protected fun log(level: Level, message: Any?, t: Throwable) { + val msg = LoggerConfig.replace(level = level, message = message, name = name) + when (level) { + Level.TRACE, Level.DEBUG -> console.log(msg, t) + Level.INFO -> console.info(msg, t) + Level.WARN -> console.warn(msg, t) + Level.ERROR -> console.error(msg, t) + else -> throw IllegalArgumentException("Invalid level $level") + } + } + +} \ No newline at end of file diff --git a/src/main/kotlin/ch/grisu118/js/logger/Logger.kt b/src/main/kotlin/ch/grisu118/js/logger/Logger.kt index de42008..6f2a8f4 100644 --- a/src/main/kotlin/ch/grisu118/js/logger/Logger.kt +++ b/src/main/kotlin/ch/grisu118/js/logger/Logger.kt @@ -1,74 +1,64 @@ package ch.grisu118.js.logger -class Logger(private val name: String, private var level: Level) : ILogger { - override val isTrace: Boolean - get() = level >= Level.TRACE - override val isDebug: Boolean - get() = level >= Level.DEBUG - override val isInfo: Boolean - get() = level >= Level.INFO - override val isWarn: Boolean - get() = level >= Level.WARN - override val isError: Boolean - get() = level >= Level.ERROR +class Logger(name: String, level: Level) : ALogger(name, level) { override fun trace(message: Any?) { if (isTrace) { - console.log("$level [$name]: ", message) + log(Level.TRACE, message) } } override fun debug(message: Any?) { if (isDebug) { - console.log("$level [$name]: ", message) + log(Level.DEBUG, message) } } override fun info(message: Any?) { if (isInfo) { - console.info("$level [$name]: ", message) + log(Level.INFO, message) } } override fun warn(message: Any?) { if (isWarn) { - console.warn("$level [$name]: ", message) + log(Level.WARN, message) } } override fun error(message: Any?) { if (isError) { - console.error("$level [$name]: ", message) + log(Level.ERROR, message) } } override fun trace(t: Throwable, message: Any?) { if (isTrace) { - console.log("$level [$name]: ", message, t) + log(Level.TRACE, message, t) } } override fun debug(t: Throwable, message: Any?) { if (isDebug) { - console.log("$level [$name]: ", message, t) + log(Level.DEBUG, message, t) } } override fun info(t: Throwable, message: Any?) { if (isInfo) { - console.info("$level [$name]: ", message, t) + log(Level.INFO, message, t) } } override fun warn(t: Throwable, message: Any?) { if (isWarn) { - console.warn("$level [$name]: ", message, t) + log(Level.WARN, message, t) } } override fun error(t: Throwable, message: Any?) { if (isError) { - console.error("$level [$name]: ", message, t) + log(Level.ERROR, message, t) } } diff --git a/src/main/kotlin/ch/grisu118/js/logger/LoggerConfig.kt b/src/main/kotlin/ch/grisu118/js/logger/LoggerConfig.kt new file mode 100644 index 0000000..6ed7034 --- /dev/null +++ b/src/main/kotlin/ch/grisu118/js/logger/LoggerConfig.kt @@ -0,0 +1,29 @@ +package ch.grisu118.js.logger + +import ch.grisu118.js.api.Date + +object LoggerConfig { + + /** + * The template for the log messages. + * The following Placeholders exists: + * + **%l** - Will be replaced with the level of this message + * + **%n** - Will be replaced with the name of the logger + * + **%m** - Will be replaced with the message + * + **%t** - Will be replaced with the current time + * + **%dt** - Will be replaced with the iso datetime string + * + **%%** - Will be replaced with with a single `%` + */ + var messageTemplate = "%l [%n]: %m" + + internal fun replace(level: Level, name: String, message: Any?): String { + val date = Date() + var msg = messageTemplate.replace("%l", level.toString()) + msg = msg.replace("%n", name) + msg = msg.replace("%m", message.toString()) + msg = msg.replace("%dt", date.toISOString()) + msg = msg.replace("%t", date.toTimeString()) + msg = msg.replace("%%", "%") + return msg + } +} \ No newline at end of file