How to log errors to Firebase Cloud Functions Log?

Viewed 3972

I'm having trouble logging meaningful error messages to my function logs, what I'm trying to achieve is have errors appear with the 'error' log level as bellow:

Image of actual error log

Instead, what I'm getting is this:

Image of test result

I'm using the following function to test this out:

exports.testError = functions.https.onRequest(async (request, response) =>
{
    console.log('Hi');

    //According to google this won't be logged as error
    console.error('Error');

    //But these will
    console.error(new Error('error'));
    console.error('Error', new Error('error'));

    return response.status(500).send('Test is finished.');
})

The first image is of a random error and its purpose is to show what I want to achieve, which is: have erros be logged as errors.

The second image is the result of my tests, it shows that even when I'm following instructions from google(Reporting Errors) it is not working as intended.

2 Answers

Use the Cloud Functions logger SDK as described in

https://firebase.google.com/docs/functions/writing-and-viewing-logs

const functions = require("firebase-functions")
functions.logger.debug("debug level in web console and gcp")
functions.logger.log("info level in web console and gcp")
functions.logger.info("info level in web console and gcp")
functions.logger.warn("info level but no icon in web console; warn icon in gcp")
functions.logger.error("error level in web console and gcp")

I was not able to get console.error() or console.info() to work (node 10, 12, or 14) as advertised in https://firebase.google.com/docs/functions/reporting-errors

Well I figured it out: I was using node 12, I changed it to node 10 and it now logs the errors as expected.

Related