Skip to content

How to format an error as Error? #754

@christian-schwaderer

Description

@christian-schwaderer

My goal is that each time I call logger.error with whatever content an entry in Error Reporting is created.

However, that doesn't work. Only some errors make it to the Error Reporting, some others appear well in the logs, but not in Error Reporting.

I've read the "Error Reporting" paragraph in the docs. But, still, I don't get it.

  1. How do you pass an Error object? Tried like Object.assign(info, { message: new Error(info.message) }); - but without luck so far.
  2. What is the expected content of service and version? I've set a random string in service and left version aside.
  3. What else am I missing?

Here's my complete config:

import winston from 'winston';
import { LoggingWinston } from '@google-cloud/logging-winston';
import compact from 'lodash/compact';

const loggingWinston = new LoggingWinston({ serviceContext: {  service: `some-random-string-here` } });

const formatErrorsAsError = winston.format((info) => {
  if (info.level === 'error') {
    Object.assign(info, { message: new Error(info.message) });
  }
  return info;
});

const logger = winston.createLogger({
  level: vars.env === 'development' ? 'debug' : 'info',
  format: winston.format.combine(
    formatErrorsAsError(),
    vars.env === 'development'
      ? winston.format.colorize()
      : winston.format.uncolorize(),
    winston.format.splat(),
    winston.format.printf(({ level, message }) => `${level}: ${message}`),
  ),
  transports: compact([
    new winston.transports.Console({
      stderrLevels: ['error'],
    }),
    vars.env !== 'development' && loggingWinston,
  ]),
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    api: loggingIssues related to the googleapis/nodejs-logging-winston API.priority: p3Desirable enhancement or fix. May not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions