I have a Laravel 5.6 project and I want to configure it such that high level errors are emailed to me.
I have added an email channel to logging.php config file and specified Monolog's Swiftmailer as the handler but since it needs a mailer instance and message in its constructor I don't know what values should be supplied.
Any help is much appreciated.
PS: I do know there are packages for this purpose but think this should be simple enough to do manually.
'email' => [
'driver' => 'monolog',
'handler' => Monolog\Handler\SwiftMailerHandler::class,
'handler_with' => [
'mailer' => ?,
'message' => ?,
],
],
With Lavarel 7.x you can solve it like this
'smtp-sys' => [
'driver' => 'monolog',
'level' => 'info',
'handler' => SwiftMailerHandler::class,
'handler_with' => [
'mailer' => new Swift_Mailer( (new Swift_SmtpTransport(env('MAIL_HOST_SYS'), env('MAIL_PORT_SYS'), env('MAIL_ENCRYPTION', 'tls')))->setUsername(env('MAIL_USERNAME_SYS'))->setPassword(env('MAIL_PASSWORD_SYS'))),
'message' => (new Swift_Message('[LOG] Exception'))
->setFrom([env('MAIL_USERNAME_SYS') => 'Dev'])
->setTo([env('MAIL_USERNAME_SYS') => 'Dev'])
],
],
It might be a little late but from Symfony SwiftMailer documentation you should use something like this:
...
'email' => [
'driver' => 'monolog',
'level' => 'critical',
'handler' => SwiftMailerHandler::class,
'handler_with' => [
'mailer' => (new Swift_SmtpTransport(env('MAIL_HOST'), env('MAIL_PORT'), env('MAIL_ENCRYPTION')))
->setUsername(env('MAIL_USERNAME'))
->setPassword(env('MAIL_PASSWORD')),
'message' => (new Swift_Message('[LOG] Exception'))
->setFrom([env('LOG_MAIL_FROM_ADDRESS') => env('LOG_MAIL_FROM_NAME')])
->setTo([env('LOG_MAIL_TO_ADDRESS') => 'Dev'])
->setBody('Here is the message itself')
],
'formatter' => HtmlFormatter::class,
],
...
Related
I have installed the Rollbar 7.0 into Laravel 8.0. PHP version is 7.4
I am trying to send a test exception message using a simple Console command but that sends me nothing.
My configs are the following:
config/app.php:
return [
'providers' => [
Rollbar\Laravel\RollbarServiceProvider::class
...
]
config/logging.php:
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['other', 'rollbar'],
'ignore_exceptions' => false,
],
'rollbar' => [
'driver' => 'monolog',
'handler' => MonologHandler::class,
'access_token' => env('ROLLBAR_TOKEN'),
'level' => env('ROLLBAR_LEVEL'),
'enabled' => true,
'environment' => env('ROLLBAR_ENVIRONMENT'),
]
....
config/services.php (but seems to be that it doesn't work)
'rollbar' => [
'access_token' => env('ROLLBAR_TOKEN'),
'environment' => env('ROLLBAR_ENVIRONMENT'),
'level' => env('ROLLBAR_LEVEL')
],
app.env:
ROLLBAR_TOKEN=real_token
ROLLBAR_LEVEL=debug
ROLLBAR_ENVIRONMENT=backend_test
And the console command itself has the following view:
public function handle()
{
// Rollbar::init([
// 'driver' => 'monolog',
// 'handler' => MonologHandler::class,
// 'access_token' => env('ROLLBAR_TOKEN'),
// 'level' => env('ROLLBAR_LEVEL'),
// 'enabled' => true,
// 'environment' => env('ROLLBAR_ENVIRONMENT'),
// ]);
try{
$x = 4/0;
} catch(\Exception $exception) {
Rollbar::error('caught demo exception', ["details" => $exception->getMessage()]));
Rollbar::flush();
exit(1);
}
}
So when it is like this, the rollbar stays silent. But if I uncomment the initialisation, that works well, sending a debug message to the rollbar.
That doesn't work all over the project too.
Could you please advice me, what could I do here in order to make it work globally with initialising in every file?
upd: I've also cleared config cache and tried to make a rollbar as a default
Laravel in app/logging.php has a default channel configuration. Normally "default" should mean that there are some other working channel too but here, somehow it the meaning is like "the only used channel". Or I just do not fully understand how should it work. So my rollbar channel seems to be overriden by the another "default" one, that is why the system doesn't use it. So the solution is to switch the default channel:
'default' => env('LOG_CHANNEL', 'stack'),
when the rollbar is included to stack channel or just
'default' => env('LOG_CHANNEL', 'rollbar'),
when it is not.
The official AWS PHP SDK Pinpoint documentation is so dense that even sending a simple email seems like a daunting task :)
https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-pinpoint-2016-12-01.html#sendmessages
$result = $client->sendMessages([
'ApplicationId' => '<string>', // REQUIRED
'MessageRequest' => [ // REQUIRED
'Addresses' => [
'<__string>' => [
'BodyOverride' => '<string>',
'ChannelType' => 'GCM|APNS|APNS_SANDBOX|APNS_VOIP|APNS_VOIP_SANDBOX|ADM|SMS|VOICE|EMAIL|BAIDU|CUSTOM',
'Context' => ['<string>', ...],
'RawContent' => '<string>',
'Substitutions' => [
'<__string>' => ['<string>', ...],
// ...
],
'TitleOverride' => '<string>',
],
// ...
],
'Context' => ['<string>', ...],
'Endpoints' => [
'<__string>' => [
'BodyOverride' => '<string>',
'Context' => ['<string>', ...],
'RawContent' => '<string>',
'Substitutions' => [
'<__string>' => ['<string>', ...],
// ...
],
'TitleOverride' => '<string>',
],
// ...
],
'MessageConfiguration' => [ // REQUIRED
'EmailMessage' => [
'Body' => '<string>',
'FeedbackForwardingAddress' => '<string>',
'FromAddress' => '<string>',
'RawEmail' => [
'Data' => <string || resource || Psr\Http\Message\StreamInterface>,
],
'ReplyToAddresses' => ['<string>', ...],
'SimpleEmail' => [
'HtmlPart' => [
'Charset' => '<string>',
'Data' => '<string>',
],
'Subject' => [
'Charset' => '<string>',
'Data' => '<string>',
],
'TextPart' => [
'Charset' => '<string>',
'Data' => '<string>',
],
],
'Substitutions' => [
'<__string>' => ['<string>', ...],
// ...
],
],
],
'TemplateConfiguration' => [
'EmailTemplate' => [
'Name' => '<string>',
'Version' => '<string>',
],
'PushTemplate' => [
'Name' => '<string>',
'Version' => '<string>',
],
'SMSTemplate' => [
'Name' => '<string>',
'Version' => '<string>',
],
'VoiceTemplate' => [
'Name' => '<string>',
'Version' => '<string>',
],
],
'TraceId' => '<string>',
],
]);
Does someone have a working code snippet just for sending a simple email using the PHP SDK v3?
The following php script demonstrates how you can use AWS PHP SDK v3 to send emails using Amazon Pinpoint service using the sendMessage API.
Feel free to fork it from gists and customize to fit your need.
<?php
require 'vendor/autoload.php';
use Aws\Pinpoint\PinpointClient;
use Aws\Exception\AwsException;
/**
* #author syumaK(Amos Syuma)
* Date: March 24, 2020
* Description: A php script that uses AWS PHP SDK for Pinpoint service to send an email message.
*
*/
/**
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
// Instantiate a client with the credentials from the credential profiles (.aws/credentials) file.
$settings = (array(
'profile' => 'syumaK',
'region' => 'us-east-1',
'version' => 'latest',
));
$pinpointClient = new Aws\Pinpoint\PinpointClient($settings);
# The "From" address. This address has to be verified in Amazon Pinpoint in the region you're using to send email.
$SENDER = "redacted";
# The addresses on the "To" line. If your Amazon Pinpoint account is in the sandbox, these addresses also have to be verified.
$TOADDRESS = "redacted";
try {
$result = $pinpointClient->sendMessages([
'ApplicationId' => '4fd13xxxxxxxxx', // REQUIRED
'MessageRequest' => [ // REQUIRED
'Addresses' => [
$TOADDRESS => [
'ChannelType' => 'EMAIL',
],
],
'MessageConfiguration' => [ // REQUIRED
'EmailMessage' => [
'FromAddress' => $SENDER,
],
],
'TemplateConfiguration' => [ // REQUIRED
'EmailTemplate' => [
'Name' => 'One',
'Version' => '1',
],
],
],
]);
print $result;
} catch (AwsException $e){
// output error message if fails
error_log($e->getMessage());
}
?>
Tested the above code snippet using the following environment spec:
Ubuntu: 18.04
Apache2: 2.4.18
aws-sdk-php": "^3.108"
Hope this helps!
Based on syumaK answer, I finally got it working with this snippet:
<?php
require 'vendor/autoload.php';
use Aws\Pinpoint\PinpointClient;
use Aws\Exception\AwsException;
/**
* #author syumaK(Amos Syuma)
* Date: March 24, 2020
* Description: A php script that uses AWS PHP SDK for Pinpoint service to send an email message.
*
*/
/**
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
// Instantiate a client with the credentials from the credential profiles (.aws/credentials) file.
$settings = (array(
'profile' => 'syumaK',
'region' => 'us-east-1',
'version' => 'latest',
));
$pinpointClient = new Aws\Pinpoint\PinpointClient($settings);
# The "From" address. This address has to be verified in Amazon Pinpoint in the region you're using to send email.
$SENDER = "redacted";
# The addresses on the "To" line. If your Amazon Pinpoint account is in the sandbox, these addresses also have to be verified.
$TOADDRESS = "redacted";
try {
$result = $pinpointClient->sendMessages([
'ApplicationId' => 'REPLACE WITH APP ID (NOT NAME)',
'MessageRequest' => [ // REQUIRED
'Addresses' => [
$TOADDRESS => [
'ChannelType' => 'EMAIL',
],
],
'MessageConfiguration' => [ // REQUIRED
'EmailMessage' => [
'FromAddress' => $SENDER,
'SimpleEmail' => [
'HtmlPart' => [
'Charset' => 'utf-8',
'Data' => 'my sample html',
],
'Subject' => [
'Charset' => 'utf-8',
'Data' => 'my sample subject',
],
'TextPart' => [
'Charset' => 'utf-8',
'Data' => 'my sample text',
]
]
],
],
]
]);
print $result;
} catch (AwsException $e){
// output error message if fails
error_log($e->getMessage());
}
?>
In Laravel I'm defining a custom log file in /config/logger.php:
'mycustomlog' => [
'driver' => 'stack',
'path' => storage_path('logs/mycustomlog.log'),
'level' => 'info',
],
Here's my stack driver for context:
'stack' => [
'driver' => 'stack',
'channels' => ['daily', 'syslog'],
'ignore_exceptions' => false,
],
and I'm calling it as follows:
Log::channel('mycustomlog')->info($e);
What I expect to happen:
A (daily) log file is created and the exception is logged. I.e mycustomlog-2019-11-07.log
What actually happens:
No log file is created, but the following error is logged to laravel.log:
[2019-11-07 10:25:31] laravel.EMERGENCY: Unable to create configured logger. Using emergency logger. {"exception":"[object] (ErrorException(code: 0): Undefined index: channels at /var/www/vendor/laravel/framework/src/Illuminate/Log/LogManager.php:232)
SOLUTION:
'mycustomlog' => [
'driver' => 'daily',
'channels' => ['syslog'],
'path' => storage_path('logs/mycustomlog.log'),
'level' => 'info',
],
You will need to have channels in the config logger.php see here. The point of the stack driver is to report to multiple channels.
'mycustomlog' => [
'driver' => 'stack',
'channels' => ['daily'],
'path' => storage_path('logs/mycustomlog.log'),
'level' => 'info',
],
If you don't want it to be in the stack you can have your config to point to the single driver like this
'mycustomlog' => [
'driver' => 'single',
'path' => storage_path('logs/mycustomlog.log'),
'level' => 'info',
]
Then call it the same way you tried on your own.
Log::channel('mycustomlog')->info($e);
Im trying to debug an Internal Server Error on production that runs Yii2.
To do this, I added EmailTarget to my config file as follows
if(!YII_DEBUG){
$logTarget[] = [
'class' => 'yii\log\EmailTarget',
'mailer' =>'mailer',
'levels' => ['error'],
'message' => [
'from' => ['mail#example.com'],
'to' => ['mymail#example.com'],
'subject' => 'Log',
],
'categories' => [
'yii\db\*',
'yii\web\HttpException:*',
],
'except' => [
'yii\web\HttpException:404',
'yii\web\HttpException:403',
'yii\web\HttpException:401'
]
];
}
then
'log' => [
'traceLevel' => 3,
'targets' => $logTarget,
],
Im not getting the 500 errors though. Am I doing smt wrong? Or are 500 errors not logged by default
So finally got it working.
Apparently, the 'categories' here blocked 500 errors from being sent probably since most 500 errors belong to yii\base\Excetion and not yii\web\HttpException.
Since, by default, if no 'categories' are put, all the errors are included, just removing this from the array fixed the problem.
My fixed array is:
if(!YII_DEBUG){
$logTarget[] = [
'class' => 'yii\log\EmailTarget',
'mailer' =>'mailer',
'levels' => ['error'],
'message' => [
'from' => ['mail#example.com'],
'to' => ['mymail#example.com'],
'subject' => 'Log',
],
'except' => [
'yii\web\HttpException:404',
'yii\web\HttpException:403',
'yii\web\HttpException:401'
]
];
}
I am quite novice with ZF3 and I can't figure out how should I define a logger module as a service and how could I use (reuse) it in other modules. The official documentation is poor from this point of view. Any short example would be good.
If you want to use zend-log in ZF app, after installation you need to do 2 thing:
To register Zend\Log in the application config under the 'modules' key.
Add config for your logger in global.php or module config
'log' => [
'MyLogger' => [
'writers' => [
'stream' => [
'name' => 'stream',
'priority' => \Zend\Log\Logger::ALERT,
'options' => [
'stream' => '/tmp/php_errors.log',
'formatter' => [
'name' => \Zend\Log\Formatter\Simple::class,
'options' => [
'format' => '%timestamp% %priorityName% (%priority%): %message% %extra%',
'dateTimeFormat' => 'c',
],
],
'filters' => [
'priority' => [
'name' => 'priority',
'options' => [
'operator' => '<=',
'priority' => \Zend\Log\Logger::INFO,
],
],
],
],
],
],
],
],
after that just take it from Service Manager and use it:
$logger = $container->get('MyLogger'); // <-- the key that you register in config above
$logger->info('Logging info message in the file');
You probably want to take logger from SM and than inject it in a class that you want to use it.
There is a god blog post about Logging with zend-log