Google\Cloud\Logging\PsrLogger::log PHP Method

log() public method

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' ] ] ]);
public log ( string | integer $level, string $message, array $context = [] )
$level string | integer The severity of the log entry.
$message string The message to log.
$context array { 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/Shared.Types/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/Shared.Types/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/Shared.Types/LogEntry#logentryoperation) for more information. }
    public function log($level, $message, array $context = [])
    {
        $this->validateLogLevel($level);
        $options = [];
        if (isset($context['exception']) && $context['exception'] instanceof \Exception) {
            $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']];
        $entry = $this->logger->entry($jsonPayload + $processedData['context'], $options + ['severity' => $level]);
        $this->logger->write($entry);
    }