Logging/src/PsrLogger.php
public function log($level, $message, array $context = [])
    {
        $this->validateLogLevel($level);
        $options = [];

        if (isset($context['exception'])
            && ($context['exception'] instanceof \Exception || $context['exception'] instanceof \Throwable)) {
            $context['exception'] = (string) $context['exception'];
        }

        if (isset($context['stackdriverOptions'])) {
            $options = $context['stackdriverOptions'];
            unset($context['stackdriverOptions']);
        }

        $formatter = new NormalizerFormatter();
        $processor = new PsrLogMessageProcessor();
        $processedData = $processor([
            'message' => (string) $message,
            'context' => $formatter->format($context)
        ]);
        $jsonPayload = [$this->messageKey => $processedData['message']];

        // Adding labels for log request correlation.
        $labels = $this->getLabels();
        if (!empty($labels)) {
            $options['labels'] =
                (isset($options['labels'])
                 ? $options['labels']
                 : []) + $labels;
            // Copy over the value for 'appengine.googleapis.com/trace_id' to
            // `trace` option too.
            if (isset($labels['appengine.googleapis.com/trace_id'])) {
                $options['trace'] =
                    $labels['appengine.googleapis.com/trace_id'];
            }
        }
        // Adding MonitoredResource
        $resource = $this->metadataProvider->monitoredResource();
        if (!empty($resource)) {
            $options['resource'] =
                (isset($options['resource'])
                 ? $options['resource']
                 : []) + $resource;
        }
        $entry = $this->logger->entry(
            $jsonPayload + $processedData['context'],
            $options + [
                'severity' => $level
            ]
        );
        $this->sendEntry($entry);
    }
public function log($level, $message, array $context = [])
    {
        $this->validateLogLevel($level);
        $options = [];

        if (isset($context['exception'])
            && ($context['exception'] instanceof \Exception || $context['exception'] instanceof \Throwable)) {
            $context['exception'] = (string) $context['exception'];
        }

        if (isset($context['stackdriverOptions'])) {
            $options = $context['stackdriverOptions'];
            unset($context['stackdriverOptions']);
        }

        $formatter = new NormalizerFormatter();
        $processor = new PsrLogMessageProcessor();
        $processedData = $processor([
            'message' => (string) $message,
            'context' => $formatter->format($context)
        ]);
        $jsonPayload = [$this->messageKey => $processedData['message']];

        // Adding labels for log request correlation.
        $labels = $this->getLabels();
        if (!empty($labels)) {
            $options['labels'] =
                (isset($options['labels'])
                 ? $options['labels']
                 : []) + $labels;
            // Copy over the value for 'appengine.googleapis.com/trace_id' to
            // `trace` option too.
            if (isset($labels['appengine.googleapis.com/trace_id'])) {
                $options['trace'] =
                    $labels['appengine.googleapis.com/trace_id'];
            }
        }
        // Adding MonitoredResource
        $resource = $this->metadataProvider->monitoredResource();
        if (!empty($resource)) {
            $options['resource'] =
                (isset($options['resource'])
                 ? $options['resource']
                 : []) + $resource;
        }
        $entry = $this->logger->entry(
            $jsonPayload + $processedData['context'],
            $options + [
                'severity' => $level
            ]
        );
        $this->sendEntry($entry);
    }
public function log ( $ level , $ message , array $ context = [ ] ) { $ this -> validateLogLevel ( $ level ) ; $ options = [ ] ; if ( isset ( $ context [ 'exception' ] ) && ( $ context [ 'exception' ] instanceof \ Exception || $ context [ 'exception' ] instanceof \ Throwable ) ) { $ context [ 'exception' ] = ( string ) $ context [ 'exception' ] ; } if ( isset ( $ context [ 'stackdriverOptions' ] ) ) { $ options = $ context [ 'stackdriverOptions' ] ; unset ( $ context [ 'stackdriverOptions' ] ) ; } $ formatter = new NormalizerFormatter ( ) ; $ processor = new PsrLogMessageProcessor ( ) ; $ processedData = $ processor ( [ 'message' => ( string ) $ message , 'context' => $ formatter -> format ( $ context ) ] ) ; $ jsonPayload = [ $ this -> messageKey => $ processedData [ 'message' ] ] ; // Adding labels for log request correlation. $ labels = $ this -> getLabels ( ) ; if ( ! empty ( $ labels ) ) { $ options [ 'labels' ] = ( isset ( $ options [ 'labels' ] ) ? $ options [ 'labels' ] : [ ] ) + $ labels ; // Copy over the value for 'appengine.googleapis.com/trace_id' to // `trace` option too. if ( isset ( $ labels [ 'appengine.googleapis.com/trace_id' ] ) ) { $ options [ 'trace' ] = $ labels [ 'appengine.googleapis.com/trace_id' ] ; } } // Adding MonitoredResource $ resource = $ this -> metadataProvider -> monitoredResource ( ) ; if ( ! empty ( $ resource ) ) { $ options [ 'resource' ] = ( isset ( $ options [ 'resource' ] ) ? $ options [ 'resource' ] : [ ] ) + $ resource ; } $ entry = $ this -> logger -> entry ( $ jsonPayload + $ processedData [ 'context' ] , $ options + [ 'severity' => $ level ] ) ; $ this -> sendEntry ( $ entry ) ; }
Write a log entry.

Example:
```
use Google\Cloud\Logging\Logger;

$psrLogger->log(Logger::ALERT, 'alert message');
```

```
// Write a log entry using the context array with placeholders.
use Google\Cloud\Logging\Logger;

$psrLogger->log(Logger::ALERT, 'alert: {message}', [
'message' => 'my alert message'
]);
```

```
// Log information regarding an HTTP request
use Google\Cloud\Logging\Logger;

$psrLogger->log(Logger::ALERT, 'alert message', [
'stackdriverOptions' => [
'httpRequest' => [
'requestMethod' => 'GET'
]
]
]);
```

@param string|int $level The severity of the log entry.
@param string $message The message to log.
@param array $context {
Context is an associative array which can include placeholders to be
used in the `$message`. Placeholders must be delimited with a single
opening brace `{` and a single closing brace `}`. The context will be
added as additional information on the `jsonPayload`. Please note
that the key `stackdriverOptions` is reserved for logging Google
Stackdriver specific data.

@type array $stackdriverOptions['resource'] The
[monitored resource](https://cloud.google.com/logging/docs/api/reference/rest/v2/MonitoredResource)
to associate this log entry with. **Defaults to** type global.
@type array $stackdriverOptions['httpRequest'] Information about the
HTTP request associated with this log entry, if applicable.
Please see
[the API docs](https://cloud.google.com/logging/docs/api/reference/rest/v2/LogEntry#httprequest)
for more information.
@type array $stackdriverOptions['labels'] A set of user-defined
(key, value) data that provides additional information about
the log entry.
@type array $stackdriverOptions['operation'] Additional information
about a potentially long-running operation with which a log
entry is associated. Please see
[the API docs](https://cloud.google.com/logging/docs/api/reference/rest/v2/LogEntry#logentryoperation)
for more information.
@type string $stackdriverOptions['insertId'] A unique identifier for
the log entry.
@type \DateTimeInterface|Timestamp|string|null $stackdriverOptions['timestamp'] The
timestamp associated with this entry. If providing a string it
must be in RFC3339 UTC "Zulu" format. Example:
"2014-10-02T15:01:23.045123456Z". If explicitly set to `null`
the timestamp will be generated by the server at the moment the
entry is received (with nanosecond precision). **Defaults to**
the current time, generated by the client with microsecond
precision.
}
@throws InvalidArgumentException
Write a log entry .