ElasticSearchLogger

Logger that dumps execution logs to ElasticSearch service.

Extends: CachedLogger

Implements: IReferenceable, IOpenable

Description

The ElasticSearchLogger class allows you to create loggers that dump execution logs to an ElasticSearch service.

Important points

  • Elasticsearch is a popular search index. It is often used to store and index execution logs by itself or as a part of ELK (ElasticSearch - Logstash - Kibana) stack.
  • Authentication is not supported in this version.

Configuration parameters

  • level: maximum log level to capture
  • source: source (context) name

connection(s):

  • discovery_key: (optional) a key to retrieve the connection from IDiscovery
  • protocol: connection protocol: http or https
  • host: host name or IP address
  • port: port number
  • uri: resource URI or connection string with all parameters in it

options:

  • interval: interval in milliseconds to save log messages (default: 10 seconds)
  • max_cache_size: maximum number of messages stored in this cache (default: 100)
  • index: ElasticSearch index name (default: “log”)
  • date_format: date format used to create the index name. Eg. log-YYYYMMDD (default: “YYYYMMDD”).
  • daily: True to create a new index every day by adding a date suffix to the index name (default: False)
  • reconnect: reconnect timeout in milliseconds (default: 60 sec)
  • timeout: invocation timeout in milliseconds (default: 30 sec)
  • max_retries: maximum number of retries (default: 3)
  • index_message: True to enable indexing for message object (default: False)
  • include_type_name: will create using a “typed” index compatible with ElasticSearch 6.x (default: false)

References

  • *:discovery:*:*:1.0 - (optional) IDiscovery services
  • *:context-info:*:*:1.0 - (optional) ContextInfo to detect the context id and specify the counter’s source.

Instance methods

close

Closes the component and frees used resources.

public void close(IContext context) throws InvocationException

  • context: IContext - (optional) a context to trace execution through a call chain.

configure

Closes the component and frees used resources.

public void configure(ConfigParams config)

  • config: ConfigParams - configuration parameters to be set.

getLogItem

Converts elasticsearch object to 6.x or 7.x version by include_type_name parameter.

protected IndexRequest getLogItem()

  • returns: IndexRequest - map in elasticsearch object format

isOpen

Checks if the component is open.

public boolean isOpen()

  • returns: boolean - True if the component is open and False otherwise.

open

Opens the component.

public void open(IContext context) throws ApplicationException

  • context:IContext - (optional) a context to trace execution through a call chain.

save

Saves log messages from the cache.

protected void save(List<LogMessage> messages) throws InvocationException

  • messages: List<LogMessage>- list with log messages

setReferences

Sets references to dependent components.

void setReferences(IReferences references)

  • references: IReferences - references to locate the component dependencies.

Examples

{@code
      var logger = new ElasticSearchLogger();
      logger.configure(ConfigParams.fromTuples(
              "connection.protocol", "http",
              "connection.host", "localhost",
              "connection.port", 9200
      ));
 
      logger.open("123");
 
      var ex = new Exception();
      logger.error("123", ex, "Error occured: %s", ex.getMessage());
      logger.debug("123", "Everything is OK.");
  }