Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

Why Kotlin's loop (for) not work correct with org.slf4j.Logger?

In my Kotlin project in folder src/resources/ I has file pairs_ids.txt.

Here code: This is a property file:

key=value

The count of all lines are 1389.

Here code that read content of this file line by line.

import org.slf4j.LoggerFactory
private val logger = LoggerFactory.getLogger("Exchange")

 private var allSymbolsIDsMap: Map<String, String> = mapOf()    
 val pairsIDs = getResourceAsText("/pairs_ids.txt")
 allSymbolsIDsMap = pairsIDs.split(",").associate {
 val (left, right) = it.split("=")
          left to right.toString()
 }
 logger.info("allSymbolsIDsMap_size = " + allSymbolsIDsMap.size)
 var countAllSymbolHandler = 0
 for ((key, value) in allSymbolsIDsMap) {
   countAllSymbolHandler++
   logger.info("countAllSymbolHandler = $countAllSymbolHandler")
}

private fun getResourceAsText(path: String): String {
    return object {}.javaClass.getResourceAsStream(path).bufferedReader().use { it.readText() }
}

Result:

Start project:

allSymbolsIDsMap_size = 1389
Start project - "countAllSymbolHandler =" print 1113 times

Again start project:

allSymbolsIDsMap_size = 1389
"countAllSymbolHandler =" print 242 times

If replace logger.info by simple println then success work. The count is always 1389.

Why loop (for) not work correct with logger?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try:

override fun run(configuration: AppConfig?, environment: Environment?) {
        val logger = LoggerFactory.getLogger(this::class.java)
        javaClass.getResourceAsStream("/pairs_ids.txt").bufferedReader().use { reader -> reader.readLines() }.forEach { line -> logger.info(line) }
    }

When playing with input/output streams, use Kotlin's use extension methods, and do all of your processing inside of the use block.

This will handle all opening and closing of the streams so that there are no leaks, or forgetting to close/flush etc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...