com.google.cloud.logging
A client for Cloud Logging - Real-time log management and analysis.
Here's a simple usage example for using google-cloud from Compute Engine/App Engine Flexible. This example shows how to write and list log entries. For the complete source code see WriteAndListLogEntries.java .
LoggingOptions
options
=
LoggingOptions
.
getDefaultInstance
();
try
(
Logging
logging
=
options
.
getService
())
{
LogEntry
firstEntry
=
LogEntry
.
newBuilder
(
StringPayload
.
of
(
"message"
))
.
setLogName
(
"test-log"
)
.
setResource
(
MonitoredResource
.
builder
(
"global"
)
.
addLabel
(
"project_id"
,
options
.
getProjectId
())
.
build
())
.
build
();
logging
.
write
(
Collections
.
singleton
(
firstEntry
));
Page
This second example shows how to use a java.util.logging.Logger
to write log entries
to Cloud Logging. The snippet installs a Cloud Logging handler using LoggingHandler.addHandler(Logger, LoggingHandler)
. Notice that this could also be done through
the logging.properties
file, adding the following line:
com.google.cloud.examples.logging.snippets.AddLoggingHandler.handlers=com.google.cloud.logging.LoggingHandler
For the complete source code see AddLoggingHandler.java .
Logger
logger
=
Logger
.
getLogger
(
AddLoggingHandler
.
class
.
getName
());
LoggingHandler
.
addHandler
(
logger
,
new
LoggingHandler
());
logger
.
warning
(
"test warning"
);
See Also: Cloud Logging
com.google.cloud.logging.spi
com.google.cloud.logging.spi.v2
com.google.cloud.logging.testing
A testing helper for Cloud Logging.
A simple usage example:
Before the test:
RemoteLoggingHelper
helper
=
RemoteLoggingHelper
.
create
();
Logging
logging
=
helper
.
getOptions
().
getService
();
Format resource names to avoid name clashes:
String
metricName
=
RemoteLoggingHelper
.
formatForTest
(
"test-metric"
);
See Also: Google Cloud Java tools for testing
com.google.cloud.logging.v2
The interfaces provided are listed below, along with usage samples.
LoggingClient
Service Description: Service for ingesting and querying logs.
Sample for LoggingClient:
try
(
LoggingClient
loggingClient
=
LoggingClient
.
create
())
{
LogName
logName
=
LogName
.
ofProjectLogName
(
"[PROJECT]"
,
"[LOG]"
);
loggingClient
.
deleteLog
(
logName
);
}
ConfigClient
Service Description: Service for configuring sinks used to route log entries.
Sample for ConfigClient:
try
(
ConfigClient
configClient
=
ConfigClient
.
create
())
{
GetBucketRequest
request
=
GetBucketRequest
.
newBuilder
()
.
setName
(
LogBucketName
.
ofProjectLocationBucketName
(
"[PROJECT]"
,
"[LOCATION]"
,
"[BUCKET]"
)
.
toString
())
.
build
();
LogBucket
response
=
configClient
.
getBucket
(
request
);
}
MetricsClient
Service Description: Service for configuring logs-based metrics.
Sample for MetricsClient:
try
(
MetricsClient
metricsClient
=
MetricsClient
.
create
())
{
LogMetricName
metricName
=
LogMetricName
.
of
(
"[PROJECT]"
,
"[METRIC]"
);
LogMetric
response
=
metricsClient
.
getLogMetric
(
metricName
);
}