Search code examples
google-cloud-platformstackdrivergoogle-cloud-stackdriver

GCP Logging - read only logs that are not exported to any sink


Using GCP's Java SDK, I'd like to read only logs that are not configured to be exported to any sink.

When I call com.google.cloud.logging.Logging.listLogEntries() I get all of the logs, including logs are exported to sinks I have configured.

Any way to collect only log entries that are not configured to be exported to any sink?


Solution

  • You need to use EntryListOption to get filtered log entries as explained on this page.

      Page<LogEntry> entries = logging.listLogEntries( 
            EntryListOption.filter("logName=projects/" + options.getProjectId() + "/logs/test-log"));
            Iterator<LogEntry> entryIterator = entries.iterateAll().iterator();
            while (entryIterator.hasNext()) {
              System.out.println(entryIterator.next());
            }
    

    Any way to collect only log entries that are not configured to be exported to any sink?

    In order to achieve this,

    1. you need to get all sinks using REST api or equivalent java client

    2. iterate over sinks to get each filter query

    3. build a dynamic filter to use with the above EntryListOption