java - 使用新的 Google Cloud 日志记录 jar 时,日志未显示在 Google Cloud Platform Stackdriver 中
问题描述
我遵循了有关 Java 日志库的文档,这QuickstartSample.java
是一个简单的 API 调用,用于将数据记录到 Stackdriver。
public class QuickstartSample {
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 = "test-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();
logging.write(Collections.singleton(entry));
System.out.printf("Logged: %s%n", text);
}
}
com.google.cloud:google-cloud-logging:1.87.0
使用版本时没有显示日志条目。
它适用于旧版本com.google.cloud:google-cloud-logging:1.2.1
视窗 7 64 位
OpenJDK 8 64位
Gradle 3.0 版(也与 maven 3.6.1 相同的结果)
运行代码时控制台没有错误,两种情况下都会执行完整的程序,但只有在使用 1.2.1 版本时才会将日志发送到 Stackdriver。
我需要将 Stackdriver 与我的项目集成,并且我想使用更新的版本。有谁知道这可能的原因?
解决方案
我遇到了一个类似的问题,我让它以两种方式工作:
- 通过将写入同步性设置为同步来运行同步调用:
// Writes the log entry synchronously
logging.setWriteSynchronicity(Synchronicity.SYNC);
logging.write(Collections.singleton(entry));
- 通过刷新异步调用:
// Writes the log entry asynchronously
logging.write(Collections.singleton(entry));
logging.flush();
我在用着com.google.cloud:google-cloud-logging:1.101.2
推荐阅读
- ruby-on-rails - Safari 不会设置来自同一域(不同子域)的 cookie
- maven - Gradle 如何将 gradle-plugin 发布到 maven Central
- java - JTable:无法获取列标题和水平滚动条
- r - 过滤数据集中的行以查找 r 中的不同单词
- python-3.x - 获取列表中的单个字符而不是单词本身
- php - 在 PHP 或 MySQL 中计算一个范围内的最大重叠事件
- c++ - shaderc IncluderInterface,包含失败?
- swift - 混合多个 MTLTexture
- .net - 相对于 VB.NET 墨卡托投影上的坐标的 X、Y 位置不正确
- reactjs - 火狐中的 NS_ERROR_NET_RESET