[[ecs]]
=== Elastic Common Schema (ECS)

The https://www.elastic.co/guide/en/ecs/current/ecs-reference.html[Elastic Common Schema (ECS)] is an open source format that defines a common set of fields to be used when storing event data like logs in Elasticsearch.

You can use the library https://github.com/elastic/ecs-logging-ruby[ecs-logging] which is a set of libraries that enables you to transform your application logs to structured logs that comply with the ECS format.

Add this line to your application's Gemfile:

[source,ruby]
------------------------------------
gem 'ecs-logging'
------------------------------------

Then execute `bundle install`. Or install from the command line yourself:

[source,ruby]
------------------------------------
$ gem install ecs-logging
------------------------------------

Then configure the client to use the logger:
[source,ruby]
------------------------------------
require 'ecs_logging/logger'
require 'elasticsearch'

logger = EcsLogging::Logger.new($stdout)
client = Elasticsearch::Client.new(logger: logger)
> client.info
{"@timestamp":"2022-07-12T05:31:18.590Z","log.level":"INFO","message":"GET http://localhost:9200/ [status:200, request:0.009s, query:n/a]","ecs.version":"1.4.0"}...
------------------------------------

See https://www.elastic.co/guide/en/ecs-logging/ruby/current/index.html[ECS Logging Ruby Reference] for more information on how to configure the logger.
