nodejs-logging-bunyan icon indicating copy to clipboard operation
nodejs-logging-bunyan copied to clipboard

Doubts with Bunyan usage in Cloud Functions

Open mat105 opened this issue 3 years ago • 2 comments

Hello.

I'm trying to add logs on my nodejs CF with bunyan.

My logging setup looks like this:

const { ENV } = require("./env");

const bunyan = require('bunyan');

// Imports the Google Cloud client library for Bunyan
const {LoggingBunyan} = require('@google-cloud/logging-bunyan');

// Creates a Bunyan Cloud Logging client
const loggingBunyan = new LoggingBunyan({
  redirectToStdout: true,
  logName: 'tracking-logs' // Al parecer esto no funciona con redirectToStdout
});


let streams = [];
if (ENV === 'local') {
  console.info("Local logging");
  streams = [
    {stream: process.stdout, level: 'debug'}
  ];
} else {
  console.info("GCP logging");
  streams = [
    // Log to the console at 'info' and above
    // {stream: process.stdout, level: 'info'},
    // And log to Cloud Logging, logging at 'info' and above
    loggingBunyan.stream('info')
  ];
}

// Create a Bunyan logger that streams to Cloud Logging
// Logs will be written to: "projects/YOUR_PROJECT_ID/logs/bunyan_log"
const logger = bunyan.createLogger({
  // The JSON payload of the log as it appears in Cloud Logging
  // will contain "name": "my-service"
  name: 'engine',
  streams: streams
});


exports.logger = logger;

It seems to work OK on the logging explorer, severity and jsonPayload of logs is shown OK, but on the CF records tab it shows this:

image

Based on the documentation, it says we should prefer setting redirectToStdout: true for cloud functions, but when i do this, the logName doesn't show up. In order to avoid log duplication in the logging explorer i commented the stream that is mapped to process.stdout.

My questions are:

  • Should i leave both process.stdout and loggingBunyan.stream? If i leave both i have duplicated records on my log explorer.
  • If i use redirectToStdout, is logName ignored for logging explorer?
  • Why does my cloud function records tab show JSON entries without a visible message and a {{projectId}} logname field?
  • Is there some recommended way of logging for cloud functions?? I want to be able to know the severity in the logging explorer and save in the jsonPayload debug data.

mat105 avatar Apr 05 '22 20:04 mat105

Any update on this? Seeing the same behavior.

Bmgaynor avatar May 02 '22 17:05 Bmgaynor

Sorry on late response @mat105 and @Bmgaynor. Please see corresponding answers to your question:

  1. Indeed, if you use both, stdout and LoggingBunyan.stream you will have duplicate records which I believe should be avoided.
  2. If you use redirectToStdout, indeed the logName is going to be overriden by Cloud Run/Functions. This is by design as of now we have an open issue to resolve this matter and we dont have a clear ETA. Thats said, we do embed logName in jsonPayload when logs are printed to stdout.
  3. Could you perhaps share the expanded message? I guess that message is not shown clearly in Logs Explorer because you didn't set to false the useMessageField described here.
  4. Using redirectToStdout is more reliable in Cloud Run/Functions - I would recommend to update to latest library and retry your code with redirectToStdout = true and useMessageField = false and sharing your feedback if the output looks acceptable.

losalex avatar Sep 13 '22 23:09 losalex

Closing this issue for now since seems there is no more comments - feel free to ask more questions or reactivate per need. Thanks!

losalex avatar Nov 08 '22 00:11 losalex

@losalex any update on the logName overriden by Cloud Run? We need to used sink to route the logs to gcs, but since sinks don't support paths, and logName is not configurable, we have to create a bucket per service and this generates a lot of overhead

arnagar avatar Jan 22 '23 10:01 arnagar