Skip to content

Commit

Permalink
Merge pull request #2 from Grisu118/extend-logmessage
Browse files Browse the repository at this point in the history
Extend logmessage
  • Loading branch information
Grisu118 authored Aug 20, 2017
2 parents 29a4454 + df90b04 commit ba7307a
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 25 deletions.
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.1.4'
ext.kotlin_version = '1.1.4-2'

repositories {
mavenCentral()
Expand All @@ -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()
Expand Down Expand Up @@ -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'
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/ch/grisu118/js/api/Date.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ch.grisu118.js.api

external class Date {
fun toISOString(): String
fun toTimeString(): String
}
37 changes: 37 additions & 0 deletions src/main/kotlin/ch/grisu118/js/logger/ALogger.kt
Original file line number Diff line number Diff line change
@@ -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")
}
}

}
32 changes: 11 additions & 21 deletions src/main/kotlin/ch/grisu118/js/logger/Logger.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/main/kotlin/ch/grisu118/js/logger/LoggerConfig.kt
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit ba7307a

Please sign in to comment.