|
| 1 | +/* |
| 2 | + Copyright 2016, Google, Inc. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package com.example.logging; |
| 18 | + |
| 19 | +// [START logging_quickstart] |
| 20 | +// Imports the Google Cloud client library |
| 21 | +import com.google.cloud.MonitoredResource; |
| 22 | +import com.google.cloud.logging.LogEntry; |
| 23 | +import com.google.cloud.logging.Logging; |
| 24 | +import com.google.cloud.logging.LoggingOptions; |
| 25 | +import com.google.cloud.logging.Payload.StringPayload; |
| 26 | + |
| 27 | +import java.util.Collections; |
| 28 | + |
| 29 | +public class QuickstartSample { |
| 30 | + public static void main(String... args) throws Exception { |
| 31 | + // Instantiates a client |
| 32 | + Logging logging = LoggingOptions.defaultInstance().service(); |
| 33 | + |
| 34 | + // The name of the log to write to |
| 35 | + String logName = args[0]; // "my-log"; |
| 36 | + |
| 37 | + // The data to write to the log |
| 38 | + String text = "Hello, world!"; |
| 39 | + |
| 40 | + LogEntry entry = LogEntry.newBuilder(StringPayload.of(text)) |
| 41 | + .setLogName(logName) |
| 42 | + .setResource(MonitoredResource.builder("global").build()) |
| 43 | + .build(); |
| 44 | + |
| 45 | + // Writes the log entry |
| 46 | + logging.write(Collections.singleton(entry)); |
| 47 | + |
| 48 | + System.out.printf("Logged: %s%n", text); |
| 49 | + } |
| 50 | +} |
| 51 | +// [END logging_quickstart] |
0 commit comments