Tail log entries

Demonstrates how to tail live log entries.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample


To learn how to install and use the client library for Logging, see Logging client libraries.

import com.google.cloud.logging.LogEntry;
import com.google.cloud.logging.LogEntryServerStream;
import com.google.cloud.logging.Logging;
import com.google.cloud.logging.Logging.TailOption;
import com.google.cloud.logging.LoggingOptions;

public class TailLogEntries {

  public static void main(String[] args) throws Exception {
    // TODO(developer): Optionally provide the logname as an argument.
    String logName = args.length > 0 ? args[0] : "";

    LoggingOptions options = LoggingOptions.getDefaultInstance();
    try (Logging logging = options.getService()) {

      // Optionally compose a filter to tail log entries only from specific log
      LogEntryServerStream stream;

      if (logName != "") {
        stream =
                    "logName=projects/" + options.getProjectId() + "/logs/" + logName));
      } else {
        stream = logging.tailLogEntries();
      System.out.println("start streaming..");
      for (LogEntry log : stream) {
        // cancel infinite streaming after receiving first entry


To learn how to install and use the client library for Logging, see Logging client libraries.

const {Logging} = require('@google-cloud/logging');
const logging = new Logging();

 * TODO(developer): Replace logName with the name of your log.
const log = logging.log(logName);
console.log('running tail log entries test');

const stream = log
    filter: 'timestamp > "2021-01-01T23:00:00Z"',
  .on('error', console.error)
  .on('data', resp => {
    // If you anticipate many results, you can end a stream early to prevent
    // unnecessary processing and API requests.
  .on('end', () => {
    console.log('log entry stream has ended');

// Note: to get all project logs, invoke logging.tailEntries

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.