Type to search.

Structured logging with log4j2

Serverless

Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Serverless interfaces and procedures might differ from classic Elastic Stack deployments.

Stack

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

By leveraging log4j2’s MapMessage or even by implementing your own MultiformatMessage with JSON support, you can add additional fields to the resulting JSON.

Example:

Java
logger.info(new StringMapMessage()
    .with("message", "Hello World!")
    .with("foo", "bar"));

If Jackson is on the classpath, you can also use an ObjectMessage to add a custom object the resulting JSON.

Java
logger.info(new ObjectMessage(myObject));

The myObject variable refers to a custom object which can be serialized by a Jackson ObjectMapper.

Using either will merge the object at the top-level (not nested under message) of the log event if it is a JSON object. If it’s a string, number boolean, or array, it will be converted into a string and added as the message property. This conversion avoids mapping conflicts as message is typed as a string in the Elasticsearch mapping.

We recommend using existing ECS fields.

If there is no appropriate ECS field, consider prefixing your fields with labels., as in labels.foo, for simple key/value pairs. For nested structures, consider prefixing with custom.. This approach protects against conflicts in case ECS later adds the same fields but with a different mapping.

A common pitfall is how dots in field names are handled in Elasticsearch and how they affect the mapping. In recent Elasticsearch versions, the following JSON structures would result in the same index mapping:

JSON
{
  "foo.bar": "baz"
}
JSON
{
  "foo": {
    "bar": "baz"
  }
}

The property foo would be mapped to the Object datatype.

This means that you can’t index a document where foo would be a different datatype, as in shown in the following example:

JSON
{
  "foo": "bar"
}

In that example, foo is a string. Trying to index that document results in an error because the data type of foo can’t be object and string at the same time.