Giter Site home page Giter Site logo

Comments (5)

morad3741 avatar morad3741 commented on May 23, 2024

I tried to add VirtualProperty with no luck:

 <Elasticsearch name="elasticsearch">
            <IndexName indexName="XXX"/>
            <ThresholdFilter level="DEBUG" onMatch="ACCEPT"/>
            <JacksonJsonLayout>
                <NonEmptyFilter/>
                <VirtualProperty name="field1" value="$${ctx:ctxVariable:-}" dynamic="true" />
                <PooledItemSourceFactory poolName="itemPool" itemSizeInBytes="1024" initialPoolSize="3000"/>
            </JacksonJsonLayout>
            <AsyncBatchDelivery batchSize="1" deliveryInterval="2" >
                <IndexTemplate name="XXXX" path="classpath:indexTemplate.json" />
                <HCHttp serverUris="http://XXXX:9200">
                    <PooledItemSourceFactory poolName="batchPool" itemSizeInBytes="1024000" initialPoolSize="3"/>
                </HCHttp>
            </AsyncBatchDelivery>
        </Elasticsearch>

and changed the logger to log:
logger.atError().addKeyValue("field1", authenticationRequest).addKeyValue("test","value").log("11111111111111111");
and still field1 is not present as a seperated field

from log4j2-elasticsearch.

rfoltyns avatar rfoltyns commented on May 23, 2024

Hi @morad3741

If you're trying to resolve $${ctx:ctxVariable:-}, then ctxVariable is the one that must be resolvable within LogEvent.contextData. field1 is just it's name in the output document.

Try this:

logger.atError().addKeyValue("ctxVariable", "33333")

from log4j2-elasticsearch.

morad3741 avatar morad3741 commented on May 23, 2024

Hi,
Im trying to add new field to the JSON dynamically,
Just like it shown in the next guide: logback logstash guide
in their example:
log.info("Order saved", kv("orderId", orderId), kv("status", status)); will create "orederId" and "status" node in the JSON,
Im looking for a way to do the same in log4j.

I tried:
logger.atError().addKeyValue("ctxVariable", "33333")
it didnt added anything to the JSON,

Thanks for your help

from log4j2-elasticsearch.

rfoltyns avatar rfoltyns commented on May 23, 2024

My mistake. I assumed that SLF4j2 .addKeyValue works like MDC. It turns out that it doesn't.

According to SLF4J Manual, addKeyValue will just help you log like this:

int newT = 15;
int oldT = 16;

// using classical API
logger.debug("oldT={} newT={} Temperature changed.", newT, oldT);

// using fluent API
logger.atDebug().addKeyValue("oldT", oldT).addKeyValue("newT", newT).log("Temperature changed.");

to get the same output as DefaultLoggingEventBuilder#mergeMarkersAndKeyValuePairs in slf4j-api:2.0.0-alpha1 will just prepend the keys as it used to in old versions.

In order to get the behaviour you expect, you need MDC:

MDC.put("ctxVariable", "someValue");
logger.atError().log("11111111111111111");
MDC.remove("ctxVariable");

It will work only with synchronous loggers.

If you need to use asynchronous loggers, you'll can get MDC to work as expected by overriding LogEvent.contextData serialization with Jackson mix-in as described here.

from log4j2-elasticsearch.

rfoltyns avatar rfoltyns commented on May 23, 2024

Closing due to lack of activity

from log4j2-elasticsearch.

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.