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

问题描述 投票:0回答:1

我有一个简单的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);
    }

}
java google-app-engine logging google-cloud-platform app-engine-flexible
1个回答
1
投票

您可以这样使用命令:

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

为了指定服务和版本,请参阅如果您没有真正获得所有日志。查看所有选项here的列表。您还可以通过转到Stackdriver - > Logging - > Logs来查看所有日志:

enter image description here

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

enter image description here

另请注意,根据您的要求,有时您只会看到某种日志。我在quickstart中将您的代码粘贴到应用程序引擎灵活,我得到了这个:

    ...........

    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

© www.soinside.com 2019 - 2024. All rights reserved.