How to use google cloud logging at a lumen/laravel application
✔ Recommended Answer
Lumen seems to work the same logging infrastructure as Laravel does. By default it uses Monolog which is a PSR-3 compatible logging library but I think the following code might also work for logging with Google cloud:
Create the logger factory class:
class CreateGoogleCloudLogger { public function __invoke() { $logging = new LoggingClient([ 'projectId' => $projectId ]); return $logging->psrLogger('app'); }}
Change your .env
file:
LOG_CHANNEL=googlecloud
Then add this in your AppServiceProvider
register function
config(['logging.channels.googlecloud' => [ 'driver' => 'custom', 'via' => App\Logging\CreateGoogleCloudLogger::class,] ]);
This should use the factory to create a PSR-3 compatible logger.
Source: stackoverflow.com
Answered By: apokryfos
To use Google Cloud Logging in a Lumen/Laravel application, you can follow these steps:
Set up a Google Cloud Platform project: If you haven't already, create a Google Cloud Platform project and enable the Cloud Logging API.
Install the Google Cloud Logging package: You can install the Google Cloud Logging package using Composer. Run the following command in your terminal:
bashcomposer require google/cloud-logging
Set up authentication: To authenticate your application with Google Cloud Platform, you can set up a service account and download the JSON key file. You can then set the
GOOGLE_APPLICATION_CREDENTIALS
environment variable to the path of the key file.Configure the logging driver: In your Lumen/Laravel application, open the
config/logging.php
file and add the following driver configuration:arduino'stack' => [ 'driver' => 'stack', 'channels' => ['gcp'], ], 'gcp' => [ 'driver' => 'monolog', 'handler' => Google\Cloud\Logging\LoggingHandler::class, 'handler_with' => [ 'projectId' => env('GOOGLE_CLOUD_PROJECT_ID'), 'logName' => 'laravel', ], ],
This configuration will create a logging channel called
gcp
that uses theGoogle\Cloud\Logging\LoggingHandler
handler class.Use the logging channel: In your application code, you can use the
Log
facade to log messages to thegcp
channel. For example:cssLog::channel('gcp')->info('This is a log message.');
This will log a message to the Cloud Logging API with the
info
severity level.
That's it! Your Lumen/Laravel application should now be configured to use Google Cloud Logging for logging.
Comments
Post a Comment