node.jstypescriptgoogle-cloud-platformwinstongoogle-cloud-run

Structured Logs in Google Cloud Run not being parsed (using Winston for logging)


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.


Solution

  • 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());