Logging from another thread on iOS
See original GitHub issueHi. I’ve tried to call Napier.v on iOS inside of withContext(Dispatchers.Default) i.e. from a non-main thread and didn’t get anything logged. I see you use ThreadLocal annotation for Napier. And it seems on iOS I need to initialize Napier separately for Dispatchers.Default thread. Probably it worth adding support for logging from a non-main thread by default as now it looks like a bug.
In this example, I see only the first message logged on iOS. But the both messages are logged on Android. If I add Napier initialization inside of Dispatchers.Default, the both messages will be logged on iOS too:
suspend fun loadDefinition(word: String): OwlBotWord {
Napier.v("Loading definition for: $word", tag = TAG)
val res: HttpResponse = httpClient.get("${baseUrl}api/v4/dictionary/${word}")
return withContext(Dispatchers.Default) {
// If I initialize Napper here the second message will be logged
val response = res.readBytes().decodeToString()
if (res.status == HttpStatusCode.OK) {
Napier.v("Loded definition for: $word", tag = TAG)
} else {
Napier.e("Status: ${res.status} response: $response", tag = TAG)
}
Json {
ignoreUnknownKeys = true
}.decodeFromString(response)
}
}
For now I’ve ended up with calling this on iOS in a setup function:
private val defaultScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
actual fun setupDebug() {
Napier.base(DebugAntilog())
defaultScope.launch {
Napier.base(DebugAntilog())
}
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:5 (2 by maintainers)
Top Results From Across the Web
NSLog() producing interleaved outp… - Apple Developer
NSLog() producing interleaved output in Xcode console in a multithreaded context ... Wherein messages logged from different threads are interleaved with one ...
Read more >Log which queue/thread a method is running on
To use this just instantiate ThreadInfo while running on the thread in question. Then you can display ThreadInfo or embed it in logging...
Read more >iOS Logging practices - Netguru
Below I'm presenting few important tips about how to create log messages in your iOS application, that will make finding information for ...
Read more >Back to the main thread: DispatchQueue.main
If you're on a background thread and want to execute code on the main thread, you need to call async() again. This time,...
Read more >Concurrency & Thread Safety in Swift | by Sean Lin | Cubo AI
It is a crucial topic that you need to learn almost everywhere in modern software programming. And iOS framework provides a couple of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@RomeuG you can check out my solution. Not sure if the maintainer is available.
Please check
2.0.0-alpha2