What the Heck is ECID and how to Plug it in log4j

Tarannum Khan
The Startup
Published in
2 min readJul 11, 2020

Every one of us, have faced bad days when we have got a production bug to solve. If you have ever faced bugs in production(if not, damn you are lucky), the first thing is to scrutinize the logs. In the vast sea of logs, finding the right sequence of logs that are relevant to your problem is a painstaking task. My eyes gonna burn, but the quest never ends. WebLogic's ECID makes life easier by tracking the full request from inception to conclusion.

Ok, But what is ECID?

ECID(Execution context id) is injected by middleware as a request enters the system and stays the same throughout the full cycle of request. It’s a unique way to identify a request across tiers. You can get a hold of the ECID which you have to look into the logs from the response headers of a request.

Where to get ECID?

Talk is cheap, here is the code.

The DiagnosticContextHelper class has the static method getContextId which gives the ECID. For resolving this class, the wls-api dependency has to be added at compile time.

How to plug ECID in Apache log4j?

We have to create a custom pattern converter and pass the value of ECID fetched from the DiagnosticContextHelper class to log4j2. The first step is to add an additional argument ECID in the pattern in log4j2.xml

The next step is to create a custom class ECIDPatternConverter which extends the LogEventPatternConverter class for passing additional attributes to the logger. The LogEventPatternConverter is a class in the log4j module.

The method format of the LogEventPatternConverter class is overridden to append the ECID in loggers. The value of the converter key(e in the above snippet) should be the same as given in the log4j2.xml file. For comprehending the log4j pattern converter in detail, go through log4j2 documentation.

For default Weblogic logs, we directly get the ECID. If for some reason, the ECID is not getting logged, then you need to enable it in the admin console by going to Weblogic home, then to diagnostics and then to context.

Lastly, how do the logs look?

Filtering logs by ECID, we come out from the labyrinth of random data coming between the requisite log sequence. For getting the full code, please visit https://github.com/DartingMelody/ECIDDemo. In conclusion, let’s have good coverage of logs in the code to use ECID effectively.

--

--

Tarannum Khan
The Startup

A homo sapien | Librocubicularist | Food and code lover