# Get current \`coroutineContext\` inside a regular function

**URL:** <https://discuss.kotlinlang.org/t/get-current-coroutinecontext-inside-a-regular-function/11568>\
**Category:** Language Design\
**Created:** [February 13, 2019, 3:40am UTC](https://discuss.kotlinlang.org/t/get-current-coroutinecontext-inside-a-regular-function/11568 "2019-02-13T03:40:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alamothe](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@alamothe](https://discuss.kotlinlang.org/u/alamothe)\
**Post date:** [February 13, 2019, 3:40am UTC](https://discuss.kotlinlang.org/t/get-current-coroutinecontext-inside-a-regular-function/11568/1 "2019-02-13T03:40:49Z")

</div>

Hello,  
I asked the following question in Stack Overflow: [kotlin - How to get current `coroutineContext` from a non-suspend function? - Stack Overflow](https://stackoverflow.com/questions/54661138/how-to-get-current-coroutinecontext-from-a-non-suspend-function)

But I am repeating here 🙂

I would like to get the current `coroutineContext` inside a regular function declared as:

```auto
fun log(message: String)

```

If it is not running inside a coroutine I am fine will `null`.

Basically, instead of:

```auto
public suspend inline val coroutineContext: CoroutineContext

```

I need:

```auto
public inline val coroutineContext: CoroutineContext?

```

This is entirely for logging purposes. I need to log a certain element from the context (in some way this is similar how coroutine’s name is appended to `Thread.currentThread().name` for debugging).

Is this possible? Thanks!

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [February 13, 2019, 5:36am UTC](https://discuss.kotlinlang.org/t/get-current-coroutinecontext-inside-a-regular-function/11568/2 "2019-02-13T05:36:43Z")

</div>

No, it is not. Coroutine context. Non-suspending function does not have a context. You need to pass it explicitly to your function as parameter or receiver.

---

<div class="post-metadata">

**Author:** ![alamothe](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@alamothe](https://discuss.kotlinlang.org/u/alamothe)\
**Post date:** [February 13, 2019, 10:13pm UTC](https://discuss.kotlinlang.org/t/get-current-coroutinecontext-inside-a-regular-function/11568/3 "2019-02-13T22:13:31Z")

</div>

The way I solved it is to use `ThreadLocal` instead of `CoroutineContext.Element` to keep the value in question, then use `asContextElement` to have coroutine context manage the `ThreadLocal`. This is explained in the docs here: [kotlinx.coroutines/coroutine-context-and-dispatchers.md at master · Kotlin/kotlinx.coroutines · GitHub](https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/coroutine-context-and-dispatchers.md#thread-local-data)

Then `ThreadLocal` is accessible both from `suspend fun` and `fun`. In this way, `ThreadLocal`s seem more powerful than coroutine context, since it is accessible from everywhere.

Technically, it would be even possible to expose `coroutineContext` as a `ThreadLocal`.
