Write and view logs


Logging is an important tool for debugging and monitoring code. Cloud Functions gives you the option of using its logger SDK, custom Google Cloud Logging , or the console object standard for developing for the web.

Writing logs

While the Cloud Functions logger SDK is recommended for most situations, you might choose one of the other options for these reasons:

  • You have an existing code base and prefer not to refactor from console.log .
  • You're familiar with Cloud Logging (formerly StackDriver logging) and prefer to use it for custom logging.

Using the Cloud Functions logger SDK

The Cloud Functions logger SDK provides a standard interface that has a similar api to console.log statements and supports other log levels. You can use this SDK to log events with structured data , enabling easier analysis and monitoring.

The logger SDK supports log entries as part of a wildcard import. For example:

   
 const 
  
 functions 
  
 = 
  
 require 
 ( 
 "firebase-functions/v1" 
 ); 
  
 functions 
 . 
 logger 
 . 
 log 
 ( 
 "Hello from info. Here's an object:" 
 , 
  
 someObj 
 ); 
 

Alternatively, you can use individual exports. This example demonstrates structured data attached to the log as the last argument:

  const 
  
 { 
  
 warn 
  
 } 
  
 = 
  
 require 
 ( 
 "firebase-functions/logger" 
 ); 
 // 
  
 Attach 
  
 structured 
  
 data 
  
 to 
  
 the 
  
 log 
  
 as 
  
 the 
  
 last 
  
 argument 
 . 
 warn 
 ( 
 "This is a 'WARNING' severity message with some metadata." 
 , 
  
 { 
  
 key1 
 : 
  
 'val1' 
 , 
  
 key2 
 : 
  
 'val2' 
 }); 
 
  • logger.log() commands have the INFOlog level.
  • logger.info() commands have the INFOlog level.
  • logger.warn() commands have the WARNINGlog level.
  • logger.error() commands have the ERRORlog level.
  • Internal system messages have the DEBUGlog level.

With logger.write() , you can write log entries addition log severity levels of CRITICAL , ALERT , and EMERGENCY . See LogSeverity .

Custom Cloud Logging logs

Cloud Functions logs with the logger SDK are backed by Cloud Logging . You can use the Cloud Logging library for Node.js to log events with structured data, enabling easier analysis and monitoring.

  const 
  
 { 
  
 Logging 
  
 } 
  
 = 
  
 require 
 ( 
 '@google-cloud/logging' 
 ); 
 // 
  
 ... 
 // 
  
 Instantiate 
  
 the 
  
 logging 
  
 SDK 
 . 
  
 The 
  
 project 
  
 ID 
  
 will 
 // 
  
 be 
  
 automatically 
  
 inferred 
  
 from 
  
 the 
  
 Cloud 
  
 Functions 
  
 environment 
 . 
 const 
  
 logging 
  
 = 
  
 new 
  
 Logging 
 (); 
 const 
  
 log 
  
 = 
  
 logging 
 . 
 log 
 ( 
 'my-custom-log-name' 
 ); 
 // 
  
 This 
  
 metadata 
  
 is 
  
 attached 
  
 to 
  
 each 
  
 log 
  
 entry 
 . 
  
 This 
  
 specifies 
  
 a 
  
 fake 
 // 
  
 Cloud 
  
 Function 
  
 called 
  
 'Custom Metrics' 
  
 in 
  
 order 
  
 to 
  
 make 
  
 your 
  
 custom 
 // 
  
 log 
  
 entries 
  
 appear 
  
 in 
  
 the 
  
 Cloud 
  
 Functions 
  
 logs 
  
 viewer 
 . 
 const 
  
 METADATA 
  
 = 
  
 { 
  
 resource 
 : 
  
 { 
  
 type 
 : 
  
 'cloud_function' 
 , 
  
 labels 
 : 
  
 { 
  
 function_name 
 : 
  
 'CustomMetrics' 
 , 
  
 region 
 : 
  
 'us-central1' 
  
 } 
  
 } 
 }; 
 // 
  
 ... 
 // 
  
 Data 
  
 to 
  
 write 
  
 to 
  
 the 
  
 log 
 . 
  
 This 
  
 can 
  
 be 
  
 a 
  
 JSON 
  
 object 
  
 with 
  
 any 
  
 properties 
 // 
  
 of 
  
 the 
  
 event 
  
 you 
  
 want 
  
 to 
  
 record 
 . 
 const 
  
 data 
  
 = 
  
 { 
  
 event 
 : 
  
 'my-event' 
 , 
  
 value 
 : 
  
 'foo-bar-baz' 
 , 
  
 // 
  
 Optional 
  
 'message' 
  
 property 
  
 will 
  
 show 
  
 up 
  
 in 
  
 the 
  
 Firebase 
  
 // 
  
 console 
  
 and 
  
 other 
  
 human 
 - 
 readable 
  
 logging 
  
 surfaces 
  
 message 
 : 
  
 'my-event: foo-bar-baz' 
 }; 
 // 
  
 Write 
  
 to 
  
 the 
  
 log 
 . 
  
 The 
  
 log 
 . 
 write 
 () 
  
 call 
  
 returns 
  
 a 
  
 Promise 
  
 if 
  
 you 
  
 want 
  
 to 
 // 
  
 make 
  
 sure 
  
 that 
  
 the 
  
 log 
  
 was 
  
 written 
  
 successfully 
 . 
 const 
  
 entry 
  
 = 
  
 log 
 . 
 entry 
 ( 
 METADATA 
 , 
  
 data 
 ); 
 log 
 . 
 write 
 ( 
 entry 
 ); 
  
 

Using console.log

The recommended solution for logging from a function is to use the logger SDK for your platform. With Node.js, you can instead use standard JavaScript logging calls such as console.log and console.error , but you first need to require a special module to patch the standard methods to work correctly:

 require("firebase-functions/logger/compat"); 

Once you have required the logger compatibility module, you can use console.log() methods as normal in your code:

  exports 
 . 
 helloError 
  
 = 
  
 functions 
 . 
 https 
 . 
 onRequest 
 (( 
 request 
 , 
  
 response 
 ) 
  
 = 
>  
 { 
  
 console 
 . 
 log 
 ( 
 'I am a log entry!' 
 ); 
  
 response 
 . 
 send 
 ( 
 'Hello World...' 
 ); 
 }); 
  
 
  • console.log() commands have the INFOlog level.
  • console.info() commands have the INFOlog level.
  • console.warn() commands have the ERRORlog level.
  • console.error() commands have the ERRORlog level.
  • Internal system messages have the DEBUGlog level.

Viewing logs

Logs for Cloud Functions are viewable either in the Google Cloud console , Cloud Logging UI, or via the firebase command-line tool.

Using the Firebase CLI

To view logs with the firebase tool, use the functions:log command:

 firebase functions:log 

To view logs for a specific function, provide the function name as an argument:

 firebase functions:log --only <FUNCTION_NAME> 

For the full range of log viewing options, view the help for functions:log :

 firebase help functions:log 

Using the Google Cloud console

You can view logs for functions in the Google Cloud console .

Using the Cloud Logging UI

You can view logs for Cloud Functions in the Cloud Logging UI.

Analyzing logs

Cloud Logging offers a powerful suite of logs analysis tools that you can use to monitor your Cloud Functions .

Charts and alerts

Once you have created logs-based metrics to monitor your functions, you can create charts and alerts based on these metrics. For example, you could create a chart to visualize latency over time, or create an alert to let you know if a certain error occurs too often.

See Creating Charts and Alerts for detailed information on how to use logs-based metrics in charts and alerting policies.

Design a Mobile Site
View Site in Mobile | Classic
Share by: