Laravel Stack Driver Logger
Google Cloud Stack Driver error monitoring integration for Laravel projects.
This library adds a listener to Laravel's logging component. Laravel's session information will be sent in to Stack Driver, as well as some other helpful information such as 'environment', 'server', and 'session'., (*1)
Installation
Install using composer:, (*2)
composer require pagevamp/laravel-stack-driver-logger
Add the service provider to the 'providers' array in config/app.php:, (*3)
Pagevamp\Providers\StackDriverLoggerServiceProvider::class,
If you only want to enable Stack Driver reporting for certain environments you can conditionally load the service provider in your AppServiceProvider:, (*4)
public function register()
{
if ($this->app->environment('production')) {
$this->app->register(\Pagevamp\Providers\StackDriverLoggerServiceProvider::class);
}
}
Configuration
This package supports configuration through the services configuration file located in config/services.php. All configuration variables will be directly passed to Stack Driver:, (*5)
'stack_driver_logger' => [
'credentials' => [
'keyFile' => json_decode(trim(env('GC_LOG_SERVICE_CRED'), "'"), 1),
],
'log_name' => env('GC_LOG_NAME', 'builder-log'),
],
Usage
To automatically monitor exceptions, simply use the Log facade in your error handler in app/Exceptions/Handler.php:, (*6)
public function report(Exception $exception)
{
\Log::error($exception); //Stack Driver
parent::report($exception);
}
Your other log messages will also be sent to Stack Driver:, (*7)
\Log::debug('Here is some debug information');
NOTE: Fatal exceptions will always be sent to Stack Driver., (*8)
Context informaton
You can pass user information as context like this:, (*9)
\Log::error('Something went wrong', [
'person' => ['id' => 123, 'username' => 'John Doe', 'email' => 'john@doe.com']
]);
Or pass some extra information:, (*10)
\Log::warning('Something went wrong', [
'download_size' => 3432425235
]);