marick/laravel-google-cloud-logging

v1.0.0 2024-04-29 17:22 UTC

This package is auto-updated.

Last update: 2024-10-25 20:34:56 UTC


README

Companion packages: Cloud Scheduler, Cloud Tasks

Introduction

This package lets you use Google Cloud Logging as the log driver for Laravel.

The package will automatically detect the environment it's running in (currently supports Cloud Run or App Engine), and attach the correct labels to the log entry so the logs appear in the application service.

Installation

Install the package with Composer:

composer require marick/laravel-google-cloud-logging

Add a new logging channel in config/logging.php:

'google_cloud' => [
    'driver' => 'google_cloud',
    'location' => env('GOOGLE_CLOUD_LOGGING_LOCATION'),
],

Use the new channel:

LOG_CHANNEL=google_cloud

Important

A location is mandatory to make log entries appear in Cloud Run or App Engine.

How to

Use log context

use Illuminate\Support\Facades\Log;

Log::debug('user logged in', [
    'user' => 5,
]);

The above context will be added in Cloud Logging:

{
  "jsonPayload": {
    "message": "user logged in"
  },
  "labels": {
    "user": 5
  }
}

Use Context

use Illuminate\Support\Facades\Context;
use Illuminate\Support\Facades\Log;

Context::add('user', 5);

Log::alert('user logged in');

The above context will be added in Cloud Logging:

{
  "jsonPayload": {
    "message": "user logged in"
  },
  "labels": {
    "user": 5
  }
}