Stackdriver Logging Client - Class Logger (1.25.5)

Reference documentation and code samples for the Stackdriver Logging Client class Logger.

A logger used to write entries to Google Stackdriver Logging.

Example:

use Google\Cloud\Logging\LoggingClient;

$logging = new LoggingClient();

$logger = $logging->logger('my-log');

Methods

__construct

Parameters
NameDescription
connection Google\Cloud\Logging\Connection\ConnectionInterface

Represents a connection to Stackdriver Logging.

name string

The name of the log to write entries to.

projectId string

The project's ID.

resource array

[optional] The monitored resource to associate log entries with. Defaults to type global.

labels array

[optional] A set of user-defined (key, value) data that provides additional information about the log entries.

delete

Deletes this log and all its log entries. The log will reappear if it receives new entries.

Example:

$logger->delete();
Parameter
NameDescription
options array

[optional] Configuration Options.

entries

Fetches entries scoped to the selected log.

Please note that a default filter specifying the logName will be applied for you.

Example:

$entries = $logger->entries();

foreach ($entries as $entry) {
    echo $entry->info()['textPayload'] . PHP_EOL;
}
// Use an advanced logs filter to fetch only entries with a specific payload.
$entries = $logger->entries([
    'filter' => 'textPayload = "hello world"'
]);

foreach ($entries as $entry) {
    echo $entry->info()['textPayload'] . PHP_EOL;
}
Parameters
NameDescription
options array

Configuration options.

↳ filter string

An advanced logs filter.

↳ orderBy string

How the results should be sorted. Presently, the only permitted values are timestamp asc and timestamp desc. Defaults to "timestamp asc".

↳ pageSize int

The maximum number of results to return per request.

↳ resultLimit int

Limit the number of results returned in total. Defaults to 0 (return all results).

↳ pageToken string

A previously-returned page token used to resume the loading of results from a specific point.

Returns
TypeDescription
Google\Cloud\Core\Iterator\ItemIterator<\google\cloud\logging\entry>

entry

Creates an entry which which can be written to a log. In order to write the entry to the log please use Google\Cloud\Logging\Logger::write() or Google\Cloud\Logging\Logger::writeBatch().

Example:

// Create an entry with a key/value set of data
$entry = $logger->entry(['user' => 'calvin']);
// Create an entry with a string
$entry = $logger->entry('my message');
// Create an entry with a severity of `EMERGENCY` and specifying a resource.
$entry = $logger->entry('my message', [
    'severity' => Logger::EMERGENCY,
    'resource' => [
        'type' => 'gcs_bucket',
        'labels' => [
            'bucket_name' => 'my-bucket'
        ]
    ]
]);
Parameters
NameDescription
data array|string

The data to log. When providing a string the data will be stored as a textPayload type. When providing an array the data will be stored as a jsonPayload type.

options array

Configuration options.

↳ resource array

The monitored resource to associate this log entry with. Defaults to type global.

↳ timestamp DateTimeInterface|Timestamp|string|null

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.

↳ severity string|int

The severity of the log entry. Defaults to "DEFAULT".

↳ insertId string

A unique identifier for the log entry.

↳ httpRequest array

Information about the HTTP request associated with this log entry, if applicable. Please see the API docs for more information.

↳ labels array

A set of user-defined (key, value) data that provides additional information about the log entry.

↳ operation array

Additional information about a potentially long-running operation with which a log entry is associated. Please see the API docs for more information.

↳ spanId string

Optional. The span ID within the trace associated with the log entry. For Trace spans, this is the same format that the Trace API v2 uses: a 16-character hexadecimal encoding of an 8-byte array, such as 000000000000004a.

↳ trace string

Optional. Resource name of the trace associated with the log entry, if any. If it contains a relative resource name, the name is assumed to be relative to //tracing.googleapis.com. Example: projects/my-projectid/traces/06796866738c859f2f19b7cfb3214824.

↳ traceSampled bool

True means that the trace resource name in the trace field was sampled for storage in a trace backend. False means that the trace was not sampled for storage when this log entry was written, or the sampling decision was unknown at the time. A non-sampled trace value is still useful as a request correlation identifier. The default is False.

↳ sourceLocation array

Source code location information associated with the log entry, if any. Please see the API docs for more information.

Returns
TypeDescription
Google\Cloud\Logging\Entry

write

Write a single entry to the log.

Example:

// Writing a simple log entry.
$logger->write('a log entry');
// Writing a simple entry with a key/value set of data and a severity of `EMERGENCY`.
$logger->write(['user' => 'calvin'], [
    'severity' => Logger::EMERGENCY
]);
// Using the entry factory method to write a log entry.
$entry = $logger->entry('a log entry');
$logger->write($entry);
Parameters
NameDescription
entry array|string|Google\Cloud\Logging\Entry

The entry to write to the log.

options array

[optional] Please see {@see \Google\Cloud\Logging\Google\Cloud\Logging\Logger::entry()} to see the options that can be applied to a log entry. Please note that if the provided entry is of type Entry these options will overwrite those that may already be set on the instance.

writeBatch

Write a set of entries to the log.

Example:

$entries = [];
$entries[] = $logger->entry('my message');
$entries[] = $logger->entry('my second message');

$logger->writeBatch($entries);
Parameters
NameDescription
entries array<Google\Cloud\Logging\Entry>

Entries to write to the log.

options array

[optional] Configuration Options.

name

Returns the logger's name.

Returns
TypeDescription
string

getLogLevelMap

Returns the log level map.

Returns
TypeDescription
array

Constants

EMERGENCY

Value: 800

ALERT

Value: 700

CRITICAL

Value: 600

ERROR

Value: 500

WARNING

Value: 400

NOTICE

Value: 300

INFO

Value: 200

DEBUG

Value: 100

DEFAULT_LEVEL

Value: 0