A Magento 2 module to send logs directly to Elasticsearch
- Install composer package
composer require kingfisherdirect/magento2-elasticsearch-logger
- Then run
bin/magento setup:upgrade
- Configure module through
bin/magento setup:install
orenv.php
file
bin/magento setup:install \
--elasticsearch-logger-config='{"hosts": ["http://elasticsearch:9200"]}'\
--elasticsearch-logger-index='magento2-logs'
- Enable Handler in
app/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<!-- some XML -->
<!-- Find configuration for class Magento\Framework\Logger\Monolog -->
<type name="Magento\Framework\Logger\Monolog">
<arguments>
<argument name="name" xsi:type="string">main</argument>
<argument name="handlers" xsi:type="array">
<item name="system" xsi:type="object">Magento\Framework\Logger\Handler\System</item>
<item name="debug" xsi:type="object">Magento\Framework\Logger\Handler\Debug</item>
<item name="syslog" xsi:type="object">Magento\Framework\Logger\Handler\Syslog</item>
<!-- Add following line -->
<item name="elasticsearch" xsi:type="object">KingfisherDirect\ElasticSearchLogger\Handler\ElasticSearchHandler</item>
</argument>
</arguments>
</type>
<!-- some other XML -->
</config>
Module is configurable through app/etc/env.php
file as Elasticsearch details
may be different per deployment.
To set the values it's best to use setup:install script as described in installation
bin/magento setup:install \
--elasticsearch-logger-config='{"hosts": ["http://elasticsearch:9200"]}' \
--elasticsearch-logger-index='magento2-logs'
This is JSON configuration that is then deserialized and used as a configuration to build Elasticsearch Client. Basic setup would be as follows:
{
"hosts": [
"http://elasticsearch.example.org:9200"
]
}
In case you use elasti.co service you'll then you should use this config:
{
"elasticCloudId": "ID",
"basicAuthentication": ["USER", "PASSWORD"]
}
Internally it uses Elasticsearch\ClientBuilder::fromConfig()
method for configuration, so you may want to dig into code or check library on https://github.com/elastic/elasticsearch-php
If this value is empty, no logs will be sent to Elasticsearch.
In env.php
this value is stored as PHP array, not string containing JSON
Default: monolog
Name of the index where logs should go
Not yet any.