googleapis / nodejs-logging-bunyan

Node.js client integration between Stackdriver Logging and Bunyan.
https://cloud.google.com/logging/
Apache License 2.0
63 stars 34 forks source link

Doubts with Bunyan usage in Cloud Functions #609

Closed mat105 closed 1 year ago

mat105 commented 2 years ago

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:

Bmgaynor commented 2 years ago

Any update on this? Seeing the same behavior.

losalex commented 2 years ago

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 commented 1 year ago

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

arnagar commented 1 year ago

@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