Apache Log4cxx  Version 2.0.0
Loading...
Searching...
No Matches
log4cxx::ECSLayout Class Reference

This layout outputs each event as a single line JSON object using the field names of the Elastic Common Schema (ECS), as described in the ECS logging specification. More...

#include <ecslayout.h>

Inheritance diagram for log4cxx::ECSLayout:
log4cxx::Layout log4cxx::spi::OptionHandler log4cxx::helpers::Object

Public Member Functions

 ECSLayout ()
 ~ECSLayout ()
void setServiceName (const LogString &newValue)
 Use newValue as the service.name field value.
const LogString & getServiceName () const
 The service.name field value.
void setServiceVersion (const LogString &newValue)
 Use newValue as the service.version field value.
const LogString & getServiceVersion () const
 The service.version field value.
void setServiceEnvironment (const LogString &newValue)
 Use newValue as the service.environment field value, e.g.
const LogString & getServiceEnvironment () const
 The service.environment field value.
void setServiceNodeName (const LogString &newValue)
 Use newValue as the service.node.name field value, which identifies a particular instance of a service.
const LogString & getServiceNodeName () const
 The service.node.name field value.
void setEventDataset (const LogString &newValue)
 Use newValue as the event.dataset field value.
const LogString & getEventDataset () const
 The event.dataset field value (the ServiceName option value when EventDataset is empty).
