Skip to content

Commit

Permalink
Add ability to log to multiple tags
Browse files Browse the repository at this point in the history
See issue tinylog-org#201.
  • Loading branch information
kahgoh committed Jul 8, 2021
1 parent 5679944 commit 2d93679
Show file tree
Hide file tree
Showing 16 changed files with 3,111 additions and 1,107 deletions.
35 changes: 24 additions & 11 deletions tinylog-api-kotlin/src/main/kotlin/org/tinylog/kotlin/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ object Logger {
// @formatter:on

private val instance = TaggedLogger(null)
private val loggers = ConcurrentHashMap<String, TaggedLogger>()
private val loggers = ConcurrentHashMap<Set<String?>, TaggedLogger>()

init {
loggers[setOf(null)] = instance
}

/**
* Gets a tagged logger instance. Tags are case-sensitive.
Expand All @@ -49,17 +53,26 @@ object Logger {
* @return Logger instance
*/
fun tag(tag: String?): TaggedLogger {
if (tag == null || tag.isEmpty()) {
return instance
return if (tag == null || tag.isEmpty()) {
instance
} else {
tags(tag)
}
}

/**
* Gets a tagged logger instance that logs to multiple tags. Tags are case-sensitive.
*
* @param tags
* Tags for the logger or nothing for an untagged logger. If specified, each tag should be unique
* @return Logger instance
*/
fun tags(vararg tags: String?): TaggedLogger {
return if (tags == null || tags.isEmpty()) {
instance
} else {
var logger = loggers[tag]
if (logger == null) {
logger = TaggedLogger(tag)
val existing = loggers.putIfAbsent(tag, logger)
return existing ?: logger
} else {
return logger
}
val tagsSet = tags.map { if (it.isNullOrEmpty()) { null } else { it } }.toSet()
loggers.computeIfAbsent(tagsSet) { TaggedLogger(it) }
}
}

Expand Down
Loading

0 comments on commit 2d93679

Please sign in to comment.