Ok following back the issue posted by @abhishek.gpt1806 , here is a solution I found to add custom fields:
I tested that using a basic simple java app with log4j2 for logging
- Visit this link, it shows how to configure log4j2 using xml configuration
- Follow the steps for ECS logging using java - log4j2
- add your custom field as follow:
<EcsLayout serviceName="java.log" serviceVersion="my-app-version" serviceNodeName="my-app-cluster-node">
<KeyValuePair key="application.name" value="my-app"/>
</EcsLayout>
The attributes of EcsLayout
are default layouts defined by default from ECS, to add more custom fields use the same principal :
<EcsLayout serviceName="java.log" serviceVersion="my-app-version" serviceNodeName="my-app-cluster-node">
<KeyValuePair key="application.name" value="my-app"/>
<KeyValuePair key="application.os" value="linux"/>
</EcsLayout>
Last step is to use Filebeat to ship logs from STDOUT or file stream to Elasticsearch, the problem here is that data collected with APM won't have custom fields defined above (application.name and applicaiton.os), therefore better use, as @felixbarny suggested the label
field. And then use the same for your custom fields for logging like:
<KeyValuePair key="label.application.name" value="my-app"/>
<KeyValuePair key="label.application.os" value="linux"/>
like this I believe you have all your application logs and apm data use the same structure of ECS.
The output for above configuration is:
{"@timestamp":"2022-06-07T12:46:25.673Z","log.level":"ERROR","message":"Presley left no stone unturned.", "ecs.version": "1.2.0","service.name":"java.log","service.version":"my-app-version","service.node.name":"my-app-cluster-node","event.dataset":"java.log","process.thread.name":"main","log.logger":"org.example.App","application.name":"my-app","application.os":"linux"}