使用 log4j2 gelflayout,有没有办法抑制在最终有效载荷中添加下划线

Using log4j2 gelflayout, is there a way to suppress adding the underscore in the final payload

我们正在尝试设置自定义字段,但使用 Key Value 配置时,带有 log4j 的 GelfLayout 似乎会注入下划线。有没有办法覆盖它。

配置如下:

 <Console name="GreyLogSysOut" target="SYSTEM_OUT">                
        <GelfLayout compressionType="OFF" includeStackTrace="true" includeThreadContext="true" >  
            <KeyValuePair key="host" value="$${ctx:mdchost}" />            
            <KeyValuePair key="level" value="3" />
            <KeyValuePair key="GL_timestamp" value="${date:yyyy-MM-dd'T'HH:mm:ss.SSSZ}" />
            <KeyValuePair key="GL_timestamp" value="${date:yyyy-MM-dd'T'HH:mm:ss.SSSZ}" />                                        
            <KeyValuePair key="GL_environment" value="$${ctx:mdcenv}" />
            <KeyValuePair key="GL_userid" value="$${ctx:mdcLogAgentId}" />
            <KeyValuePair key="GL_process" value="PriPolApps" />
            <KeyValuePair key="GL_appclass" value="PriPolApps" />
            <KeyValuePair key="GL_appid" value="$${ctx:mdcappid}" />
            <KeyValuePair key="GL_sessionId" value="$${ctx:mdcLogSessionId}" />
            <KeyValuePair key="GL_correlationId" value="$${ctx:correlationId}" />                
            <KeyValuePair key="GL_logdir" value="${sys:appserver.Name}" />
            <KeyValuePair key="GL_jvm" value="$${ctx:jvm}" />
        </GelfLayout>        
    </Console>   

但是输出中插入了下划线,有没有办法去掉。

"_GL_correlationId":"${ctx:correlationId}", "_GL_logdir":"/usr/local/pfs/logs/server6/", "_GL_jvm":"${ctx:jvm}",

是的,有一种方法可以删除它。答案很简单。

只需使用 Custom GelfLayout。您可以使用上面的配置,但删除 includeStackTraceincludeThreadContextcustomFields 行:

 <GelfLayout compressionType="OFF" includeStackTrace="true" includeThreadContext="true" >  
            <KeyValuePair key="host" value="$${ctx:mdchost}" />            
            <KeyValuePair key="level" value="3" />
            <KeyValuePair key="GL_timestamp" value="${date:yyyy-MM-dd'T'HH:mm:ss.SSSZ}" />                                            
            <KeyValuePair key="GL_environment" value="$${ctx:mdcenv}" />
            <KeyValuePair key="GL_userid" value="$${ctx:mdcLogAgentId}" />            
        </GelfLayout>

你会得到这样的输出:

"_GL_correlationId":"${ctx:correlationId}", "_GL_logdir":"/usr/local/pfs/logs/server6/", "_GL_jvm":"${ctx:jvm}",