void setLocationInfo (bool newValue)
 Include the source code location (the log.origin.
bool getLocationInfo () const
 Is the source code location of the logging request included?
LogString getContentType () const override
 Returns the content type output by this layout, i.e "application/json".
bool activateOptions () override
 Activate the options that were previously set with calls to option setters.
void setOption (const LogString &option, const LogString &value) override
 Override this method to set option to value.
void format (LogString &output, const spi::LoggingEventPtr &event) const override
 Append the attributes of event as a single line JSON object onto output.
bool ignoresThrowable () const override
 The ECS layout handles the throwable contained in logging events.
void format (LogString &output, const spi::LoggingEventPtr &event, helpers::Pool &p) const
Public Member Functions inherited from log4cxx::Layout
virtual ~Layout ()
void format (LogString &output, const spi::LoggingEventPtr &event, helpers::Pool &p) const
virtual void appendHeader (LogString &output)
 Append the header for the layout format.
void appendHeader (LogString &output, helpers::Pool &p)
virtual void appendFooter (LogString &output)
 Append the footer for the layout format.
void appendFooter (LogString &output, helpers::Pool &p)
bool activateOptions () override
 Activate the options that were previously set with calls to option setters.
void activateOptions (helpers::Pool &)
Public Member Functions inherited from log4cxx::spi::OptionHandler
virtual ~OptionHandler ()
void activateOptions (helpers::Pool &)
Public Member Functions inherited from log4cxx::helpers::Object
virtual ~Object ()
virtual const helpers::Class & getClass () const =0
virtual bool instanceof (const Class &clazz) const =0
virtual const void * cast (const Class &clazz) const =0

Additional Inherited Members

Protected Member Functions inherited from log4cxx::Layout
size_t getFormattedEventCharacterCount () const
 The expected length of a formatted event excluding the message text.

Detailed Description

This layout outputs each event as a single line JSON object using the field names of the Elastic Common Schema (ECS), as described in the ECS logging specification.

The output can be sent to Elasticsearch (for example, using Filebeat, Logstash or fluent-bit) without further parsing.

The fields in each line are:

Field Content Included
@timestamp The event time in UTC with millisecond precision, e.g. 2026-11-15T10:20:30.123Z always
log.level The event level, e.g. INFO always
message The rendered message always
ecs.version 1.2.0 always
log.logger The logger name always
process.thread.name The name of the thread that made the logging request (or its identifier when the name is not available) always
service.name The ServiceName option value when not empty
service.version The ServiceVersion option value when not empty
service.environment The ServiceEnvironment option value when not empty
service.node.name The ServiceNodeName option value when not empty
event.dataset The EventDataset option value, or the ServiceName option value when not empty
tags An array containing the nested diagnostic context (NDC) when the NDC is not empty
key The value of each mapped diagnostic context (MDC) entry when the MDC is not empty
log.origin.file.name The source file name of the logging request when the LocationInfo option is true and location information is available
log.origin.file.line The source line number (a JSON number) as above
log.origin.function The qualified function name, e.g. com::foo::Bar::doIt as above

Each mapped diagnostic context (MDC) entry is added using its key as the field name, so an MDC key such as trace.id or user.id populates the ECS field of that name. An MDC entry that has the same name as a field already added by this layout is not output, so the JSON object never contains a duplicate field name.

Here is an example configuration that writes ECS JSON lines to a file:

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<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>
<root>
<priority value ="INFO" />
<appender-ref ref="ECS" />
</root>
</log4j:configuration>

A logging request in this configuration produces a line like:

{"@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"}

To send the events to a log server instead, use this layout in a JSONSocketAppender.

Constructor & Destructor Documentation

◆ ECSLayout()

log4cxx::ECSLayout::ECSLayout ( )

◆ ~ECSLayout()

log4cxx::ECSLayout::~ECSLayout ( )

Member Function Documentation

◆ activateOptions()

bool log4cxx::ECSLayout::activateOptions ( )
overridevirtual

Activate the options that were previously set with calls to option setters.

Calculates the length of the formatted output.

Implements log4cxx::spi::OptionHandler.

◆ format() [1/2]

void log4cxx::ECSLayout::format ( LogString & output,
const spi::LoggingEventPtr & event ) const
overridevirtual

Append the attributes of event as a single line JSON object onto output.

Implements log4cxx::Layout.

◆ format() [2/2]

void log4cxx::Layout::format ( LogString & output,
const spi::LoggingEventPtr & event,
helpers::Pool & p ) const
Deprecated
Backward compatability support will be removed in a future version.
Deprecated
Use format() without a Pool parameter instead

◆ getContentType()

LogString log4cxx::ECSLayout::getContentType ( ) const
overridevirtual

Returns the content type output by this layout, i.e "application/json".

Reimplemented from log4cxx::Layout.

◆ getEventDataset()

const LogString & log4cxx::ECSLayout::getEventDataset ( ) const

The event.dataset field value (the ServiceName option value when EventDataset is empty).

◆ getLocationInfo()

bool log4cxx::ECSLayout::getLocationInfo ( ) const

Is the source code location of the logging request included?

◆ getServiceEnvironment()

const LogString & log4cxx::ECSLayout::getServiceEnvironment ( ) const

The service.environment field value.

◆ getServiceName()

const LogString & log4cxx::ECSLayout::getServiceName ( ) const

The service.name field value.

◆ getServiceNodeName()

const LogString & log4cxx::ECSLayout::getServiceNodeName ( ) const

The service.node.name field value.

◆ getServiceVersion()

const LogString & log4cxx::ECSLayout::getServiceVersion ( ) const

The service.version field value.

◆ ignoresThrowable()

bool log4cxx::ECSLayout::ignoresThrowable ( ) const
inlineoverridevirtual

The ECS layout handles the throwable contained in logging events.

Hence, this method returns false.

Implements log4cxx::Layout.

◆ setEventDataset()

void log4cxx::ECSLayout::setEventDataset ( const LogString & newValue)

Use newValue as the event.dataset field value.

When empty (the default), the ServiceName option value is used.

◆ setLocationInfo()

void log4cxx::ECSLayout::setLocationInfo ( bool newValue)

Include the source code location (the log.origin.

* fields) of the logging request when newValue is true. By default, location information is not included.

◆ setOption()

void log4cxx::ECSLayout::setOption ( const LogString & option,
const LogString & value )
overridevirtual

Override this method to set option to value.

Supported options Supported values Default value
ServiceName {any} -
ServiceVersion {any} -
ServiceEnvironment {any} -
ServiceNodeName {any} -
EventDataset {any} (the ServiceName value)
LocationInfo True,False false
See also
setServiceName, setServiceVersion, setServiceEnvironment, setServiceNodeName, setEventDataset, setLocationInfo

Reimplemented from log4cxx::spi::OptionHandler.

◆ setServiceEnvironment()

void log4cxx::ECSLayout::setServiceEnvironment ( const LogString & newValue)

Use newValue as the service.environment field value, e.g.

production or staging. An empty value (the default) omits the field.

◆ setServiceName()

void log4cxx::ECSLayout::setServiceName ( const LogString & newValue)

Use newValue as the service.name field value.

An empty value (the default) omits the field.

◆ setServiceNodeName()

void log4cxx::ECSLayout::setServiceNodeName ( const LogString & newValue)

Use newValue as the service.node.name field value, which identifies a particular instance of a service.

An empty value (the default) omits the field.

◆ setServiceVersion()

void log4cxx::ECSLayout::setServiceVersion ( const LogString & newValue)

Use newValue as the service.version field value.

An empty value (the default) omits the field.


The documentation for this class was generated from the following file: