-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Grisu118/extend-logmessage
Extend logmessage
- Loading branch information
Showing
5 changed files
with
87 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |