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:


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.


Answered By: apokryfos

Method #2

To use Google Cloud Logging in a Lumen/Laravel application, you can follow these steps:

  1. Set up a Google Cloud Platform project: If you haven't already, create a Google Cloud Platform project and enable the Cloud Logging API.

  2. Install the Google Cloud Logging package: You can install the Google Cloud Logging package using Composer. Run the following command in your terminal:

    composer require google/cloud-logging
  3. 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.

  4. Configure the logging driver: In your Lumen/Laravel application, open the config/logging.php file and add the following driver configuration:

    '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 the Google\Cloud\Logging\LoggingHandler handler class.

  5. Use the logging channel: In your application code, you can use the Log facade to log messages to the gcp channel. For example:

    Log::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.


Most Popular

PhpStorm, return value is expected to be 'A', 'object' returned

Remove Unicode Zero Width Space PHP

Laravel file upload returns forbidden 403, file permission is 700 not 755