本文整理匯總了Java中com.google.cloud.MonitoredResource類的典型用法代碼示例。如果您正苦於以下問題:Java MonitoredResource類的具體用法?Java MonitoredResource怎麽用?Java MonitoredResource使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
MonitoredResource類屬於com.google.cloud包,在下文中一共展示了MonitoredResource類的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import com.google.cloud.MonitoredResource; //導入依賴的package包/類
/** Expects a new or existing Stackdriver log name as the first argument.*/
public static void main(String... args) throws Exception {
// Instantiates a client
Logging logging = LoggingOptions.getDefaultInstance().getService();
// The name of the log to write to
String logName = args[0]; // "my-log";
// The data to write to the log
String text = "Hello, world!";
LogEntry entry = LogEntry.newBuilder(StringPayload.of(text))
.setSeverity(Severity.ERROR)
.setLogName(logName)
.setResource(MonitoredResource.newBuilder("global").build())
.build();
// Writes the log entry asynchronously
logging.write(Collections.singleton(entry));
System.out.printf("Logged: %s%n", text);
}
示例2: testWriteAndListLogs
import com.google.cloud.MonitoredResource; //導入依賴的package包/類
@Test(timeout = 60000)
public void testWriteAndListLogs() throws Exception {
// write a log entry
LogEntry entry = LogEntry.newBuilder(StringPayload.of("Hello world again"))
.setLogName(TEST_WRITE_LOG)
.setResource(MonitoredResource.newBuilder("global").build())
.build();
logging.write(Collections.singleton(entry));
// flush out log immediately
logging.flush();
bout.reset();
// Check if the log is listed yet
while (bout.toString().isEmpty()) {
ListLogs.main(TEST_WRITE_LOG);
Thread.sleep(5000);
}
assertThat(bout.toString().contains("Hello world again")).isTrue();
}
示例3: doPost
import com.google.cloud.MonitoredResource; //導入依賴的package包/類
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
JsonNode body = objectMapper.readTree(req.getReader());
String logName = body.path("log_name").asText();
String token = body.path("token").asText();
Severity severity = Severity.valueOf(body.path("level").asText());
LogEntry logEntry = LogEntry.newBuilder(Payload.StringPayload.of(token))
.setLogName(logName)
.setSeverity(severity)
.setResource(MonitoredResource.newBuilder("global").build())
.build();
logging.write(Collections.singletonList(logEntry));
resp.setContentType("text/plain");
resp.getWriter().println(URLEncoder.encode(logName, "UTF-8"));
}