@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."); } }
public static void main(String... args) throws Exception { // Create a service object // Credentials are inferred from the environment LoggingOptions options = LoggingOptions.getDefaultInstance(); try (Logging logging = options.getService()) { // Create a log entry LogEntry firstEntry = LogEntry.newBuilder(StringPayload.of("message")) .setLogName("test-log") .setResource( MonitoredResource.newBuilder("global") .addLabel("project_id", options.getProjectId()) .build()) .build(); logging.write(Collections.singleton(firstEntry)); // List log entries Page<LogEntry> entries = logging.listLogEntries( EntryListOption.filter( "logName=projects/" + options.getProjectId() + "/logs/test-log")); for (LogEntry logEntry : entries.iterateAll()) { System.out.println(logEntry); } } } }
LogEntry.newBuilder(firstPayload) .addLabel("key1", "value1") .setLogName(logId) .setHttpRequest(HttpRequest.newBuilder().setStatus(500).build()) .setResource(MonitoredResource.newBuilder("global").build()) LogEntry.newBuilder(secondPayload) .addLabel("key2", "value2") .setLogName(logId) .setOperation(Operation.of("operationId", "operationProducer")) .setResource(MonitoredResource.newBuilder("cloudsql_database").build())
builder.setSeverity(Severity.fromPb(entryPb.getSeverity())); if (!entryPb.getLogName().equals("")) { builder.setLogName(ProjectLogName.parse(entryPb.getLogName()).getLog());
/** * Creates a {@code LogEntry} object given the log name, the monitored resource and the entry * payload. */ public static LogEntry of(String logName, MonitoredResource resource, Payload<?> payload) { return newBuilder(payload).setLogName(logName).setResource(resource).build(); }
/** 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); } }
LogEntry.newBuilder(STRING_PAYLOAD) .setPayload(StringPayload.of("otherPayload")) .setLogName(LOG_NAME) .setResource(RESOURCE) .setTimestamp(TIMESTAMP)
.toBuilder() .setPayload(StringPayload.of("otherPayload")) .setLogName("otherLogName") .setResource(MonitoredResource.newBuilder("global").build()) .setTimestamp(43) .toBuilder() .setPayload(STRING_PAYLOAD) .setLogName(LOG_NAME) .setResource(RESOURCE) .setTimestamp(TIMESTAMP)
builder.setSeverity(Severity.fromPb(entryPb.getSeverity())); if (!entryPb.getLogName().equals("")) { builder.setLogName(ProjectLogName.parse(entryPb.getLogName()).getLog());
/** * Creates a {@code LogEntry} object given the log name, the monitored resource and the entry * payload. */ public static LogEntry of(String logName, MonitoredResource resource, Payload<?> payload) { return newBuilder(payload).setLogName(logName).setResource(resource).build(); }