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
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
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();
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;
}
options
array
Configuration options.
↳ 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.
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'
]
]
]);
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
↳ 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 tothe 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.
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);
entry
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);
entries
options
array
[optional] Configuration Options.
name
Returns the logger's name.
string
getLogLevelMap
Returns the log level map.
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