com.google.cloud.logging.Severity Java Examples

The following examples show how to use com.google.cloud.logging.Severity. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QuickstartSample.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/** Expects a new or existing Stackdriver log name as the first argument.*/
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 = args[0];  // "my-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();

  // Writes the log entry asynchronously
  logging.write(Collections.singleton(entry));

  System.out.printf("Logged: %s%n", text);
}
 
Example #2
Source File: CustomLoggingServlet.java    From tomcat-runtime with Apache License 2.0 6 votes vote down vote up
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {

  JsonNode body = objectMapper.readTree(req.getReader());
  String logName = body.path("log_name").asText();
  String token = body.path("token").asText();
  Severity severity = Severity.valueOf(body.path("level").asText());

  LogEntry logEntry = LogEntry.newBuilder(Payload.StringPayload.of(token))
      .setLogName(logName)
      .setSeverity(severity)
      .setResource(MonitoredResource.newBuilder("global").build())
      .build();

  logging.write(Collections.singletonList(logEntry));

  resp.setContentType("text/plain");
  resp.getWriter().println(URLEncoder.encode(logName, "UTF-8"));
}
 
Example #3
Source File: LoggingExample.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
@Override
LogEntry parse(String... args) throws Exception {
  if (args.length >= 3) {
    if ((args.length & 0x1) != 0x1) {
      throw new IllegalArgumentException("Labels must be a list of key-value pairs.");
    }
    String logName = args[0];
    Severity severity = Severity.valueOf(args[1].toUpperCase());
    String message = args[2];
    Map<String, String> labels = Maps.newHashMapWithExpectedSize((args.length - 3) / 2);
    for (int i = 3; i < args.length; i += 2) {
      labels.put(args[i], args[i + 1]);
    }
    return LogEntry.newBuilder(StringPayload.of(message))
        .setLogName(logName)
        .setSeverity(severity)
        .setLabels(labels)
        .build();
  } else {
    throw new IllegalArgumentException("Missing required arguments.");
  }
}
 
Example #4
Source File: StackdriverMonitor.java    From data-transfer-project with Apache License 2.0 4 votes vote down vote up
@Override
public void severe(Supplier<String> supplier, Object... data) {
  log(Severity.ERROR, supplier, data);
}
 
Example #5
Source File: StackdriverMonitor.java    From data-transfer-project with Apache License 2.0 4 votes vote down vote up
@Override
public void info(Supplier<String> supplier, Object... data) {
  log(Severity.INFO, supplier, data);
}
 
Example #6
Source File: StackdriverMonitor.java    From data-transfer-project with Apache License 2.0 4 votes vote down vote up
@Override
public void debug(Supplier<String> supplier, Object... data) {
  log(Severity.NOTICE, supplier, data);
}
 
Example #7
Source File: StackdriverMonitor.java    From data-transfer-project with Apache License 2.0 4 votes vote down vote up
private void log(Severity severity, Supplier<String> supplier, Object... data) {
  MonitoredResource.Builder resourceBuilder =
      MonitoredResource.newBuilder("generic_task")
          .addLabel("project_id", projectId)
          // This is slightly backwards as in GCP a job can have many tasks
          // but to line up with the DTP terminology around a job we'll use
          // GCP's job to line up with DTP's job.
          .addLabel("task_id", getHostName());

  if (null != jobId) {
    resourceBuilder.addLabel("job", jobId);
  }

  StringBuilder logMessage = new StringBuilder();
  logMessage.append(supplier.get());

  if (data != null) {
    for (Object datum : data) {
      if (datum instanceof Throwable) {
        logMessage.append(format("\n%s", Throwables.getStackTraceAsString(((Throwable) datum))));
      } else if (datum instanceof UUID) {
        logMessage.append(format("\nJobId: %s", ((UUID) datum)));
      } else if (datum instanceof EventCode) {
        logMessage.append(format("\nEventCode: %s", (EventCode) datum));
      } else if (datum != null) {
        logMessage.append(format("\n%s", datum));
      }
    }
  }

  LogEntry entry =
      LogEntry.newBuilder(Payload.StringPayload.of(logMessage.toString()))
          .setSeverity(severity)
          .setLogName(LOG_NAME)
          .setResource(resourceBuilder.build())
          .build();

  try {
    // Writes the log entry asynchronously
    logging.write(Collections.singleton(entry));
  } catch (Throwable t) {
    System.out.println("Problem logging: " + t.getMessage());
    t.printStackTrace(System.out);
  }
}