Giter Site home page Giter Site logo

Comments (7)

vy avatar vy commented on August 19, 2024

Hey @mschwehl, I am glad you liked the project. Though I am a bit confused by your questions. Let me elaborate that a bit:

  1. Judging by logback-spring.xml you shared, spring-cloud-samples uses Logback for logging, though, as its name implies, log4j2-logstash-layout is a Log4j 2.x layout plugin. So I am totally puzzled here. That being said, I will address your questions assuming you want to use Log4j 2.x in combination with log4j2-logstash-layout plugin in spring-cloud-examples.
  2. You want to expose pid. I think what you need is to just add a pid attribute referencing to ${env:PID}. Just create a new template JSON having "pid": "${env:PID}" and you are done. (Why can't we ship it by default? Because we stick to JSONEventLayoutV1, which has a pre-defined structure.)
  3. We are using the default Jackson ObjectMapper to read and write JSON. So you want to configure this mapper, say, to emit empty string on null entries. Correct?

from log4j2-logstash-layout.

mschwehl avatar mschwehl commented on August 19, 2024

Hello @vy,

Thank you for your quick response and sorry for the confusion here.
My office is using an infrastructure using elkstack. One huge client is sending json messages in the 'sleuth' format and i want to produce the same output for my client using your log4j-json library

All works fine so far, thank you for providing this lib, it really fills a gap.

Refering to 2:

My tester is


        MDC.put("X-B3-TraceId", "tracID");
	logger.info("Test Message");

       // pattern.json
	{
			"message": "${json:message}",
			"severity": "${json:level}",
			"trace": "${json:mdc:X-B3-TraceId}",
			"nulltest": "${json:mdc:nulltest}",
			"pid": "${env:PID}"
	}
	//output
	{"message":"Test Message","severity":"INFO","trace":"tracID","nulltest":"null","pid":"${env:PID}"}

i think the PID is not evaluated, therefore my question i asked to you

Refering to 3:

I will have a look how to achieve the translation from 'null' to "" in nulltest using jackson.

best regards
Martin.

from log4j2-logstash-layout.

vy avatar vy commented on August 19, 2024

There was indeed a bug with resolving null MDC (context data) values. I pushed the fix. Would you mind testing it, please? If it works for you, I will make a new release.

Coming back to the PID discussion... I thought you already had an environment variable called PID passed to your Java application. My suggestion is to first retrieve the PID in Java and then call System.setProperty("PID", pid), which will make it accessible to Log4j lookups with ${env:PID} variable.

from log4j2-logstash-layout.

mschwehl avatar mschwehl commented on August 19, 2024

I can confirm that the null-issue is fixed, thank you

Back to PID. It was my misanderstanding, i was thinking that

https://github.com/apache/logging-log4j2/blob/master/log4j-core/src/main/java/org/apache/logging/log4j/core/LogEvent.java

is providing the pid just like the ThreadId. I was wrong, thank you for pointing me to my error.

There are several ways to put the pid in the MDC. I will choose one of them and can go on with my coding. If you don't mind i will close this issue after your next release (or you can do as well)

Bist wishes
Martin

from log4j2-logstash-layout.

vy avatar vy commented on August 19, 2024

@mschwehl, great! Thanks for taking time to test it. I have just pushed the new release to Sonatype. v0.11 should appear in Maven Central momentarily.

from log4j2-logstash-layout.

MPeli avatar MPeli commented on August 19, 2024

Is it possible to get PID without manipulating MDC? We have got an application which we cannot modify, but we have got access to log4j2.xml. Thank you.

from log4j2-logstash-layout.

vy avatar vy commented on August 19, 2024

Using LogstashLayout, you can have the PID as follows:

  1. Put the PID into an environment variable, e.g., PID.
  2. Configure LogstashLayout as follows:
<LogstashLayout ...>
    <EventTemplateAdditionalFields>
        <KeyValuePair key="pid" value="${env:PID}"/>
    </EventTemplateAdditionalFields>
</LogstashLayout>

If you can use JsonTemplateLayout instead, which I strongly recommend you to do, you can solve this without introducing any environment variables:

<JsonTemplateLayout eventTemplateUri="classpath:GelfLayout.json">
  <EventTemplateAdditionalField
       key="pid"
       format="JSON"
       value='{"$resolver": "pattern", "pattern": "%pid"}'/>
</JsonTemplateLayout>

from log4j2-logstash-layout.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.