4

I'm attempting to format my logs in such a way that Google Cloud will correctly extract the log level. This is running on Cloud Run, with typescript. Cloud Run is grabbing the logs from the container output.

If I do the following, google correctly parses the log line:

console.log(JSON.stringify({
   severity: 'ERROR',
   message: 'This is testing a structured log error for GCP'
}));

And the log output looks like this: log level error

I've tried a number of different ways to format with winston, ended up with the following:

useFormat = format.combine(
      format((info, opts) => {
        info['severity'] = info.level;
        delete info.level;
        return info;
      })(),
      format.json());


    this.winston = winston.createLogger({
      level: logLevel,
      format: useFormat,
      transports: [new winston.transports.Console()]
    });

Which looks like it will work (it correctly outputs the json line), I get this in the GCP logs: enter image description here

Any help appreciated.

2 Answers 2

12

Turns out I was close, just needed to .upperCase() the log level (and I'm mapping Verbose -> Debug, I don't really understand why GCP decided to do a totally different log leveling system than everyone else). New code:

useFormat = 
    format.combine(
      format((info, opts) => {
        let level = info.level.toUpperCase();
          if(level === 'VERBOSE') {
            level = 'DEBUG';
          }

          info['severity'] = level;
          delete info.level;
          return info;
      })(),
      format.json());
4
  • Hi @patrickWhite good job finding the answer for your question. Please accept it for better visibility for another SO users. Sep 23, 2019 at 10:23
  • Stackdriver log-levels are documented in cloud.google.com/logging/docs/reference/v2/rest/v2/…, and are based on tools.ietf.org/html/rfc5424#section-6.2.1, "verbose" has always struck me not as a level but as a request for more output... such as showing those debug logs.
    – Grayside
    Sep 23, 2019 at 18:33
  • I wish as a community we had just used 1 to N for log levels so people could use it as they see fit, but most libraries have settled with a debug and a verbose, and NOT having a critical, so stack driver is just a bit of an impedance mismatch. But, appears to be working, now to figure out some of the nice bells and whistles around tracing, etc. Sep 23, 2019 at 23:18
  • 1
    I just wanted to point out that Google offers a winston transport for Cloud Logging. cloud.google.com/nodejs/docs/reference/logging-winston/latest Oct 26, 2021 at 6:46
0

The last bit of the question is confusing. The problem OP is pointing to is that the json is printed out and the severity is default. The json should not be printed out, only the message, and the severity should be debug. The answer that OP provides does what is wanted.

For others that may be confused in the same way I was.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.