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.
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.
Share Improve this question edited Nov 19, 2020 at 16:48 Doug Stevenson 318k36 gold badges454 silver badges472 bronze badges asked Nov 19, 2020 at 12:59 Elielson AnjosElielson Anjos 711 silver badge5 bronze badges 6- Could you please explain more briefly your concerns? As I reproduced your issue, and I was able to see in Stackdriver Logging for the Cloud Function that when you define console.error(new Error('error'));, it causes an error log ( and we can see it choosing error log level ) , and also from the Cloud Function logs, I can see it appears as error log level. – Nibrass H Commented Nov 27, 2020 at 11:35
-
Well, that's odd, I'm expecting to see the same as you. Perhaps I'm doing something wrong with my import statements, these are all the imports I'm using:
import * as functions from 'firebase-functions'; import * as admin from 'firebase-admin';
– Elielson Anjos Commented Nov 27, 2020 at 12:40 - Could you please explain more what are you expecting and what are you not able to see? Have you inspect your Stackdriver Logging for your Cloud Function and filter by error log level? – Nibrass H Commented Dec 2, 2020 at 10:37
- Yes, I did it in the question, screenshots of what I expect to see and what I'm actually seeing is included in the question. – Elielson Anjos Commented Dec 4, 2020 at 11:51
- I was not able to see that you are checking the errors in Stackdriver by checking the screenshots, please check Stakdriver Logging after testing your Cloud Function. After deploying your Cloud Functions, from the Testing tab of your Cloud Function, please test your function in order to see the error. – Nibrass H Commented Dec 4, 2020 at 20:30
2 Answers
Reset to default 12Use the Cloud Functions logger SDK as described in
https://firebase.google./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./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.