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

Keep original labels like "instanceId"

Open JudithvW opened this issue 2 years ago • 3 comments

Is your feature request related to a problem? Please describe. A Cloud Run instance can handle multiple requests. Also, multiple Cloud Run instances could be running. It is currently not visible which Cloud Run instance is logging.

Describe the solution you'd like If I use console.log in my NodeJS application, the labels in the log will show the labels of the Cloud Run Service, as well as an "instanceId". When I use this package, those 'default' labels in the logging disappear. I want those labels to be present, even when I add other labels.

Additional context image Example of default google logging (console.log), which shows the Cloud Run labels and an instanceId.

image Example of logging with this package, which removed the original labels and only added the labels set by this package.

JudithvW avatar Sep 13 '23 07:09 JudithvW

Hi @JudithvW, looks like I don't have access to your screenshot. Could you provide a sample code how you write log so I can try repro?

cindy-peng avatar Jan 18 '24 23:01 cindy-peng

const { Logging } = require("@google-cloud/logging");

class Logger {
  constructor() {
    this.logging = new Logging({
      projectId: process.env.GCP_PROJECT,
    });

    this.log = this.logging.log("microservices");

    this.metadata = {
      labels: {},
    };
  }

  setMetadataField(fieldName, fieldValue) {
    this.metadata.labels[fieldName] = fieldValue;
  }

  // NB: message can be a string, but can also be a JSON object (not an Array!)

  async debug(message) {
    const entry = this.log.entry(this.metadata, message);

    await this.log.debug(entry);
  }

  async info(message) {
    const entry = this.log.entry(this.metadata, message);

    await this.log.info(entry);
  }

  async warn(message) {
    const entry = this.log.entry(this.metadata, message);

    await this.log.warning(entry);
  }

  async error(message) {
    const entry = this.log.entry(this.metadata, message);

    await this.log.error(entry);
  }
}

module.exports = Logger;

JudithvW avatar Jan 19 '24 09:01 JudithvW

What was the solution to this? I am facing a similar issue

ali-yunes avatar Aug 08 '24 13:08 ali-yunes