# Add context key to a logger

**URL:** <https://forum.inductiveautomation.com/t/add-context-key-to-a-logger/26291>\
**Category:** Module Development\
**Tags:** ignition79\
**Created:** [July 4, 2019, 6:49am UTC](https://forum.inductiveautomation.com/t/add-context-key-to-a-logger/26291 "2019-07-04T06:49:49Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [July 4, 2019, 6:49am UTC](https://forum.inductiveautomation.com/t/add-context-key-to-a-logger/26291/1 "2019-07-04T06:49:49Z")

</div>

Hello,  
I’m looking to add context key to my logger for some modules…

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/d/d19566b65e1b17937d266967cfe6491e408723e5.png)

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 4, 2019, 12:35pm UTC](https://forum.inductiveautomation.com/t/add-context-key-to-a-logger/26291/2 "2019-07-04T12:35:32Z")

</div>

This is done using [MDC](https://logback.qos.ch/manual/mdc.html).

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [July 4, 2019, 1:15pm UTC](https://forum.inductiveautomation.com/t/add-context-key-to-a-logger/26291/3 "2019-07-04T13:15:07Z")

</div>

I use the **AbstractTagDriver**.  
I would like to add some information in order to filter logs by device.

There is a MDC key **device-name** reported by the gateway logger.  
is it enough if I add :

```auto
MDC.put("device-name", "MyDevice");

```

in the constructor of the **AbstractTagDriver**.

Or I need to add this at the beginning of each method ?  
the doc mention that the `MDC` manages contextual information on a _per thread basis_ .

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 4, 2019, 1:21pm UTC](https://forum.inductiveautomation.com/t/add-context-key-to-a-logger/26291/4 "2019-07-04T13:21:12Z")

</div>

You have to add and remove this key every time you log something. It’s important to remove it before leaving the context of whatever method(s) you’re in or else the next loggers that happen to run on the current thread will have the key set still.

You can use `LoggerEx` to build a logger that always sets/unsets the key on every logging statement:

```auto
logger = LoggerEx.newBuilder()
    .mdcContext("device-name", driverContext.getDeviceName())
    .build(String.format("drivers.%s", getClass().getSimpleName()));

```

There’s a bit of a performance penalty for the convenience, but we tend to use it anyway…

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [July 4, 2019, 1:51pm UTC](https://forum.inductiveautomation.com/t/add-context-key-to-a-logger/26291/5 "2019-07-04T13:51:21Z")

</div>

> [@Kevin.Herron](#):
>
> logger = LoggerEx.newBuilder() .mdcContext("device-name", driverContext.getDeviceName()) .build(String.format("drivers.%s", getClass().getSimpleName()));

👍 I will try to replace

```auto
   import org.slf4j.Logger;

   logger = LoggerFactory.getLogger(getClass());

```

```auto
   import org.slf4j.Logger;
   import com.inductiveautomation.ignition.common.util.LoggerEx;

   logger = LoggerEx.newBuilder()
           .mdcContext("device-name", driverContext.getDeviceName())
           .build(getClass()).getLoggerSLF4J(); 

```

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 4, 2019, 1:53pm UTC](https://forum.inductiveautomation.com/t/add-context-key-to-a-logger/26291/6 "2019-07-04T13:53:40Z")

</div>

You need to define your logger field as a `LoggerEx` rather than assigning the value of `getLoggerSLF4J()` or the MDC keys won’t be set. That method just returns the underlying SLF4J Logger instance.

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [July 4, 2019, 2:03pm UTC](https://forum.inductiveautomation.com/t/add-context-key-to-a-logger/26291/7 "2019-07-04T14:03:15Z")

</div>

All my log use

```auto
logger.debug("blahblah={}",myVariable);

```

I have to replace it as follow ? debug =\> debug **f**

```auto
logger.debugf("blahblah={}",myVariable);

```

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [July 4, 2019, 2:56pm UTC](https://forum.inductiveautomation.com/t/add-context-key-to-a-logger/26291/8 "2019-07-04T14:56:38Z")

</div>

errorf() don’t work with markers like {} 😢

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [July 4, 2019, 3:26pm UTC](https://forum.inductiveautomation.com/t/add-context-key-to-a-logger/26291/9 "2019-07-04T15:26:43Z")

</div>

@Kevin.Herron, do you see any risk to replace :

```auto
import org.slf4j.Logger;

logger = LoggerFactory.getLogger(getClass());
logger.debug("blahblah={}",myVariable);

```

with :

```auto
import com.inductiveautomation.ignition.common.util.LoggerEx;
import org.slf4j.helpers.MessageFormatter

logger = LoggerEx.newBuilder()
           .mdcContext("device-name", driverContext.getDeviceName())
           .build(getClass()).getLoggerSLF4J(); 

logger.debug(MessageFormatter.format("blahblah={}",myVariable));

```

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 4, 2019, 3:45pm UTC](https://forum.inductiveautomation.com/t/add-context-key-to-a-logger/26291/10 "2019-07-04T15:45:02Z")

</div>

Using the SLF4J Logger will not get you automatic MDC support. That only happens with LoggerEx. (your example still uses `getLoggerSLF4J()`)

You’ll have to manually set/unset the context key if you want to continue using the SLF4J Logger.

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [July 4, 2019, 3:46pm UTC](https://forum.inductiveautomation.com/t/add-context-key-to-a-logger/26291/11 "2019-07-04T15:46:47Z")

</div>

oops,  
I was thinking at :

```auto
import com.inductiveautomation.ignition.common.util.LoggerEx;
import org.slf4j.helpers.MessageFormatter

logger = LoggerEx.newBuilder()
           .mdcContext("device-name", driverContext.getDeviceName())
           .build(getClass()); 

logger.debug(MessageFormatter.format("blahblah={}",myVariable));

```

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 4, 2019, 3:48pm UTC](https://forum.inductiveautomation.com/t/add-context-key-to-a-logger/26291/12 "2019-07-04T15:48:21Z")

</div>

I think that will work… haven’t tried it though.

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [July 6, 2019, 9:27am UTC](https://forum.inductiveautomation.com/t/add-context-key-to-a-logger/26291/13 "2019-07-06T09:27:46Z")

</div>

@Kevin.Herron

If we have common class used in Gateway and Client scope, when we use **com.inductiveautomation.ignition.common.util.LoggerEx** the MDC data are ignored in the client scope ?  
the vision console in 7.9 or 8.0 doesn’t suport MDC filtering.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 6, 2019, 12:56pm UTC](https://forum.inductiveautomation.com/t/add-context-key-to-a-logger/26291/14 "2019-07-06T12:56:14Z")

</div>

It’s `%s` and `%d` like `String.format`.

MDC data is only used in the gateway.
