Skip to content

Commit bd987bc

Browse files
committed
add LOGGER
1 parent f90bf15 commit bd987bc

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

KtorKMPFileCaching/src/browserStorageSystemMain/kotlin/fr/frankois944/ktorfilecaching/KtorFileCaching.localStorageSystemMain.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import io.ktor.client.plugins.cache.storage.CacheStorage
66
import io.ktor.client.plugins.cache.storage.CachedResponseData
77
import io.ktor.http.Url
88
import io.ktor.util.collections.ConcurrentMap
9+
import io.ktor.util.logging.KtorSimpleLogger
10+
import io.ktor.util.logging.trace
911
import kotlinx.coroutines.CoroutineDispatcher
1012
import kotlinx.coroutines.coroutineScope
1113
import kotlinx.coroutines.launch
@@ -46,6 +48,9 @@ internal class FileCacheStorage(
4648
) : CacheStorage {
4749
private val mutexes = ConcurrentMap<String, Mutex>()
4850

51+
@Suppress("ktlint:standard:property-naming")
52+
private val LOGGER = KtorSimpleLogger("KtorFileCaching")
53+
4954
override suspend fun store(
5055
url: Url,
5156
data: CachedResponseData,
@@ -115,8 +120,7 @@ internal class FileCacheStorage(
115120
if (Database.getItem("${prefix}_$urlHex") == null) return@withLock
116121
Database.removeItem("${prefix}_$urlHex")
117122
} catch (cause: Exception) {
118-
println("Exception during cache deletion in a file: ${cause.stackTraceToString()}")
119-
// LOGGER.trace { "Exception during cache deletion in a file: ${cause.stackTraceToString()}" }
123+
LOGGER.trace { "Exception during cache deletion in a file: ${cause.stackTraceToString()}" }
120124
}
121125
}
122126
}
@@ -129,8 +133,7 @@ internal class FileCacheStorage(
129133
val serializedData = Cbor.encodeToHexString(caches.map { SerializableCachedResponseData(it) })
130134
Database.setItem("${prefix}_$urlHex", serializedData)
131135
} catch (cause: Exception) {
132-
// LOGGER.trace { "Exception during saving a cache to a file: ${cause.stackTraceToString()}" }
133-
println("Exception during saving a cache to a file: ${cause.stackTraceToString()}")
136+
LOGGER.trace { "Exception during saving a cache to a file: ${cause.stackTraceToString()}" }
134137
}
135138
}
136139

@@ -139,8 +142,7 @@ internal class FileCacheStorage(
139142
val item = Database.getItem("${prefix}_$urlHex") ?: return emptySet()
140143
Cbor.decodeFromHexString<Set<SerializableCachedResponseData>>(item).map { it.cachedResponseData }.toSet()
141144
} catch (cause: Exception) {
142-
// LOGGER.trace { "Exception during cache lookup in a file: ${cause.stackTraceToString()}" }
143-
println("Exception during cache lookup in a file: ${cause.stackTraceToString()}")
145+
LOGGER.trace { "Exception during cache lookup in a file: ${cause.stackTraceToString()}" }
144146
emptySet()
145147
}
146148
}

KtorKMPFileCaching/src/okioFileSystemMain/kotlin/fr/frankois944/ktorfilecaching/KtorFileCaching.okioFileSystem.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import io.ktor.client.plugins.cache.storage.CacheStorage
66
import io.ktor.client.plugins.cache.storage.CachedResponseData
77
import io.ktor.http.Url
88
import io.ktor.util.collections.ConcurrentMap
9+
import io.ktor.util.logging.KtorSimpleLogger
10+
import io.ktor.util.logging.trace
911
import kotlinx.coroutines.CoroutineDispatcher
1012
import kotlinx.coroutines.Dispatchers
1113
import kotlinx.coroutines.coroutineScope
@@ -43,6 +45,9 @@ internal class FileCacheStorage(
4345
private val mutexes = ConcurrentMap<String, Mutex>()
4446
private val baseDir = "$directoryPath${Path.DIRECTORY_SEPARATOR}$storedCacheDirectory"
4547

48+
@Suppress("ktlint:standard:property-naming")
49+
private val LOGGER = KtorSimpleLogger("KtorFileCaching")
50+
4651
init {
4752
fileSystem.createDirectories(baseDir.toPath())
4853
}
@@ -117,8 +122,7 @@ internal class FileCacheStorage(
117122
if (!fileSystem.exists(filePath)) return@withLock
118123
fileSystem.delete(filePath)
119124
} catch (cause: Exception) {
120-
println("Exception during cache deletion in a file: ${cause.stackTraceToString()}")
121-
// LOGGER.trace { "Exception during cache deletion in a file: ${cause.stackTraceToString()}" }
125+
LOGGER.trace { "Exception during cache deletion in a file: ${cause.stackTraceToString()}" }
122126
}
123127
}
124128
}
@@ -134,8 +138,7 @@ internal class FileCacheStorage(
134138
fileSystem.write(filePath) { write(serializedData) }
135139
}
136140
} catch (cause: Exception) {
137-
// LOGGER.trace { "Exception during saving a cache to a file: ${cause.stackTraceToString()}" }
138-
println("Exception during saving a cache to a file: ${cause.stackTraceToString()}")
141+
LOGGER.trace { "Exception during saving a cache to a file: ${cause.stackTraceToString()}" }
139142
}
140143
}
141144

@@ -146,8 +149,7 @@ internal class FileCacheStorage(
146149
val bytes = fileSystem.read(filePath) { readByteArray() }
147150
Cbor.decodeFromByteArray<Set<SerializableCachedResponseData>>(bytes).map { it.cachedResponseData }.toSet()
148151
} catch (cause: Exception) {
149-
// LOGGER.trace { "Exception during cache lookup in a file: ${cause.stackTraceToString()}" }
150-
println("Exception during cache lookup in a file: ${cause.stackTraceToString()}")
152+
LOGGER.trace { "Exception during cache lookup in a file: ${cause.stackTraceToString()}" }
151153
emptySet()
152154
}
153155
}

0 commit comments

Comments
 (0)