首页 > 解决方案 > 使用新的 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 与我的项目集成,并且我想使用更新的版本。有谁知道这可能的原因?

标签: javagoogle-cloud-platformstackdrivergoogle-cloud-stackdrivergoogle-cloud-logging

解决方案


我遇到了一个类似的问题,我让它以两种方式工作:

  1. 通过将写入同步性设置为同步来运行同步调用:
// Writes the log entry synchronously
logging.setWriteSynchronicity(Synchronicity.SYNC);
logging.write(Collections.singleton(entry));
  1. 通过刷新异步调用:
// Writes the log entry asynchronously
logging.write(Collections.singleton(entry));
logging.flush();

我在用着com.google.cloud:google-cloud-logging:1.101.2


推荐阅读