Skip to content

Commit 3060006

Browse files
authored
Add to database logs and earlier framework instantiation (#14)
Add to database logs and earlier framework instantiation Also improve testing
1 parent 585785c commit 3060006

File tree

5 files changed

+73
-5
lines changed

5 files changed

+73
-5
lines changed

.travis.yml

+13-5
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,17 @@ script:
5858
# Output the custom loggers command filtering out modules we've accounted for
5959
- if [[ $TEST_GROUP = magento_23 ]]; then php bin/magento ampersand:log-correlation-id:list-custom-loggers --filter "Klarna\Core\Logger\Logger" --filter "Dotdigitalgroup\Email\Logger\Logger" --filter "Amazon\Core\Logger\Logger" --filter "Amazon\Core\Logger\IpnLogger" ; fi
6060
- if [[ $TEST_GROUP = magento_latest ]]; then php bin/magento ampersand:log-correlation-id:list-custom-loggers --filter "Yotpo\Yotpo\Model\Logger" --filter "Klarna\Core\Logger\Logger" --filter "Dotdigitalgroup\Email\Logger\Logger" --filter "Amazon\Core\Logger\Logger" --filter "Amazon\Core\Logger\IpnLogger" --filter "Magento\AdminAdobeIms\Logger\AdminAdobeImsLogger" ; fi
61+
# Test di compilation
62+
- rm -rf generated
63+
- composer install
64+
- php bin/magento module:enable --all
65+
- php bin/magento setup:di:compile
66+
6167
after_failure:
62-
- test -d ./vendor/ampersand/travis-vanilla-magento/instances/ampmodule/var/report/ && for r in ./vendor/ampersand/travis-vanilla-magento/instances/ampmodule/var/report/*; do cat $r; done
63-
- test -f ./vendor/ampersand/travis-vanilla-magento/instances/ampmodule/var/log/system.log && grep -v "Broken reference" ./vendor/ampersand/travis-vanilla-magento/instances/ampmodule/var/log/system.log
64-
- test -f ./vendor/ampersand/travis-vanilla-magento/instances/ampmodule/var/log/exception.log && cat ./vendor/ampersand/travis-vanilla-magento/instances/ampmodule/var/log/exception.log
65-
- test -f ./vendor/ampersand/travis-vanilla-magento/instances/ampmodule/var/log/support_report.log && grep -v "Broken reference" ./vendor/ampersand/travis-vanilla-magento/instances/ampmodule/var/log/support_report.log
66-
- sleep 10;
68+
- cd ./vendor/ampersand/travis-vanilla-magento/instances/ampmodule/
69+
- for r in ./var/report/*; do cat $r; done
70+
- for l in ./var/log/*; do cat $l; done
71+
- ls -l ./dev/tests/integration/tmp/sandbox*/var
72+
- for r in ./dev/tests/integration/tmp/sandbox*/var/report/*; do cat $r; done
73+
- for l in ./dev/tests/integration/tmp/sandbox*/var/log/*; do cat $l; done
74+
- sleep 10;

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ This cache decorator initialises the identifier which is immutable for the remai
5151
- The correlation ID is attached to web responses as `X-Log-Correlation-Id` in `src/HttpResponse/HeaderProvider/LogCorrelationIdHeader.php`
5252
- REST API requests work a bit differently in Magento and attach the header using `src/Plugin/AddToWebApiResponse.php`
5353
- Monolog files have the correlation ID added into their context section under the key `amp_correlation_id` via `src/Processor/MonologCorrelationId.php`
54+
- Magento database logs have this identifier added by `src/Plugin/AddToDatabaseLogs.php`
5455
- New Relic has this added as a custom parameter under the key `amp_correlation_id`
5556

5657
## Example usage

dev/ampersand_magento2_log_correlation/di.xml

+12
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,16 @@
2626
<argument name="headerInput" xsi:type="string"></argument>
2727
</arguments>
2828
</type>
29+
30+
<!-- Attach amp_correlation_id to all framework monolog entries -->
31+
<type name="Magento\Framework\Logger\Monolog">
32+
<arguments>
33+
<argument name="processors" xsi:type="array">
34+
<item name="correlationIdProcessor" xsi:type="array">
35+
<item name="0" xsi:type="object">Ampersand\LogCorrelationId\Processor\MonologCorrelationId</item>
36+
<item name="1" xsi:type="string">addCorrelationId</item>
37+
</item>
38+
</argument>
39+
</arguments>
40+
</type>
2941
</config>

src/Plugin/AddToDatabaseLogs.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace Ampersand\LogCorrelationId\Plugin;
4+
5+
use Ampersand\LogCorrelationId\Service\CorrelationIdentifier;
6+
use Magento\Framework\DB\LoggerInterface;
7+
8+
class AddToDatabaseLogs
9+
{
10+
/**
11+
* @var CorrelationIdentifier
12+
*/
13+
private CorrelationIdentifier $correlationIdentifier;
14+
15+
/**
16+
* Constructor
17+
*
18+
* @param CorrelationIdentifier $correlationIdentifier
19+
*/
20+
public function __construct(CorrelationIdentifier $correlationIdentifier)
21+
{
22+
$this->correlationIdentifier = $correlationIdentifier;
23+
}
24+
25+
/**
26+
* Append the log correlation ID to the database log
27+
*
28+
* @param LoggerInterface $subject
29+
* @param string $string
30+
* @return string
31+
*/
32+
public function beforeLog(LoggerInterface $subject, $string)
33+
{
34+
$logCorrelationIdString =
35+
$this->correlationIdentifier->getIdentifierKey() .
36+
'=' .
37+
$this->correlationIdentifier->getIdentifierValue() .
38+
PHP_EOL;
39+
40+
return $logCorrelationIdString . $string;
41+
}
42+
}

src/etc/di.xml

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
<plugin name="ampersand_log_correlation_id_response_plugin" type="Ampersand\LogCorrelationId\Plugin\AddToWebApiResponse" sortOrder="1" disabled="false"/>
2525
</type>
2626

27+
<!-- Attach X-Log-Correlation-Id to database logs -->
28+
<type name="Magento\Framework\DB\LoggerInterface">
29+
<plugin name="ampersand_log_correlation_id_db_log_plugin" type="Ampersand\LogCorrelationId\Plugin\AddToDatabaseLogs" sortOrder="1" disabled="false"/>
30+
</type>
31+
2732
<!-- Attach amp_correlation_id to all monolog entries -->
2833
<type name="Magento\Framework\Logger\Monolog">
2934
<arguments>

0 commit comments

Comments
 (0)