/
LogEntryWriteHttpRequest.java
64 lines (58 loc) · 2.42 KB
/
LogEntryWriteHttpRequest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.logging;
// [START logging_write_log_entry_advanced]
import com.google.cloud.MonitoredResource;
import com.google.cloud.logging.HttpRequest;
import com.google.cloud.logging.LogEntry;
import com.google.cloud.logging.Logging;
import com.google.cloud.logging.LoggingOptions;
import com.google.cloud.logging.Payload;
import com.google.cloud.logging.Severity;
import java.util.Collections;
/** Write LogEntry with HTTP request using the Cloud Logging API. */
public class LogEntryWriteHttpRequest {
public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String logName = "log-name"; // i.e "my-log"
String payLoad = "payload"; // i.e "Hello world!"
HttpRequest httpRequest =
HttpRequest.newBuilder()
.setRequestUrl("www.example.com")
.setRequestMethod(HttpRequest.RequestMethod.GET) // Supported method GET,POST,PUT,HEAD
.setStatus(200)
.build();
createLogEntryRequest(logName, payLoad, httpRequest);
}
public static void createLogEntryRequest(String logName, String payLoad, HttpRequest httpRequest)
throws Exception {
// Instantiates a logging client
try (Logging logging = LoggingOptions.getDefaultInstance().getService()) {
// create an instance of LogEntry with HTTP request
LogEntry logEntry =
LogEntry.newBuilder(Payload.StringPayload.of(payLoad))
.setSeverity(Severity.ERROR)
.setLogName(logName)
.setHttpRequest(httpRequest)
.setResource(MonitoredResource.newBuilder("global").build())
.build();
// Writes the log entry asynchronously
logging.write(Collections.singleton(logEntry));
System.out.printf("Logged: %s", payLoad);
}
}
}
// [END logging_write_log_entry_advanced]