question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Logging from another thread on iOS

See original GitHub issue

Hi. 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:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
PhilipDukhovcommented, Mar 29, 2021

@RomeuG you can check out my solution. Not sure if the maintainer is available.

0reactions
AAkiracommented, Aug 16, 2021

Please check 2.0.0-alpha2

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found