diff --git a/build.gradle b/build.gradle index 4993892..5e6e5e8 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ apply plugin: 'maven' apply plugin: 'maven-publish' group 'ch.grisu118.js' -version '0.2.1' +version '0.3.0' repositories { mavenCentral() 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 index 4467ac0..2a0025e 100644 --- a/src/main/kotlin/ch/grisu118/js/logger/ALogger.kt +++ b/src/main/kotlin/ch/grisu118/js/logger/ALogger.kt @@ -1,7 +1,5 @@ package ch.grisu118.js.logger -import kotlin.reflect.KFunction1 - abstract class ALogger(protected val name: String, protected var level: Level) : ILogger { override val isTrace: Boolean get() = level >= Level.TRACE @@ -15,8 +13,8 @@ abstract class ALogger(protected val name: String, protected var level: Level) : get() = level >= Level.ERROR protected fun log(level: Level, message: Any?) { - val msg = "$level [$name]: $message" - when(level) { + 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) @@ -26,8 +24,8 @@ abstract class ALogger(protected val name: String, protected var level: Level) : } protected fun log(level: Level, message: Any?, t: Throwable) { - val msg = "$level [$name]: $message" - when(level) { + 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) @@ -35,4 +33,5 @@ abstract class ALogger(protected val name: String, protected var level: Level) : else -> throw IllegalArgumentException("Invalid level $level") } } + } \ No newline at end of file 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