Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit d8849ec

Browse files
authored
Merge branch 'master' into patch-14
2 parents 2440c14 + 27ae1d8 commit d8849ec

File tree

4 files changed

+229
-0
lines changed

4 files changed

+229
-0
lines changed

_data/toc/configuration-guide.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ pages:
393393
- label: Example - logging database activity
394394
url: /config-guide/log/log-db.html
395395

396+
- label: Example - logging to a custom log file
397+
url: /config-guide/log/custom-logger-handler.html
398+
396399
- label: How to locate your session files
397400
url: /config-guide/sessions.html
398401

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
You can use one of the following approaches for logging into a custom file:
2+
3+
- Set up a custom log file in the `di.xml`
4+
- Set up a custom file in the custom logger handler class
5+
6+
## Set up a custom log file in the `di.xml`
7+
8+
This example shows how to use [virtual types]({{page.baseurl}}/extension-dev-guide/build/di-xml-file.html#virtual-types) to log `debug` messages into a custom log file instead of a standard Magento `/var/log/debug.log`.
9+
10+
1. In the `di.xml` file of your module, define a custom log file as a [virtual type]({{page.baseurl}}/extension-dev-guide/build/di-xml-file.html#virtual-types).
11+
12+
```xml
13+
<virtualType name="Magento\Payment\Model\Method\MyCustomDebug" type="Magento\Framework\Logger\Handler\Base">
14+
<arguments>
15+
<argument name="fileName" xsi:type="string">/var/log/payment.log</argument>
16+
</arguments>
17+
</virtualType>
18+
```
19+
20+
The `name` value of `Magento\Payment\Model\Method\MyCustomDebug` must be unique.
21+
22+
1. Define the handler in another [virtual type]({{page.baseurl}}/extension-dev-guide/build/di-xml-file.html#virtual-types) with a unique `name`:
23+
24+
```xml
25+
<virtualType name="Magento\Payment\Model\Method\MyCustomLogger" type="Magento\Framework\Logger\Monolog">
26+
<arguments>
27+
<argument name="handlers" xsi:type="array">
28+
<item name="debug" xsi:type="object">Magento\Payment\Model\Method\MyCustomDebug</item>
29+
</argument>
30+
</arguments>
31+
</virtualType>
32+
```
33+
34+
1. Inject the `MyCustomLogger` [virtual type]({{page.baseurl}}/extension-dev-guide/build/di-xml-file.html#virtual-types) in the `Magento\Payment\Model\Method\Logger` object:
35+
36+
```xml
37+
<type name="Magento\Payment\Model\Method\Logger">
38+
<arguments>
39+
<argument name="logger" xsi:type="object">Magento\Payment\Model\Method\MyCustomLogger</argument>
40+
</arguments>
41+
</type>
42+
```
43+
44+
1. The virtual class `Magento\Payment\Model\Method\MyCustomLogger` will be injected into the `debug` handler of the `$logger` property in the `Magento\Payment\Model\Method\Logger` class.
45+
46+
```xml
47+
...
48+
<argument name="handlers" xsi:type="array">
49+
<item name="debug" xsi:type="object">Magento\Payment\Model\Method\MyCustomDebug</item>
50+
</argument>
51+
```
52+
53+
Exception messages are logged into the `/var/log/payment.log` file.
54+
55+
## Set up a custom file in the custom logger handler class
56+
57+
This example shows how to use a custom logger handler class to log `error` messages into a specific log file.
58+
59+
1. Create a class that logs data. In this example, the class is defined in `app/code/Vendor/ModuleName/Logger/Handler/ErrorHandler.php`.
60+
61+
```php
62+
<?php
63+
/**
64+
* @author Vendor
65+
* @copyright Copyright (c) 2019 Vendor (https://www.vendor.com/)
66+
*/
67+
namespace Vendor\ModuleName\Logger\Handler;
68+
69+
use Magento\Framework\Logger\Handler\Base as BaseHandler;
70+
use Monolog\Logger as MonologLogger;
71+
72+
/**
73+
* Class ErrorHandler
74+
*/
75+
class ErrorHandler extends BaseHandler
76+
{
77+
/**
78+
* Logging level
79+
*
80+
* @var int
81+
*/
82+
protected $loggerType = MonologLogger::ERROR;
83+
84+
/**
85+
* File name
86+
*
87+
* @var string
88+
*/
89+
protected $fileName = '/var/log/my_custom_logger/error.log';
90+
}
91+
```
92+
93+
1. Define the handler for this class as a [virtual type]({{page.baseurl}}/extension-dev-guide/build/di-xml-file.html#virtual-types) in the module's `di.xml` file.
94+
95+
```xml
96+
<virtualType name="MyCustomLogger" type="Magento\Framework\Logger\Monolog">
97+
<arguments>
98+
<argument name="handlers" xsi:type="array">
99+
<item name="error" xsi:type="object">Vendor\ModuleName\Logger\Handler\ErrorHandler</item>
100+
</argument>
101+
</arguments>
102+
</virtualType>
103+
```
104+
105+
`MyCustomLogger` is a unique identifier.
106+
107+
1. In the `type` definition, specify the class name where the custom logger handler will be injected. Use the virtual type name from the previous step as an argument for this type.
108+
109+
```xml
110+
<type name="Vendor\ModuleName\Observer\MyObserver">
111+
<arguments>
112+
<argument name="logger" xsi:type="object">MyCustomLogger</argument>
113+
</arguments>
114+
</type>
115+
```
116+
117+
Source code of `Vendor\ModuleName\Observer\MyObserver` class:
118+
119+
```php
120+
<?php
121+
/**
122+
* @author Vendor
123+
* @copyright Copyright (c) 2019 Vendor (https://www.vendor.com/)
124+
*/
125+
declare(strict_types=1);
126+
127+
namespace Vendor\ModuleName\Observer;
128+
129+
use Psr\Log\LoggerInterface as PsrLoggerInterface;
130+
use Exception;
131+
use Magento\Framework\Event\ObserverInterface;
132+
use Magento\Framework\Event\Observer;
133+
134+
/**
135+
* Class MyObserver
136+
*/
137+
class MyObserver implements ObserverInterface
138+
{
139+
/**
140+
* @var PsrLoggerInterface
141+
*/
142+
private $logger;
143+
144+
/**
145+
* MyObserver constructor.
146+
*
147+
* @param PsrLoggerInterface $logger
148+
*/
149+
public function __construct(
150+
PsrLoggerInterface $logger
151+
) {
152+
$this->logger = $logger;
153+
}
154+
155+
/**
156+
* @param Observer $observer
157+
*/
158+
public function execute(Observer $observer)
159+
{
160+
try {
161+
// some code goes here
162+
} catch (Exception $e) {
163+
$this->logger->error($e->getMessage());
164+
}
165+
}
166+
}
167+
```
168+
169+
1. The class `Vendor\ModuleName\Logger\Handler\ErrorHandler` will be injected into the `error` handler of the `$logger` property in the `Vendor\ModuleName\Observer\MyObserver`.
170+
171+
```xml
172+
...
173+
<argument name="handlers" xsi:type="array">
174+
<item name="error" xsi:type="object">Vendor\ModuleName\Logger\Handler\ErrorHandler</item>
175+
</argument>
176+
...
177+
```
178+
179+
Exception messages will be logged into `/var/log/my_custom_logger/error.log` file.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
group: configuration-guide
3+
title: Example - logging to a custom log file
4+
functional_areas:
5+
- Configuration
6+
- System
7+
- Setup
8+
contributor_name: Atwix
9+
contributor_link: https://www.atwix.com/
10+
---
11+
12+
The `Magento\Framework\Logger` module contains the following handler classes:
13+
14+
| Class | Log file |
15+
| ----- | -------- |
16+
| [Magento\Framework\Logger\Handler\Base]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/Logger/Handler/Base.php) | - |
17+
| [Magento\Framework\Logger\Handler\Debug]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/Logger/Handler/Debug.php) | `/var/log/debug.log` |
18+
| [Magento\Framework\Logger\Handler\Exception]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/Logger/Handler/Exception.php) | `/var/log/exception.log` |
19+
| [Magento\Framework\Logger\Handler\System]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/Logger/Handler/System.php) | `/var/log/system.log` |
20+
21+
You may find them in the `lib/internal/Magento/Framework/Logger/Handler` directory.
22+
23+
{% include config-guide/custom-logger-handler-examples.md %}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
group: configuration-guide
3+
title: Example - logging to a custom log file
4+
functional_areas:
5+
- Configuration
6+
- System
7+
- Setup
8+
contributor_name: Atwix
9+
contributor_link: https://www.atwix.com/
10+
---
11+
12+
The `Magento\Framework\Logger` module contains the following handler classes:
13+
14+
| Class | Log file |
15+
| ----- | -------- |
16+
| [Magento\Framework\Logger\Handler\Base]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/Logger/Handler/Base.php) | - |
17+
| [Magento\Framework\Logger\Handler\Debug]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/Logger/Handler/Debug.php) | `/var/log/debug.log` |
18+
| [Magento\Framework\Logger\Handler\Exception]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/Logger/Handler/Exception.php) | `/var/log/exception.log` |
19+
| [Magento\Framework\Logger\Handler\Syslog]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/Logger/Handler/Syslog.php) | - |
20+
| [Magento\Framework\Logger\Handler\System]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/Logger/Handler/System.php) | `/var/log/system.log` |
21+
22+
You may find them in the `lib/internal/Magento/Framework/Logger/Handler` directory.
23+
24+
{% include config-guide/custom-logger-handler-examples.md %}

0 commit comments

Comments
 (0)