本文整理汇总了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"));
}