Class Log (11.2.0)

Package

@google-cloud/logging

Example

import{Logging}from'@google-cloud/logging';import{LogOptions}from'@google-cloud/logging/build/src/log';constoptions:LogOptions={maxEntrySize:256,jsonFieldsToTruncate:['jsonPayload.fields.metadata.structValue.fields.custom.stringValue',],defaultWriteDeleteCallback:(err:any)=>{if(err){console.log('Error: '+err);}},};constlogging=newLogging();constlog=logging.log('syslog',options);

Constructors

(constructor)(logging, name, options)

constructor(logging:Logging,name:string,options?:LogOptions);

Constructs a new instance of theLog class

Parameters
NameDescription
loggingLogging
namestring
optionsLogOptions

Properties

defaultWriteDeleteCallback

defaultWriteDeleteCallback?:ApiResponseCallback;

formattedName_

formattedName_:string;

jsonFieldsToTruncate

jsonFieldsToTruncate:string[];

logging

logging:Logging;

maxEntrySize

maxEntrySize?:number;

name

name:string;

partialSuccess

partialSuccess:boolean;

removeCircular_

removeCircular_:boolean;

Methods

alert(entry, options)

alert(entry:Entry|Entry[],options?:WriteOptions):Promise<ApiResponse>;

Write a log entry with a severity of "ALERT".

This is a simple wrapper around . All arguments are the same as documented there.

Parameters
NameDescription
entryEntry |Entry[]

A log entry, or array of entries, to write.

optionsWriteOptions

Write options

Returns
TypeDescription
Promise<ApiResponse>

{Promise

Example
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constlog=logging.log('my-log');constentry=log.entry('gce_instance',{instance:'my_instance'});log.alert(entry,(err,apiResponse)=>{});//-// If the callback is omitted, we'll return a Promise.//-log.alert(entry).then(data=>{constapiResponse=data[0];});

alert(entry, options, callback)

alert(entry:Entry|Entry[],options:WriteOptions,callback:ApiResponseCallback):void;
Parameters
NameDescription
entryEntry |Entry[]
optionsWriteOptions
callbackApiResponseCallback
Returns
TypeDescription
void

alert(entry, callback)

alert(entry:Entry|Entry[],callback:ApiResponseCallback):void;
Parameters
NameDescription
entryEntry |Entry[]
callbackApiResponseCallback
Returns
TypeDescription
void

assignSeverityToEntries_(entries, severity)

staticassignSeverityToEntries_(entries:Entry|Entry[],severity:string):Entry[];

Return an array of log entries with the desired severity assigned.

Parameters
NameDescription
entriesEntry |Entry[]

Log entries.

severitystring

The desired severity level.

Returns
TypeDescription
Entry[]

critical(entry, options)

critical(entry:Entry|Entry[],options?:WriteOptions):Promise<ApiResponse>;

Write a log entry with a severity of "CRITICAL".

This is a simple wrapper around . All arguments are the same as documented there.

Parameters
NameDescription
entryEntry |Entry[]

A log entry, or array of entries, to write.

optionsWriteOptions

Write options

Returns
TypeDescription
Promise<ApiResponse>

{Promise

Example
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constlog=logging.log('my-log');constentry=log.entry('gce_instance',{instance:'my_instance'});log.critical(entry,(err,apiResponse)=>{});//-// If the callback is omitted, we'll return a Promise.//-log.critical(entry).then(data=>{constapiResponse=data[0];});

critical(entry, options, callback)

critical(entry:Entry|Entry[],options:WriteOptions,callback:ApiResponseCallback):void;
Parameters
NameDescription
entryEntry |Entry[]
optionsWriteOptions
callbackApiResponseCallback
Returns
TypeDescription
void

critical(entry, callback)

critical(entry:Entry|Entry[],callback:ApiResponseCallback):void;
Parameters
NameDescription
entryEntry |Entry[]
callbackApiResponseCallback
Returns
TypeDescription
void

debug(entry, options)

debug(entry:Entry|Entry[],options?:WriteOptions):Promise<ApiResponse>;

Write a log entry with a severity of "DEBUG".

This is a simple wrapper around . All arguments are the same as documented there.

Parameters
NameDescription
entryEntry |Entry[]

A log entry, or array of entries, to write.

optionsWriteOptions

Write options

Returns
TypeDescription
Promise<ApiResponse>

{Promise

Example
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constlog=logging.log('my-log');constentry=log.entry('gce_instance',{instance:'my_instance'});log.debug(entry,(err,apiResponse)=>{});//-// If the callback is omitted, we'll return a Promise.//-log.debug(entry).then(data=>{constapiResponse=data[0];});

debug(entry, options, callback)

debug(entry:Entry|Entry[],options:WriteOptions,callback:ApiResponseCallback):void;
Parameters
NameDescription
entryEntry |Entry[]
optionsWriteOptions
callbackApiResponseCallback
Returns
TypeDescription
void

debug(entry, callback)

debug(entry:Entry|Entry[],callback:ApiResponseCallback):void;
Parameters
NameDescription
entryEntry |Entry[]
callbackApiResponseCallback
Returns
TypeDescription
void

delete(gaxOptions)

delete(gaxOptions?:CallOptions):Promise<ApiResponse>;
Parameter
NameDescription
gaxOptionsCallOptions

Request configuration options, outlined here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.

Returns
TypeDescription
Promise<ApiResponse>

{Promise

Examples
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constlog=logging.log('my-log');log.delete((err,apiResponse)=>{if(!err){// The log was deleted.}});//-// If the callback is omitted, we'll return a Promise.//-log.delete().then(data=>{constapiResponse=data[0];});

Another example:

// Imports the Google Cloud client libraryconst{Logging}=require('@google-cloud/logging');// Creates a clientconstlogging=newLogging();/**   * TODO(developer): Uncomment the following line to run the code.   */// const logName = 'Name of the log to delete, e.g. my-log';constlog=logging.log(logName);asyncfunctiondeleteLog(){// Deletes a logger and all its entries.// Note that a deletion can take several minutes to take effect.// See https://googleapis.dev/nodejs/logging/latest/Log.html#deleteawaitlog.delete();console.log(`Deleted log:${logName}`);}deleteLog();

delete(gaxOptions, callback)

delete(gaxOptions:CallOptions,callback:DeleteCallback):void;
Parameters
NameDescription
gaxOptionsCallOptions
callbackDeleteCallback
Returns
TypeDescription
void

delete(callback)

delete(callback:DeleteCallback):void;
Parameter
NameDescription
callbackDeleteCallback
Returns
TypeDescription
void

emergency(entry, options, callback)

emergency(entry:Entry|Entry[],options:WriteOptions,callback:ApiResponseCallback):void;

Write a log entry with a severity of "EMERGENCY".

This is a simple wrapper around . All arguments are the same as documented there.

Parameters
NameDescription
entryEntry |Entry[]

A log entry, or array of entries, to write.

optionsWriteOptions

Write options

callbackApiResponseCallback

Callback function.

Returns
TypeDescription
void

{Promise

Example
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constlog=logging.log('my-log');constentry=log.entry('gce_instance',{instance:'my_instance'});log.emergency(entry,(err,apiResponse)=>{});//-// If the callback is omitted, we'll return a Promise.//-log.emergency(entry).then(data=>{constapiResponse=data[0];});

emergency(entry, callback)

emergency(entry:Entry|Entry[],callback:ApiResponseCallback):void;
Parameters
NameDescription
entryEntry |Entry[]
callbackApiResponseCallback
Returns
TypeDescription
void

entry(metadata)

entry(metadata?:LogEntry):Entry;

Create an entry object for this log.

Using this method will not itself make any API requests. You will use the object returned in other API calls, such as .

Note,Cloud Logging Quotas and limits dictates that the maximum log entry size, including all [LogEntry Resource properties]https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry, cannot exceed _approximately_ 256 KB.

SeeLogEntry JSON representation

Parameter
NameDescription
metadataLogEntry

See a [LogEntry Resource](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry).

Returns
TypeDescription
Entry

{Entry}

Example
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constlog=logging.log('my-log');constmetadata={resource:{type:'gce_instance',labels:{zone:'global',instance_id:'3'}}};constentry=log.entry(metadata,{delegate:'my_username'});entry.toJSON();// {//   logName: 'projects/grape-spaceship-123/logs/syslog',//   resource: {//     type: 'gce_instance',//     labels: {//       zone: 'global',//       instance_id: '3'//     }//   },//   jsonPayload: {//     delegate: 'my_username'//   }// }

entry(data)

entry(data?:string|{}):Entry;
Parameter
NameDescription
datastring | {}
Returns
TypeDescription
Entry

entry(metadata, data)

entry(metadata?:LogEntry,data?:string|{}):Entry;
Parameters
NameDescription
metadataLogEntry
datastring | {}
Returns
TypeDescription
Entry

error(entry, options)

error(entry:Entry|Entry[],options?:WriteOptions):Promise<ApiResponse>;

Write a log entry with a severity of "ERROR".

This is a simple wrapper around . All arguments are the same as documented there.

Parameters
NameDescription
entryEntry |Entry[]

A log entry, or array of entries, to write.

optionsWriteOptions

Write options

Returns
TypeDescription
Promise<ApiResponse>

{Promise

Example
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constlog=logging.log('my-log');constentry=log.entry('gce_instance',{instance:'my_instance'});log.error(entry,(err,apiResponse)=>{});//-// If the callback is omitted, we'll return a Promise.//-log.error(entry).then(data=>{constapiResponse=data[0];});

error(entry, options, callback)

error(entry:Entry|Entry[],options:WriteOptions,callback:ApiResponseCallback):void;
Parameters
NameDescription
entryEntry |Entry[]
optionsWriteOptions
callbackApiResponseCallback
Returns
TypeDescription
void

error(entry, callback)

error(entry:Entry|Entry[],callback:ApiResponseCallback):void;
Parameters
NameDescription
entryEntry |Entry[]
callbackApiResponseCallback
Returns
TypeDescription
void

formatName_(projectId, name)

staticformatName_(projectId:string,name:string):string;

Format the name of a log. A log's full name is in the format of 'projects/{projectId}/logs/{logName}'.

Parameters
NameDescription
projectIdstring
namestring
Returns
TypeDescription
string

{string}

getEntries(options)

getEntries(options?:GetEntriesRequest):Promise<GetEntriesResponse>;

This method is a wrapper around {module:logging#getEntries}, but with a filter specified to only return entries from this log.

Seeentries.list API Documentation

Parameter
NameDescription
optionsGetEntriesRequest
Returns
TypeDescription
Promise<GetEntriesResponse>

{Promise

Example
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constlog=logging.log('my-log');log.getEntries((err,entries)=>{// `entries` is an array of Cloud Logging entry objects.// See the `data` property to read the data from the entry.});//-// To control how many API requests are made and page through the results// manually, set `autoPaginate` to `false`.//-functioncallback(err,entries,nextQuery,apiResponse){if(nextQuery){// More results exist.log.getEntries(nextQuery,callback);}}log.getEntries({autoPaginate:false},callback);//-// If the callback is omitted, we'll return a Promise.//-log.getEntries().then(data=>{constentries=data[0];});

getEntries(callback)

getEntries(callback:GetEntriesCallback):void;
Parameter
NameDescription
callbackGetEntriesCallback
Returns
TypeDescription
void

getEntries(options, callback)

getEntries(options:GetEntriesRequest,callback:GetEntriesCallback):void;
Parameters
NameDescription
optionsGetEntriesRequest
callbackGetEntriesCallback
Returns
TypeDescription
void

getEntriesStream(options)

getEntriesStream(options:GetEntriesRequest):import("stream").Duplex;

This method is a wrapper around {module:logging#getEntriesStream}, but with a filter specified to only return {module:logging/entry} objects from this log.

Log#getEntriesStream

Parameter
NameDescription
optionsGetEntriesRequest
Returns
TypeDescription
import("stream")."\"stream\"".internal.Duplex

{ReadableStream} A readable stream that emitsEntry instances.

Example
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constlog=logging.log('my-log');log.getEntriesStream().on('error',console.error).on('data',entry=>{// `entry` is a Cloud Logging entry object.// See the `data` property to read the data from the entry.}).on('end',function(){// All entries retrieved.});//-// If you anticipate many results, you can end a stream early to prevent// unnecessary processing and API requests.//-log.getEntriesStream().on('data',function(entry){this.end();});

info(entry, options)

info(entry:Entry|Entry[],options?:WriteOptions):Promise<ApiResponse>;

Write a log entry with a severity of "INFO".

This is a simple wrapper around . All arguments are the same as documented there.

Parameters
NameDescription
entryEntry |Entry[]

A log entry, or array of entries, to write.

optionsWriteOptions

Write options

Returns
TypeDescription
Promise<ApiResponse>

{Promise

Example
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constlog=logging.log('my-log');constentry=log.entry('gce_instance',{instance:'my_instance'});log.info(entry,(err,apiResponse)=>{});//-// If the callback is omitted, we'll return a Promise.//-log.info(entry).then(data=>{constapiResponse=data[0];});

info(entry, options, callback)

info(entry:Entry|Entry[],options:WriteOptions,callback:ApiResponseCallback):void;
Parameters
NameDescription
entryEntry |Entry[]
optionsWriteOptions
callbackApiResponseCallback
Returns
TypeDescription
void

info(entry, callback)

info(entry:Entry|Entry[],callback:ApiResponseCallback):void;
Parameters
NameDescription
entryEntry |Entry[]
callbackApiResponseCallback
Returns
TypeDescription
void

notice(entry, options)

notice(entry:Entry|Entry[],options?:WriteOptions):Promise<ApiResponse>;

Write a log entry with a severity of "NOTICE".

This is a simple wrapper around . All arguments are the same as documented there.

Parameters
NameDescription
entryEntry |Entry[]

A log entry, or array of entries, to write.

optionsWriteOptions

Write options

Returns
TypeDescription
Promise<ApiResponse>

{Promise

Example
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constlog=logging.log('my-log');constentry=log.entry('gce_instance',{instance:'my_instance'});log.notice(entry,(err,apiResponse)=>{});//-// If the callback is omitted, we'll return a Promise.//-log.notice(entry).then(data=>{constapiResponse=data[0];});

notice(entry, options, callback)

notice(entry:Entry|Entry[],options:WriteOptions,callback:ApiResponseCallback):void;
Parameters
NameDescription
entryEntry |Entry[]
optionsWriteOptions
callbackApiResponseCallback
Returns
TypeDescription
void

notice(entry, callback)

notice(entry:Entry|Entry[],callback:ApiResponseCallback):void;
Parameters
NameDescription
entryEntry |Entry[]
callbackApiResponseCallback
Returns
TypeDescription
void

tailEntries(options)

tailEntries(options?:TailEntriesRequest):import("stream").Duplex;

This method is a wrapper around {module:logging#tailEntries}, but with a filter specified to only return {module:logging/entry} objects from this log.

Log#tailEntries

Parameter
NameDescription
optionsTailEntriesRequest
Returns
TypeDescription
import("stream")."\"stream\"".internal.Duplex

{DuplexStream} A duplex stream that emits TailEntriesResponses containing an array ofEntry instances.

Example
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constlog=logging.log('my-log');log.tailEntries().on('error',console.error).on('data',resp=>{console.log(resp.entries);console.log(resp.suppressionInfo);}).on('end',function(){// All entries retrieved.});//-// If you anticipate many results, you can end a stream early to prevent// unnecessary processing and API requests.//-log.tailEntries().on('data',function(entry){this.end();});

warning(entry, options)

warning(entry:Entry|Entry[],options?:WriteOptions):Promise<ApiResponse>;

Write a log entry with a severity of "WARNING".

This is a simple wrapper around . All arguments are the same as documented there.

Parameters
NameDescription
entryEntry |Entry[]

A log entry, or array of entries, to write.

optionsWriteOptions

Write options

Returns
TypeDescription
Promise<ApiResponse>

{Promise

Example
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constlog=logging.log('my-log');constentry=log.entry('gce_instance',{instance:'my_instance'});log.warning(entry,(err,apiResponse)=>{});//-// If the callback is omitted, we'll return a Promise.//-log.warning(entry).then(data=>{constapiResponse=data[0];});

warning(entry, options, callback)

warning(entry:Entry|Entry[],options:WriteOptions,callback:ApiResponseCallback):void;
Parameters
NameDescription
entryEntry |Entry[]
optionsWriteOptions
callbackApiResponseCallback
Returns
TypeDescription
void

warning(entry, callback)

warning(entry:Entry|Entry[],callback:ApiResponseCallback):void;
Parameters
NameDescription
entryEntry |Entry[]
callbackApiResponseCallback
Returns
TypeDescription
void

write(entry, options)

write(entry:Entry|Entry[],options?:WriteOptions):Promise<ApiResponse>;

Write log entries to Cloud Logging.

Note,Cloud Logging Quotas and limits dictates that the maximum cumulative size of all entries per write, including all [LogEntry Resource properties]https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry, cannot exceed _approximately_ 10 MB.

Seeentries.write API Documentation

Parameters
NameDescription
entryEntry |Entry[]

A log entry, or array of entries, to write.

optionsWriteOptions

Write options

Returns
TypeDescription
Promise<ApiResponse>

{Promise

Examples
constentry=log.entry('gce_instance',{instance:'my_instance'});log.write(entry,(err,apiResponse)=>{if(!err){// The log entry was written.}});//-// You may also pass multiple log entries to write.//-constsecondEntry=log.entry('compute.googleapis.com',{user:'my_username'});log.write([entry,secondEntry],(err,apiResponse)=>{if(!err){// The log entries were written.}});//-// To save some steps, you can also pass in plain values as your entries.// Note, however, that you must provide a configuration object to specify// the resource.//-constentries=[{user:'my_username'},{home:process.env.HOME}];constoptions={resource:'compute.googleapis.com'};log.write(entries,options,(err,apiResponse)=>{});//-// If the callback is omitted, we'll return a Promise.//-log.write(entries).then(data=>{constapiResponse=data[0];});

Another example:

const{Logging}=require('@google-cloud/logging');constlogging=newLogging();/**   * TODO(developer): Uncomment the following line and replace with your values.   */// const logName = 'my-log';constlog=logging.log(logName);// A text log entryconsttext_entry=log.entry('Hello world!');// A json log entry with additional contextconstmetadata={severity:'WARNING',labels:{foo:'bar',},// A default log resource is added for some GCP environments// This log resource can be overwritten per spec:// https://cloud.google.com/logging/docs/reference/v2/rest/v2/MonitoredResourceresource:{type:'global',},};constmessage={name:'King Arthur',quest:'Find the Holy Grail',favorite_color:'Blue',};constjson_Entry=log.entry(metadata,message);asyncfunctionwriteLogEntry(){// Asynchronously write the log entryawaitlog.write(text_entry);// Asynchronously batch write the log entriesawaitlog.write([text_entry,json_Entry]);// Let the logging library dispatch logslog.write(text_entry);console.log(`Wrote to${logName}`);}writeLogEntry();

Another example:

// [START logging_http_request]/*  const projectId = 'YOUR_PROJECT_ID'; // Your Google Cloud Platform project ID  const logName = 'my-log'; // The name of the log to write to  const requestMethod = 'GET'; // GET, POST, PUT, etc.  const requestUrl = 'http://www.google.com';  const status = 200;  const userAgent = `my-user-agent/1.0.0`;  const latencySeconds = 3;  const responseSize = 256; // response size in bytes.  */// Imports the Google Cloud client libraryconst{Logging}=require('@google-cloud/logging');// Creates a clientconstlogging=newLogging({projectId});// Selects the log to write toconstlog=logging.log(logName);// The data to write to the logconsttext='Hello, world!';// The metadata associated with the entryconstmetadata={resource:{type:'global'},httpRequest:{requestMethod,requestUrl,status,userAgent,latency:{seconds:latencySeconds,},responseSize,},};// Prepares a log entryconstentry=log.entry(metadata,text);// Writes the log entryasyncfunctionwriteLog(){awaitlog.write(entry);console.log(`Logged:${text}`);}writeLog();

write(entry, options, callback)

write(entry:Entry|Entry[],options:WriteOptions,callback:ApiResponseCallback):void;
Parameters
NameDescription
entryEntry |Entry[]
optionsWriteOptions
callbackApiResponseCallback
Returns
TypeDescription
void

write(entry, callback)

write(entry:Entry|Entry[],callback:ApiResponseCallback):void;
Parameters
NameDescription
entryEntry |Entry[]
callbackApiResponseCallback
Returns
TypeDescription
void

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

Last updated 2025-10-30 UTC.