Laravel to send email for logging instead of writing to storage

Asked
Active3 hr before
Viewed126 times

4 Answers

laravelemail
90%

Did my installer give me the wrong overhead door springs or is my motor just too weak? , Is my PHP login system following best practices? Is the code really OOP? , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , Podcast 392: Do polyglots have an edge when it comes to mastering programming...

I still log the error, and if it's production, send an email with the exception:

App::error(function(Exception $exception, $code) {
   Log::error($exception);

   if (Config::getEnvironment() == 'production') {
      $data = array('exception' => $exception);
      Mail::send('emails.error', $data, function($message) {
         $message - > from($email_app);
         $message - > to(Config::get('settings.error_email')) - > subject(Config::get('settings.app_name').
            ' Error');
      });
      Log::info('Error Email sent to '.Config::get('settings.error_email'));
      return Response::view('errors.500', array(), 500);
   }
});
load more v
88%

Instead of sending your emails, the log mail driver will write all email messages to your log files for inspection. Typically, this driver would only be used during local development. For more information on configuring your application per environment, check out the configuration documentation.,Sending email doesn't have to be complicated. Laravel provides a clean, simple email API powered by the popular SwiftMailer library. Laravel and SwiftMailer provide drivers for sending email via SMTP, Mailgun, Postmark, Amazon SES, and sendmail, allowing you to quickly get started sending mail through a local or cloud based service of your choice.,To generate a mailable with a corresponding Markdown template, you may use the --markdown option of the make:mail Artisan command:,Rendering Mailables Previewing Mailables In The Browser

The API based drivers such as Mailgun and Postmark are often simpler and faster than sending mail via SMTP servers. Whenever possible, we recommend that you use one of these drivers. All of the API based drivers require the Guzzle HTTP library, which may be installed via the Composer package manager:

composer require guzzlehttp / guzzle
load more v
72%

Daily files. By default, that will give you files like storage/logs/laravel-2018-12-03.log, storage/logs/laravel-2018-12-04.log and so on,Writing log messages and using logging levels,Writing to a single file. By default, that’s usually storage/logs/laravel.log,Formatting your log messages

First, your custom channel needs to choose a custom driver and then add a via option to point to a logger factory class.

'channels' => [
   'custom' => [
      'driver' => 'custom',
      'via' => AppLoggingCustomLoggerFactory::class,
   ],
],
load more v
65%

Feel free to learn more about Monolog in our Monolog basics logging tutorial.,Laravel utilizes many popular open-source libraries. One of them is Monolog. It's a highly popular logging library among PHP developers, as it's easy to learn and very pleasant to work with. Monolog provides many ways to write logs such as writing them to files or sending them to email.,You can also provide contextual data in the form of an array. This data will be formatted and printed alongside the log message.,Laravel allows you to log messages about the behaviour of your application to files, the system of error logs, or even send them to Slack to notify your development team.

The configuration file contains one multidimensional array with two main keys. The first key is default and it looks like this:

'default' => env('LOG_CHANNEL', 'stack')
load more v

Other "laravel-email" queries related to "Laravel to send email for logging instead of writing to storage"