Plain console.warn() shows up in logs with serverity "ERROR"
Asked Answered
A

3

4

When I log something with console.warn() it seems to appear in the Stackdriver logs with severity "ERROR". The Stackdriver Error Reporting does not show these errors, so it seems there they are not considered errors. This makes it impossible to filter the logs to only show me errors.

Reading the Stackdriver logging docs I get the impression that I'm not supposed to use the plain javascript console functions but instead use Bunyan. Is that correct? I didn't read anywhere that I shouldn't.

Airless answered 19/4, 2019 at 10:6 Comment(3)
Where is the code running that calls console.warn()?Biondo
Cloud Functions I know for sure, possibly also the App Engine.Airless
same issue for GKE containerVarese
A
1

I have since learned from Google support that things changed (at least for cloud functions) since Node 10. Node 8 still logged correctly with console.info getting level info and console.warn getting level warning, and that seems to align with my experience.

In recent versions of firebase-functions there is the logger library which you should use for writing logs. For non-firebase environments you can use @google-cloud/logging which seems essentially the same thing. You then have full control over severity level as well as the ability to log extra JSON payload as the second parameter.

So in other words, don't use the native Javascript console methods.

Airless answered 4/8, 2020 at 9:26 Comment(0)
B
3

Cloud Functions only distinguishes between stdout & stderr.

The docs on Writing, Viewing, and Responding to Logs say that "Cloud Functions includes simple logging by default. Logs written to stdout or stderr will appear automatically". The logging docs page that you referenced mentions the same thing about stdout & stderr being automatic for Cloud Functions.

My interpretation is that console.warn() is going to stderr, and once there the distinction between warn and error is lost. I suspect you'd see the same for console.debug() showing up as INFO. I have this behavior in VMs when stderr is used, but I think App Engine does not have this problem.

I don't think the logging docs page is suggesting Bunyan specifically. It addresses Winston similarly, as well as a client library (in which case authentication should just work).

Error Reporting has a specific notion of what constitutes an "error" to be captured: https://cloud.google.com/error-reporting/docs/formatting-error-messages

Biondo answered 6/6, 2019 at 14:2 Comment(2)
So I guess the only way to get a distinction between error, warning, info and debug is to use the stackdriver logging library and set the "severity" field in the logEntry data: cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntryAirless
This page cloud.google.com/functions/docs/monitoring/logging says "Logs emitted using console.log() have the INFO log level." and "Logs emitted using console.error() have the ERROR log level." and "Logs written directly to stdout or stderr do not have an associated log level."Underfeed
A
1

I have since learned from Google support that things changed (at least for cloud functions) since Node 10. Node 8 still logged correctly with console.info getting level info and console.warn getting level warning, and that seems to align with my experience.

In recent versions of firebase-functions there is the logger library which you should use for writing logs. For non-firebase environments you can use @google-cloud/logging which seems essentially the same thing. You then have full control over severity level as well as the ability to log extra JSON payload as the second parameter.

So in other words, don't use the native Javascript console methods.

Airless answered 4/8, 2020 at 9:26 Comment(0)
R
-1

If your logs are showing up in Stackdriver Logging, then Error Reporting is at least able to see them. From there, there's some more requirements that depend on exactly what you're using (eg if you're just logging JSON, it may need a reportLocation with serviceContext).

This might be useful: https://cloud.google.com/error-reporting/docs/formatting-error-messages

On the other hand, if you're just trying to view severity ERROR logs, just using the advanced filter in Logging for severity=ERROR might do what you're looking for?

Riana answered 24/4, 2019 at 17:19 Comment(1)
I would like to use the filter to to show only errors, but the things I printed with console.warn() are recorded by stackdriver as severity=error.Airless

© 2022 - 2024 — McMap. All rights reserved.