Skip to content

Commit

Permalink
Merge pull request #1 from Grisu118/allow-loglevel-update
Browse files Browse the repository at this point in the history
Allow loglevel update
  • Loading branch information
Grisu118 authored Aug 17, 2017
2 parents 4536ff0 + 57a1e72 commit 29a4454
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
15 changes: 9 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# KotlinJS Logger

[![Release](https://jitpack.io/v/Grisu118/jslogger.svg?style=flat-square)](https://jitpack.io/#Grisu118/jslogger)
[ ![Release](https://api.bintray.com/packages/grisu118/kotlin/jslogger/images/download.svg) ](https://bintray.com/grisu118/kotlin/jslogger/_latestVersion)

Simple Logger for KotlinJS

## Usage

```kotlin
````kotlin
class XY {
val logger = LoggerFactory.logger(this)

Expand All @@ -16,9 +15,9 @@ class XY {
logger.info{ "Will be evaluated only if info or lower level is active" }
}
}
```
````

### Loglevel
### Available Loglevels
* Trace
* Debug
* Info
Expand All @@ -28,5 +27,9 @@ class XY {

### Change Loglevel

Call `LogFactory.loglevel(Regex(".*"), Level.DEBUG)` before creating the logger.
The highest level from all matching levels is used!
* `LoggerFactory.loglevel(Regex("XY"), Level.DEBUG)`
* you can set the loglevel for all loggers created after this call and matching the regex.
* `LoggerFactory.updateLevel(Regex("XY"), Level.TRACE)`
* in addition to the effect of the above method. All existing and matching loggers will change their level.

In both cases, the logger will use the highest level from all matching levels.
20 changes: 18 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ apply plugin: 'maven'
apply plugin: 'maven-publish'

group 'ch.grisu118.js'
version '0.1.1'
version '0.2.0'

repositories {
mavenCentral()
Expand All @@ -32,11 +32,26 @@ compileKotlin2Js {
kotlinOptions.metaInfo = true
kotlinOptions.outputFile = "${projectDir}/build/classes/main/jslogger.js"
kotlinOptions.sourceMap = true
kotlinOptions.moduleKind = 'commonjs'
kotlinOptions.moduleKind = 'umd'
}

jar.dependsOn compileKotlin2Js

// custom tasks for creating source/javadoc jars
task("sourcesJar", type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}

// add javadoc/source jar tasks as artifacts
artifacts {
archives sourcesJar
}

repositories {
jcenter()
}

bintray {
user = project.property('bintrayUser')
key = project.property('bintrayApiKey')
Expand All @@ -62,6 +77,7 @@ publishing {
groupId project.group
artifactId 'jslogger'
version project.version
artifact sourcesJar
}
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/ch/grisu118/js/logger/ILogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ interface ILogger {
fun error(t: Throwable, msg: () -> Any?) {
if (isError) error(t, msg())
}

/**
* Update the current loglevel for this logger.
*/
fun levelUpdate(level: Level)
}
6 changes: 5 additions & 1 deletion src/main/kotlin/ch/grisu118/js/logger/Logger.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ch.grisu118.js.logger

class Logger(private val name: String, private val level: Level) : ILogger {
class Logger(private val name: String, private var level: Level) : ILogger {
override val isTrace: Boolean
get() = level >= Level.TRACE
override val isDebug: Boolean
Expand Down Expand Up @@ -72,4 +72,8 @@ class Logger(private val name: String, private val level: Level) : ILogger {
}
}

override fun levelUpdate(level: Level) {
this.level = level
}

}
19 changes: 16 additions & 3 deletions src/main/kotlin/ch/grisu118/js/logger/LoggerFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package ch.grisu118.js.logger
object LoggerFactory {

private val defaultLevel = Level.INFO
private val levels = mutableSetOf<Pair<Regex, Level>>()
private val levels = mutableMapOf<Regex, Level>()
private val loggers = mutableMapOf<String, Logger>()

/**
Expand Down Expand Up @@ -42,9 +42,22 @@ object LoggerFactory {
* @param level the level for the associated loggers.
*/
fun loglevel(regex: Regex, level: Level) {
levels.add(regex to level)
levels.put(regex, level)
}

private fun level(name: String) = levels.filter { it.first.matches(name) }.maxBy { it.second }?.second ?: defaultLevel
/**
* Set the level for all loggers.
* If multiple regex matches, then the highest seen level is used.
* @param regex the regex to identify associated loggers.
* @param level the level for the associated loggers.
*/
fun updateLevel(regex: Regex, level: Level) {
levels.put(regex, level)
loggers.forEach {
it.value.levelUpdate(LoggerFactory.level(it.key))
}
}

private fun level(name: String) = levels.filter { it.key.matches(name) }.maxBy { it.value }?.value ?: defaultLevel

}

0 comments on commit 29a4454

Please sign in to comment.