Google App灵活的Java日志记录无效

时间:2017-12-17 01:10:22

标签: java google-app-engine logging google-cloud-platform app-engine-flexible

我有一个简单的servlet,如下所示。而且我无法在gcloud日志中看到任何System.out或log.info。我用了gcloud app logs tail -s my-app-name。我只看到GET和favicon的日志。请帮助我在google云端应用中启用日志。

@SuppressWarnings("serial")
@WebServlet(name = "Home", description = "Write low order IP address to response", urlPatterns = "/")
public class HomeServlet extends HttpServlet {
    Logger logger = Logger.getLogger(HomeServlet.class.getName());
    Connection conn;

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        if(logger.isLoggable(Level.FINE)) {
            log("FINE is loggable.");
            System.out.println("This is System Out. FINE is loggable");
            logger.fine("Received GET request");
        } else {
            log("FINE is not loggable");
            System.out.println("This is System Out. FINE is not loggable");
            logger.severe("Received GET request");
        }
        String path = req.getRequestURI();
        if (path.startsWith("/favicon.ico")) {
            return; // ignore the request for favicon.ico
        }

        PrintWriter out = resp.getWriter();
        resp.setContentType("text/plain");

        String userIp = req.getRemoteAddr();
        out.print("Your IP is: " + userIp);
        log("responded GET request from: " + userIp);
    }

}

1 个答案:

答案 0 :(得分:1)

您可以这样使用命令:

gcloud app logs tail --service=my-service --version=my-app-version

为了指定服务和版本,请参阅如果您没有真正获取所有日志。查看所有选项here的列表。您也可以转到 Stackdriver - >来查看所有日志。记录 - >日志

enter image description here

在那里,您可以按应用版本过滤日志:

enter image description here

另请注意,根据您提出的要求,有时您只会看到某种日志。我将您的代码粘贴到quickstart for app engine灵活,我得到了这个:

    ...........

    2017-12-18 09:40:07 my-service[my-app-version]  "GET /favicon.ico" 200
    2017-12-18 09:40:07 my-service[my-app-version]  "GET /" 200
    2017-12-18 09:40:07 my-service[my-app-version]  "GET /favicon.ico" 200
    2017-12-18 09:40:08 my-service[my-app-version]  "GET /" 200
    2017-12-18 09:40:13 my-service[my-app-version]  org.eclipse.jetty.server.handler.ContextHandler.root: com.example.appengine.gettingstartedjava.helloworld.HomeServlet: FINE is not loggable
    2017-12-18 09:40:13 my-service[my-app-version]  com.example.appengine.gettingstartedjava.helloworld.HomeServlet: Received GET request
    2017-12-18 09:40:13 my-service[my-app-version]  org.eclipse.jetty.server.handler.ContextHandler.root: com.example.appengine.gettingstartedjava.helloworld.HomeServlet: responded GET request from: 35.187.117.231
    2017-12-18 09:40:13 my-service[my-app-version]  This is System Out. FINE is not loggable
    2017-12-18 09:40:15 my-service[my-app-version]  This is System Out. FINE is not loggable

..........

除此之外,您还可以使用Stackdriver Logging Client Libraries。首先将此添加到您的POM依赖项:

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-logging</artifactId>
    <version>1.14.0</version>
</dependency>

然后使用quickstart并将此代码粘贴到HelloServlet.java中。你的代码看起来像这样:

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    PrintWriter out = resp.getWriter();
    out.println("Hello, world - Flex Servlet");

    Logging logging = LoggingOptions.getDefaultInstance().getService();

    String logName = "My-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);

}

你可以在Stackdriver中看到这个结果:

enter image description here