NopeCharon opened a new pull request, #831:
URL: https://github.com/apache/logging-log4cxx/pull/831

   Refs #428
   
   ## Problem
   
   Elasticsearch (directly, or through Filebeat, Logstash or fluent-bit) 
expects log events to use the [Elastic Common Schema 
(ECS)](https://www.elastic.co/docs/reference/ecs) field names. `JSONLayout` 
uses its own field names (`timestamp`, `level`, `logger`, `context_map`, ...) 
and a local time without a time zone, so its output has to be parsed and 
remapped before it can be used there. #428 asked for an ECS layout, and a 
maintainer suggested building one alongside `JSONLayout`.
   
   ## Change
   
   A new `ECSLayout` outputs each event as a single line JSON object, following 
the [ECS logging 
specification](https://github.com/elastic/ecs-logging/tree/main/spec):
   
   ```json
   
{"@timestamp":"2026-11-15T10:20:30.123Z","log.level":"INFO","message":"Hello","ecs.version":"1.2.0","log.logger":"com.foo.Bar","process.thread.name":"main","service.name":"MyApp","service.version":"1.2.3","service.environment":"production","event.dataset":"MyApp"}
   ```
   
   | Field | Content | Included |
   | --- | --- | --- |
   | `@timestamp` | event time in UTC with millisecond precision | always |
   | `log.level` | event level | always |
   | `message` | rendered message | always |
   | `ecs.version` | `1.2.0` | always |
   | `log.logger` | logger name | always |
   | `process.thread.name` | thread name (`getThreadUserName()`) | always |
   | `service.name`, `service.version`, `service.environment`, 
`service.node.name` | new `ServiceName`, `ServiceVersion`, 
`ServiceEnvironment`, `ServiceNodeName` options | when not empty |
   | `event.dataset` | new `EventDataset` option, or the `ServiceName` value | 
when not empty |
   | `tags` | the NDC, as a one element array | when the NDC is not empty |
   | *key* | each MDC entry | when the MDC is not empty |
   | `log.origin.file.name`, `log.origin.file.line`, `log.origin.function` | 
short file name, line number (a JSON number), qualified function name (e.g. 
`com::foo::Bar::doIt`) | when the `LocationInfo` option is true and location 
information is available |
   
   - `@timestamp`, `log.level` and `message` are the first three fields, as the 
specification requires.
   - `event.dataset` defaults to the service name, as the specification 
recommends.
   - Each MDC entry is a top level field named by its key (as in the Elastic 
Log4j 2 `EcsLayout` and the Log4j `EcsLayout.json` template), so an MDC key 
such as `trace.id` or `user.id` fills in the ECS field of that name. An MDC 
entry with the same name as a field the layout has already written is skipped, 
so the object never has a duplicate name. Elasticsearch rejects documents with 
duplicate fields.
   - The service and event dataset option names match the Elastic `EcsLayout` 
for Log4j 2. `LocationInfo` means the same as in `JSONLayout`.
   - Strings are escaped with `JSONLayout::appendItem`. The timestamp uses a 
GMT `SimpleDateFormat` inside a `CachedDateFormat`, as `JSONLayout` does.
   - The class is registered, so a configuration file can use 
`class="ECSLayout"` (or `org.apache.log4j.EcsLayout`), including as the layout 
of a `JSONSocketAppender`.
   
   Example configuration:
   
   ```xml
   <appender name="ECS" class="FileAppender">
     <param name="File" value="logs/MyApp.ecs.json" />
     <layout class="ECSLayout">
       <param name="ServiceName" value="MyApp" />
       <param name="ServiceVersion" value="1.2.3" />
       <param name="ServiceEnvironment" value="production" />
     </layout>
   </appender>
   ```
   
   Documentation:
   - `ECSLayout` class reference: the field table, the `setOption` table, an 
example configuration and output, and a link to `JSONSocketAppender`.
   - Layouts section of the concepts page: the new layout is listed.
   - Change log entry under 2.0.0 new features.
   
   ## Testing
   
   New `ECSLayoutTest`:
   - `testRegisteredClass`: `Class::forName("org.apache.log4j.EcsLayout")` 
creates an `ECSLayout`.
   - `testSetOption`, `testEventDatasetDefault`: option names are 
case-insensitive, and `event.dataset` falls back to the service name.
   - `testTimestamp`: `@timestamp` matches the event time converted by 
`apr_time_exp_gmt`, not by a Log4cxx date format.
   - `testFormat`, `testFormatWithServiceFields`: exact output, including 
escaping and fields that are left out when empty.
   - `testFormatWithContext`: the NDC in `tags`, MDC entries as top level 
fields, and MDC entries that would duplicate a field (`message`, 
`service.name`, `tags`) are skipped, while names that are not output 
(`service.version`, `log.origin.function`) are kept.
   - `testFormatWithLocationInfo`, `testFormatWithoutAvailableLocationInfo`: 
`log.origin.*` for a member function and a free function, and no `log.origin.*` 
when the location is unavailable.
   
   On Ubuntu 24.04 (GCC 13), the full `ctest` suite passes (72/72) and the new 
files build without warnings. `ecslayouttest` and `jsonlayouttest` also build 
and pass with `-DCMAKE_CXX_STANDARD=11 -DLOG4CXX_CHAR=wchar_t`.
   
   End to end: a program logs from the main thread and from a named thread, 
with an NDC and MDC values that contain a quote, a non-ASCII character, a tab 
and a newline. It was configured in three ways:
   - an XML file with a `FileAppender` and `ECSLayout`
   - a properties file with `layout=org.apache.log4j.EcsLayout` and 
`LocationInfo=true`
   - a `JSONSocketAppender` with an `ECSLayout`, sending to a TCP listener
   
   A Python script parsed every output line with `json.loads`, rejecting 
duplicate keys. It also checked that the first three keys are `@timestamp`, 
`log.level` and `message`, that `@timestamp` is `YYYY-MM-DDTHH:MM:SS.mmmZ`, and 
that `log.origin.file.line` is an integer. All lines passed. For example:
   
   ```
   
{"@timestamp":"2026-11-15T11:17:18.160Z","log.level":"WARN","message":"Tab\there,
 
newline\nthere","ecs.version":"1.2.0","log.logger":"com.example.App","process.thread.name":"app","service.name":"MyApp","service.node.name":"node-1","event.dataset":"MyApp.app","tags":["request-1"],"trace.id":"4bf92f3577b34da6a3ce929d0e0e4736","user.name":"Zoƫ
 
\"admin\"","log.origin.file.name":"main.cpp","log.origin.file.line":23,"log.origin.function":"main"}
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

  • [PR] Add a layout that outputs E... NopeCharon (via GitHub) via notifications

Reply via email to