Tail log entries

Demonstrates how to tail live log entries.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Go

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

To authenticate to Logging, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import(
"context"
"fmt"
"io"
logging"cloud.google.com/go/logging/apiv2"
"cloud.google.com/go/logging/apiv2/loggingpb"
)
// tailLogs creates a channel to stream log entries that were recently ingested for a project
functailLogs(projectIDstring)error{
// projectID := "your_project_id"
ctx:=context.Background()
client,err:=logging.NewClient (ctx)
iferr!=nil{
returnfmt.Errorf("NewClient error: %w",err)
}
deferclient.Close()
stream,err:=client.TailLogEntries(ctx)
iferr!=nil{
returnfmt.Errorf("TailLogEntries error: %w",err)
}
deferstream.CloseSend()
req:=&loggingpb.TailLogEntriesRequest{
ResourceNames:[]string{
"projects/"+projectID,
},
}
iferr:=stream.Send(req);err!=nil{
returnfmt.Errorf("stream.Send error: %w",err)
}
// read and print two or more streamed log entries
forcounter:=0;counter < 2;{
resp,err:=stream.Recv()
iferr==io.EOF{
break
}
iferr!=nil{
returnfmt.Errorf("stream.Recv error: %w",err)
}
fmt.Printf("received:\n%v\n",resp)
ifresp.Entries!=nil{
counter+=len(resp.Entries)
}
}
returnnil
}

Java

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

To authenticate to Logging, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

importcom.google.cloud.logging.LogEntry ;
importcom.google.cloud.logging.LogEntryServerStream ;
importcom.google.cloud.logging.Logging ;
importcom.google.cloud.logging.Logging.TailOption ;
importcom.google.cloud.logging.LoggingOptions ;
publicclass TailLogEntries{
publicstaticvoidmain(String[]args)throwsException{
// TODO(developer): Optionally provide the logname as an argument.
StringlogName=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=
logging.tailLogEntries (
TailOption .filter(
"logName=projects/"+options.getProjectId()+"/logs/"+logName));
}else{
stream=logging.tailLogEntries ();
}
System.out.println("start streaming..");
for(LogEntry log:stream){
System.out.println(log);
// cancel infinite streaming after receiving first entry
stream.cancel ();
}
}
}
}

Node.js

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

To authenticate to Logging, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

const{Logging}=require('@google-cloud/logging');
constlogging=newLogging ();
/**
 * TODO(developer): Replace logName with the name of your log.
 */
constlog=logging.log(logName);
console.log('running tail log entries test');
conststream=log
.tailEntries({
filter:'timestamp > "2021-01-01T23:00:00Z"',
})
.on('error',console.error)
.on('data',resp=>{
console.log(resp.entries);
console.log(resp.suppressionInfo);
// If you anticipate many results, you can end a stream early to prevent
// unnecessary processing and API requests.
stream .end();
})
.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.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.