Reference documentation and code samples for the Stackdriver Logging Client class LoggingClient.
Google Stackdriver Logging allows you to store, search, analyze, monitor, and alert on log data and events from Google Cloud Platform and Amazon Web Services. Find more information at the Google Stackdriver Logging docs.
This client supports transport over REST or gRPC.
In order to enable gRPC support please make sure to install and enable the gRPC extension through PECL:
$ pecl install grpc ```php NOTE: Support for gRPC is currently at an Alpha quality level, meaning it is still a work in progress and is more likely to get backwards-incompatible updates. When using gRPC in production environments, it is highly recommended that you make use of the Protobuf PHP extension for improved performance. Protobuf can be installed via [PECL](https://pecl.php.net). $ pecl install protobuf
 Example: use Google\Cloud\Logging\LoggingClient;
$logging = new LoggingClient(); ```
Methods
psrBatchLogger
Create a PsrLogger with batching enabled.
| Parameters | |
|---|---|
| Name | Description | 
| name | stringThe name of the log to write entries to. | 
| options | arrayConfiguration options. | 
| ↳ apiEndpoint | stringA hostname with optional port to use in place of the service's default endpoint. | 
| ↳ messageKey | stringThe key in the  | 
| ↳ resource | arrayThe monitored resource to associate log entries with. Defaults to type global. | 
| ↳ labels | arrayA set of user-defined (key, value) data that provides additional information about the log entry. | 
| ↳ metadataProvider | MetadataProviderInterfaceDefaults to An automatically chosen provider, based on detected environment settings. | 
| ↳ debugOutput | boolWhether or not to output debug information. Please note debug output currently only applies in CLI based applications. Defaults to  | 
| ↳ batchOptions | arrayA set of options for a BatchJob. Google\Cloud\Core\Batch\BatchJob::__construct() for more details. Defaults to ['batchSize' => 1000, 'callPeriod' => 2.0, 'numWorkers' => 2]. | 
| ↳ clientConfig | arrayConfiguration options for the Logging client used to handle processing of batch items. For valid options please see Google\Cloud\Logging\LoggingClient::__construct(). | 
| ↳ batchRunner | BatchRunnerA BatchRunner object. Mainly used for the tests to inject a mock. Defaults to a newly created BatchRunner. | 
| ↳ closureSerializer | ClosureSerializerInterfaceAn implementation responsible for serializing closures used in the  | 
| Returns | |
|---|---|
| Type | Description | 
| Google\Cloud\Logging\PsrLogger | |
__construct
Create a Logging client.
| Parameters | |
|---|---|
| Name | Description | 
| config | arrayConfiguration options. | 
| ↳ projectId | stringThe project ID from the Google Developer's Console. | 
| ↳ authCache | CacheItemPoolInterfaceA cache for storing access tokens. Defaults to a simple in memory implementation. | 
| ↳ authCacheOptions | arrayCache configuration options. | 
| ↳ authHttpHandler | callableA handler used to deliver Psr7 requests specifically for authentication. | 
| ↳ credentialsFetcher | FetchAuthTokenInterfaceA credentials fetcher instance. | 
| ↳ httpHandler | callableA handler used to deliver Psr7 requests. Only valid for requests sent over REST. | 
| ↳ keyFile | arrayThe contents of the service account credentials .json file retrieved from the Google Developer's Console. Ex:  | 
| ↳ keyFilePath | stringThe full path to your service account credentials .json file retrieved from the Google Developers Console. | 
| ↳ requestTimeout | floatSeconds to wait before timing out the request. Defaults to  | 
| ↳ retries | intNumber of retries for a failed request. Defaults to  | 
| ↳ scopes | arrayScopes to be used for the request. | 
| ↳ quotaProject | stringSpecifies a user project to bill for access charges associated with the request. | 
| ↳ transport | stringThe transport type used for requests. May be either  | 
createSink
Create a sink.
Example:
$sink = $logging->createSink('my-sink', 'storage.googleapis.com/my-bucket'); | Parameters | |
|---|---|
| Name | Description | 
| name | stringThe name of the sink. | 
| destination | stringThe export destination. Please see Exporting Logs With Sinks for more information and examples. | 
| options | arrayConfiguration options. | 
| ↳ filter | string | 
| ↳ outputVersionFormat | stringThe log entry version to use for this sink's exported log entries. This version does not have to correspond to the version of the log entry when it was written to Stackdriver Logging. May be either  | 
| Returns | |
|---|---|
| Type | Description | 
| Google\Cloud\Logging\Sink | |
sink
Lazily instantiates a sink. There are no network requests made at this point. To see the operations that can be performed on a sink please see Google\Cloud\Logging\Sink.
Example:
$sink = $logging->sink('my-sink'); echo $sink->name(); | Parameter | |
|---|---|
| Name | Description | 
| name | stringThe name of the sink. | 
| Returns | |
|---|---|
| Type | Description | 
| Google\Cloud\Logging\Sink | |
sinks
Fetches sinks associated with your project.
Example:
$sinks = $logging->sinks(); foreach ($sinks as $sink) { echo $sink->name() . PHP_EOL; } | Parameters | |
|---|---|
| Name | Description | 
| options | arrayConfiguration options. | 
| ↳ pageSize | intThe maximum number of results to return per request. | 
| ↳ resultLimit | intLimit the number of results returned in total. Defaults to  | 
| ↳ pageToken | stringA previously-returned page token used to resume the loading of results from a specific point. | 
| Returns | |
|---|---|
| Type | Description | 
| Google\Cloud\Core\Iterator\ItemIterator<\google\cloud\logging\sink> | |
createMetric
Create a metric.
Example:
$metric = $logging->createMetric( 'my-metric', 'logName = projects/my-project/logs/my-log' ); | Parameters | |
|---|---|
| Name | Description | 
| name | stringThe name of the metric. | 
| filter | string | 
| options | arrayConfiguration Options. | 
| ↳ description | stringA description of the metric. | 
| Returns | |
|---|---|
| Type | Description | 
| Google\Cloud\Logging\Metric | |
metric
Lazily instantiates a metric. There are no network requests made at this point. To see the operations that can be performed on a metric please see Google\Cloud\Logging\Metric.
Example:
$metric = $logging->metric('my-metric'); echo $metric->name(); | Parameter | |
|---|---|
| Name | Description | 
| name | stringThe name of the metric. | 
| Returns | |
|---|---|
| Type | Description | 
| Google\Cloud\Logging\Metric | |
metrics
Fetches metrics associated with your project.
Example:
$metrics = $logging->metrics(); foreach ($metrics as $metric) { echo $metric->name() . PHP_EOL; } | Parameters | |
|---|---|
| Name | Description | 
| options | arrayConfiguration options. | 
| ↳ pageSize | intThe maximum number of results to return per request. | 
| ↳ resultLimit | intLimit the number of results returned in total. Defaults to  | 
| ↳ pageToken | stringA previously-returned page token used to resume the loading of results from a specific point. | 
| Returns | |
|---|---|
| Type | Description | 
| Google\Cloud\Core\Iterator\ItemIterator<\google\cloud\logging\metric> | |
entries
Fetches log entries.
Example:
$entries = $logging->entries(); foreach ($entries as $entry) { echo $entry->info()['textPayload'] . PHP_EOL; } // Use an advanced logs filter to fetch only entries from a specified log. $entries = $logging->entries([ 'filter' => 'logName = projects/my-project/logs/my-log' ]); foreach ($entries as $entry) { echo $entry->info()['textPayload'] . PHP_EOL; } | Parameters | |
|---|---|
| Name | Description | 
| options | arrayConfiguration options. | 
| ↳ projectIds | string[]A list of projectIds to fetch entries from in addition to entries found in the project bound to this client. | 
| ↳ resourceNames | string[]One or more cloud resources from which to retrieve log entries. Projects listed in projectIds are added to this list. Example: "projects/my-project-1A", "projects/1234567890". | 
| ↳ filter | string | 
| ↳ orderBy | stringHow the results should be sorted. Presently, the only permitted values are  | 
| ↳ pageSize | intThe maximum number of results to return per request. | 
| ↳ resultLimit | intLimit the number of results returned in total. Defaults to  | 
| ↳ pageToken | stringA previously-returned page token used to resume the loading of results from a specific point. | 
| Returns | |
|---|---|
| Type | Description | 
| Google\Cloud\Core\Iterator\ItemIterator<\google\cloud\logging\entry> | |
psrLogger
Fetches a logger which will write log entries to Stackdriver Logging and implements the PSR-3 specification.
Example:
$psrLogger = $logging->psrLogger('my-log'); // Write entries with background batching. $psrLogger = $logging->psrLogger('my-log', [ 'batchEnabled' => true ]); | Parameters | |
|---|---|
| Name | Description | 
| name | stringThe name of the log to write entries to. | 
| options | arrayConfiguration options. | 
| ↳ messageKey | stringThe key in the  | 
| ↳ resource | arrayThe monitored resource to associate log entries with. Defaults to type global. | 
| ↳ labels | arrayA set of user-defined (key, value) data that provides additional information about the log entry. | 
| ↳ metadataProvider | MetadataProviderInterfaceDefaults to An automatically chosen provider, based on detected environment settings. | 
| ↳ batchEnabled | boolDetermines whether or not to use background batching. Defaults to  | 
| ↳ debugOutput | boolWhether or not to output debug information. Please note debug output currently only applies in CLI based applications. Defaults to  | 
| ↳ debugOutputResource | resourceA resource to output debug output to. Applies only when  | 
| ↳ batchOptions | arrayA set of options for a BatchJob. Google\Cloud\Core\Batch\BatchJob::__construct() for more details. Defaults to ['batchSize' => 1000, 'callPeriod' => 2.0, 'numWorkers' => 2]. Applies only when  | 
| ↳ clientConfig | arrayConfiguration options for the Logging client used to handle processing of batch items. For valid options please see Google\Cloud\Logging\LoggingClient::__construct(). Defaults to the options provided to the current client. Applies only when  | 
| ↳ batchRunner | BatchRunnerA BatchRunner object. Mainly used for the tests to inject a mock. Defaults to a newly created BatchRunner. Applies only when  | 
| ↳ closureSerializer | ClosureSerializerInterfaceAn implementation responsible for serializing closures used in the  | 
| Returns | |
|---|---|
| Type | Description | 
| Google\Cloud\Logging\PsrLogger | |
logger
Fetches a logger which will write log entries to Stackdriver Logging.
Example:
$logger = $logging->logger('my-log'); | Parameters | |
|---|---|
| Name | Description | 
| name | stringThe name of the log to write entries to. | 
| options | arrayConfiguration options. | 
| ↳ resource | arrayThe monitored resource to associate log entries with. Defaults to type global. | 
| ↳ labels | arrayA set of user-defined (key, value) data that provides additional information about the log entry. | 
| Returns | |
|---|---|
| Type | Description | 
| Google\Cloud\Logging\Logger | |
Constants
VERSION
Value: '1.26.0'FULL_CONTROL_SCOPE
Value: 'https://www.googleapis.com/auth/logging.admin'READ_ONLY_SCOPE
Value: 'https://www.googleapis.com/auth/logging.read'WRITE_ONLY_SCOPE
Value: 'https://www.googleapis.com/auth/logging.write'