我有一个简单的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);
}
}
最佳答案
您可以通过以下方式使用命令:
gcloud app logs tail --service=my-service --version=my-app-version
为了指定服务和版本,请参阅“如果您没有真正获取所有日志”。查看所有选项的列表here。您还可以通过转到Stackdriver-> Logging-> Logs查看所有日志:
到达那里后,您可以按应用程序版本过滤日志:
另外请注意,根据您的请求,有时您只会看到某些类型的日志。我将您的代码粘贴到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中看到以下结果: