com.google.cloud.logging.LoggingOptions Java Examples

The following examples show how to use com.google.cloud.logging.LoggingOptions. 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: TraceSampleApplicationTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Before
public void setupTraceClient() throws IOException {
	this.url = String.format("http://localhost:%d/", this.port);

	// Create a new RestTemplate here because the auto-wired instance has built-in instrumentation
	// which interferes with us setting the 'x-cloud-trace-context' header.
	this.testRestTemplate = new TestRestTemplate();

	this.logClient = LoggingOptions.newBuilder()
			.setProjectId(this.projectIdProvider.getProjectId())
			.setCredentials(this.credentialsProvider.getCredentials())
			.build()
			.getService();

	ManagedChannel channel = ManagedChannelBuilder
			.forTarget("dns:///cloudtrace.googleapis.com")
			.build();

	this.traceServiceStub = TraceServiceGrpc.newBlockingStub(channel)
			.withCallCredentials(MoreCallCredentials.from(this.credentialsProvider.getCredentials()));
}
 
Example #2
Source File: LoggingAppender.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
/**
 * Wraps {@link com.google.cloud.logging.logback.LoggingAppender#getLoggingOptions()} to
 * add {@link UserAgentHeaderProvider} configuration, so that usage can be properly
 * attributed to Spring Cloud GCP.
 */
@Override
protected LoggingOptions getLoggingOptions() {

	if (loggingOptions == null) {
		LoggingOptions.Builder loggingOptionsBuilder = LoggingOptions.newBuilder();

		// only credentials are set in the options of the parent class
		Credentials credentials = super.getLoggingOptions().getCredentials();
		if (credentials != null) {
			loggingOptionsBuilder.setCredentials(credentials);
		}

		// set User-Agent
		loggingOptionsBuilder.setHeaderProvider(new UserAgentHeaderProvider(this.getClass()));

		this.loggingOptions = loggingOptionsBuilder.build();
	}

	return this.loggingOptions;
}
 
Example #3
Source File: ListLogs.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/** Expects an existing Stackdriver log name as an argument. */
public static void main(String... args) throws Exception {
  // [START logging_list_log_entries]
  // Instantiates a client
  LoggingOptions options = LoggingOptions.getDefaultInstance();

  String logName = args[0];

  try (Logging logging = options.getService()) {

    String logFilter = "logName=projects/" + options.getProjectId() + "/logs/" + logName;

    // List all log entries
    Page<LogEntry> entries = logging.listLogEntries(
        EntryListOption.filter(logFilter));
    do {
      for (LogEntry logEntry : entries.iterateAll()) {
        System.out.println(logEntry);
      }
      entries = entries.getNextPage();
    } while (entries != null);

  }
  // [END logging_list_log_entries]
}
 
Example #4
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 #5
Source File: CreateAndListMetrics.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
public static void main(String... args) throws Exception {
  // Create a service object
  // Credentials are inferred from the environment
  try (Logging logging = LoggingOptions.getDefaultInstance().getService()) {

    // Create a metric
    MetricInfo metricInfo =
        MetricInfo.newBuilder("test-metric", "severity >= ERROR")
            .setDescription("Log entries with severity higher or equal to ERROR")
            .build();
    logging.create(metricInfo);

    // List metrics
    Page<Metric> metrics = logging.listMetrics();
    for (Metric metric : metrics.iterateAll()) {
      System.out.println(metric);
    }
  }
}
 
Example #6
Source File: CreateAndListSinks.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
public static void main(String... args) throws Exception {
  // Create a service object
  // Credentials are inferred from the environment
  try (Logging logging = LoggingOptions.getDefaultInstance().getService()) {

    // Create a sink to back log entries to a BigQuery dataset
    SinkInfo sinkInfo =
        SinkInfo.newBuilder("test-sink", DatasetDestination.of("test-dataset"))
            .setFilter("severity >= ERROR")
            .build();
    logging.create(sinkInfo);

    // List sinks
    Page<Sink> sinks = logging.listSinks();
    for (Sink sink : sinks.iterateAll()) {
      System.out.println(sink);
    }
  }
}
 
Example #7
Source File: StackdriverMonitorExtension.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize() {
  try {
    this.logging = LoggingOptions
      .newBuilder()
      .setProjectId(GoogleCloudUtils.getProjectId())
      .setCredentials(GoogleCredentials.getApplicationDefault())
      .build()
      .getService();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #8
Source File: WriteAndListLogEntries.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
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);
    }
  }
}
 
Example #9
Source File: LoggingSampleApplicationTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Before
public void setupLogging() {
	this.logClient = LoggingOptions.getDefaultInstance().getService();
}
 
Example #10
Source File: ExampleSystemTest.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setUp() throws IOException {
  loggingClient = LoggingOptions.getDefaultInstance().getService();
  publisher = Publisher.newBuilder(
      ProjectTopicName.of(PROJECT_ID, TOPIC_NAME)).build();
}
 
Example #11
Source File: ExampleSystemTest.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setUp() throws IOException {
  loggingClient = LoggingOptions.getDefaultInstance().getService();
}