|
| 1 | +/** |
| 2 | + * Copyright 2017 Google Inc. |
| 3 | + * |
| 4 | + * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | + * except in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * <p>http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * <p>Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | + * express or implied. See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.example.flexible.errorreporting; |
| 15 | + |
| 16 | +import com.google.cloud.ServiceOptions; |
| 17 | +import com.google.cloud.errorreporting.v1beta1.ReportErrorsServiceClient; |
| 18 | +import com.google.devtools.clouderrorreporting.v1beta1.ProjectName; |
| 19 | +import com.google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.PrintWriter; |
| 23 | +import java.io.StringWriter; |
| 24 | +import java.util.logging.Level; |
| 25 | +import java.util.logging.Logger; |
| 26 | +import javax.servlet.ServletException; |
| 27 | +import javax.servlet.annotation.WebServlet; |
| 28 | +import javax.servlet.http.HttpServlet; |
| 29 | +import javax.servlet.http.HttpServletRequest; |
| 30 | +import javax.servlet.http.HttpServletResponse; |
| 31 | + |
| 32 | +// [START flex_error_reporting] |
| 33 | +@WebServlet(name = "Error reporting", value = "/error") |
| 34 | +public class ErrorReportingExample extends HttpServlet { |
| 35 | + |
| 36 | + private Logger logger = Logger.getLogger(ErrorReportingExample.class.getName()); |
| 37 | + |
| 38 | + @Override |
| 39 | + public void doGet(HttpServletRequest req, HttpServletResponse resp) |
| 40 | + throws IOException, ServletException { |
| 41 | + |
| 42 | + // errors logged to stderr / Cloud logging with exceptions are automatically reported. |
| 43 | + logger.log(Level.SEVERE, "exception using log framework", new IllegalArgumentException()); |
| 44 | + |
| 45 | + // use the error-reporting client library to log custom error events |
| 46 | + logCustomErrorEvent(); |
| 47 | + |
| 48 | + // runtime exceptions are also automatically picked up by error reporting. |
| 49 | + throw new RuntimeException("this is a runtime exception"); |
| 50 | + } |
| 51 | + |
| 52 | + private void logCustomErrorEvent() { |
| 53 | + try (ReportErrorsServiceClient reportErrorsServiceClient = ReportErrorsServiceClient.create()) { |
| 54 | + // custom exception logged using the API |
| 55 | + Exception e = new Exception("custom event reported using the API"); |
| 56 | + // Events reported using the API must contain a stack trace message |
| 57 | + StringWriter stackTrace = new StringWriter(); |
| 58 | + e.printStackTrace(new PrintWriter(stackTrace)); |
| 59 | + ReportedErrorEvent errorEvent = |
| 60 | + ReportedErrorEvent.getDefaultInstance() |
| 61 | + .toBuilder() |
| 62 | + .setMessage(stackTrace.toString()) |
| 63 | + .build(); |
| 64 | + // default project id |
| 65 | + ProjectName projectName = ProjectName.create(ServiceOptions.getDefaultProjectId()); |
| 66 | + reportErrorsServiceClient.reportErrorEvent(projectName, errorEvent); |
| 67 | + } catch (Exception e) { |
| 68 | + logger.log(Level.SEVERE, "Exception encountered logging custom event", e); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | +// [END flex_error_reporting] |
0 commit comments