@cali_green
To send logs to email on exception or error in Symfony, you need to follow these steps:
- Configure the SwiftMailer library in your Symfony project by installing it via Composer if you haven't already done so:
1
|
composer require symfony/swiftmailer-bundle
|
- Configure the SwiftMailer settings in your config/packages/swiftmailer.yaml file:
1
2
3
4
5
6
7
8
|
# config/packages/swiftmailer.yaml
swiftmailer:
transport: smtp
host: {your_smtp_host}
port: {your_smtp_port}
encryption: {your_smtp_encryption}
username: {your_smtp_username}
password: {your_smtp_password}
|
Replace {your_smtp_host}
, {your_smtp_port}
, {your_smtp_encryption}
, {your_smtp_username}
, and {your_smtp_password}
with your actual SMTP server details.
- Install the Monolog library in your Symfony project:
1
|
composer require symfony/monolog-bundle
|
- Configure the Monolog settings in your config/packages/monolog.yaml file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: grouped
grouped:
type: group
members: [streamed, buffered]
streamed:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
buffered:
type: buffer
bufferSize: 30
handler: swift
swift:
type: swift_mailer
from_email: {sender_email_address}
to_email: {recipient_email_address}
subject: "Error/Exception occurred in Symfony app"
|
Replace {sender_email_address}
and {recipient_email_address}
with the appropriate email addresses.
- Set up the error and exception email notification in your Symfony app's services.yaml file:
1
2
3
4
5
6
7
8
|
# config/services.yaml
services:
AppEventListenerExceptionListener:
tags:
- {name: kernel.event_listener, event: kernel.exception, method: onKernelException}
AppEventListenerErrorListener:
tags:
- {name: kernel.event_listener, event: kernel.error, method: onKernelError}
|
- Create the ExceptionListener class with the following configuration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
// src/EventListener/ExceptionListener.php
<?php
namespace AppEventListener;
use SymfonyComponentHttpKernelEventExceptionEvent;
use SymfonyComponentHttpFoundationResponse;
use PsrLogLoggerInterface;
class ExceptionListener
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function onKernelException(ExceptionEvent $event)
{
$exception = $event->getThrowable();
$this->logger->error($exception->getMessage());
}
}
|
- Create the ErrorListener class with the following configuration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
// src/EventListener/ErrorListener.php
<?php
namespace AppEventListener;
use SymfonyComponentHttpKernelEventErrorEvent;
use SymfonyComponentHttpFoundationResponse;
use PsrLogLoggerInterface;
class ErrorListener
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function onKernelError(ErrorEvent $event)
{
$error = $event->getError();
$this->logger->error($error->getMessage());
}
}
|
- Clear the cache on your Symfony project by running the following command:
1
|
php bin/console cache:clear
|
Now, when an exception or error occurs in your Symfony application, the logs will be sent to the specified email address.