Ways to customize logging in gemfire for every log entry event to have a custom string or prefix
search cancel

Ways to customize logging in gemfire for every log entry event to have a custom string or prefix

book

Article ID: 294482

calendar_today

Updated On:

Products

VMware Tanzu Gemfire

Issue/Introduction

Some Gemfire customers may desire a way to customize logging in GemFire such that every log entry event can have a custom string or prefix. The goal is to eventually provide better aggregation when using tools like Splunk.


Environment

Product Version: 9.10
OS: Linux/Windows

Resolution

If you want the same prefix on every log entry, you can change the log4j2.xml file that ships with GemFire and modify the geode-pattern line like:

<Property name="geode-pattern">[MY NAME %level{lowerCase=true} %date{yyyy/MM/dd HH:mm:ss.SSS z} %memberName &lt;%thread&gt; tid=%hexTid] %message%n%throwable%n</Property>

That will log messages like:

[MY NAME info 2022/04/01 08:55:04.791 PDT server1 <main> tid=0x1] GemFire P2P Listener started on /192.168.1.3:49025

However, if you want something more customizable than that, then you can follow the below steps to use Mapped Diagnostic Context (MDC, which is a log4j acronym and has been renamed to ThreadContext in log4j2):

1. Create a custom log4j2.xml file based on the one shipped with GemFire and add the variable field like:
 

<Property name="geode-pattern">[%X{environment} %level{lowerCase=true} %date{yyyy/MM/dd HH:mm:ss.SSS z} %memberName &lt;%thread&gt; tid=%hexTid] %message%n%throwable%n</Property>


2. Add an entry for environment to the MDC in the application (before starting GemFire) like:
 

import org.apache.logging.log4j.ThreadContext;

ThreadContext.put("environment", "PROD");


The resulting log message will look like:
 

[PROD info 2022/04/01 09:24:03.116 PDT server1 <main> tid=0x1] Starting DistributionManager 192.168.1.3(server1:32633)<v1>:41001.  (took 597 ms)


3. Set the system property isThreadContextMapInheritable to true, as shown below, so that all threads inherit the MDC properties:
 

-DisThreadContextMapInheritable=true


  or, when using gfsh:
 

--J--DisThreadContextMapInheritable=true