MonologStackdriver
This package enables you to push your Monolog log entries to Stackdriver which is part of the Google Cloud Platform., (*1)
The supplied StackdriverHandler copies the given log level into the Stackdriver's severity based on your log method., (*2)
It also respects the context argument which allows you to send extra contextual data with your log message. This will be stored in the log message under jsonPayload.data., (*3)
Configuration
Service account
With our samples we assume you have a service account Google Developers Console JSON key file available within your project to point at with read rights., (*4)
If you don't have this file yet, you can create it via Google Cloud Platform - IAM & Admin - Service accounts. Please make sure you have at least the role of Logs writer enabled., (*5)
Google\Cloud\Logging\LoggingClient options
Please read the documentation for the Google\Cloud\Logging\LoggingClient for other authentication options and further specific connection and setup., (*6)
Google\Cloud\Logging\Logger options
Please read the documentation for the Google\Cloud\Logging\Logger setup via Google\Cloud\Logging\LoggingClient for specific details about these options., (*7)
This set of options will allow you to set the default resource type and it's related labels that apply to all the logs. Please read Method: monitoredResourceDescriptors.list and do the "Try this API" to get a full list of the specific labels per resource., (*8)
Google\Cloud\Logging\Entry options
Please read the documentation for the Google\Cloud\Logging\Entry setup via Google\Cloud\Logging\Logger for specific details about these options., (*9)
By default, you can add Stackdriver specific log entry options by adding these wrapped in the stackdriver-key inside the context array. Very useful to add log entry specific labels for instance., (*10)
$context['stackdriver'] = [
// stackdriver related entry options
];
If you need to, you can override this key name by setting $entryOptionsWrapper to your own value (string) when using StackdriverHandler::__construct., (*11)
It is also possible to set path to credentials and project id via the global constant., (*12)
define('GOOGLE_APPLICATION_CREDENTIALS', '/path/to/service-account-key-file.json');
define('GOOGLE_CLOUD_PROJECT', 'eg-my-project-id-148223');
Pick your framework for some specific setup
Vanilla usage
use Monolog\Logger;
use CodeInternetApplications\MonologStackdriver\StackdriverHandler;
// ( ... )
// GCP Project ID
$projectId = 'eg-my-project-id-148223';
// See Google\Cloud\Logging\LoggingClient::__construct
$loggingClientOptions = [
'keyFilePath' => '/path/to/service-account-key-file.json'
];
// init handler
$stackdriverHandler = new StackdriverHandler(
$projectId,
$loggingClientOptions
);
// init logger with StackdriverHandler
$logger = new Logger('stackdriver', [$stackdriverHandler]);
// basic info log with contextual data
$logger->info('New order', ['orderId' => 1001]);
// ( ... )
// add specific log entry options, eg labels
$context = ['orderId' => 1001];
// add a 'stackdriver' entry to the context to append
// log entry specific options
$context['stackdriver'] = [
'labels' => [
'action' => 'paid'
]
];
$logger->info('Order update', $context);
// ( ... )
// add specific log entry options, eg labels and operation
$context = ['orderId' => 1001];
$context['stackdriver'] = [
'labels' => [
'order' => 'draft'
],
'operation' => [
'id' => 'order-1001',
'first' => true,
'last' => false
]
];
$logger->info('Order update', $context);
// update both label and operation
$context['stackdriver'] = [
'labels' => [
'order' => 'paid'
],
'operation' => [
'id' => 'order-1001',
'first' => false,
'last' => false
]
];
$logger->info('Order update', $context);
// update both label and operation again
$context['stackdriver'] = [
'labels' => [
'order' => 'fulfilled'
],
'operation' => [
'id' => 'order-1001',
'first' => false,
'last' => true
]
];
$logger->info('Order update', $context);