winston-transport-sentry-node icon indicating copy to clipboard operation
winston-transport-sentry-node copied to clipboard

Catch all uncaught errors

Open moisesrodriguez opened this issue 5 years ago • 5 comments

Hello,

I've been using this transport that has an outdated version of winston and Sentry. In it, it has an option called pathGlobal that it will catch all uncaught errors. Is there something like this in this transport?

Thanks

moisesrodriguez avatar Nov 07 '19 16:11 moisesrodriguez

Yes, you can setup level: 'error'.

Like this: options: { level: 'error', ... }

https://github.com/aandrewww/winston-transport-sentry-node/blob/master/src/transport.ts#L69

To be more universal, needed to make changes to the code

aandrewww avatar Nov 13 '19 13:11 aandrewww

@aandrewww ok that should do the trick. But I think it would be nice to make that a configuration option if you want to capture exceptions without having to set the log level to error. More than anything because I want to capture de errors for Sentry and have them show in the Sentry page.

moisesrodriguez avatar Nov 14 '19 22:11 moisesrodriguez

@aandrewww This doesn't seem to catch uncaught exceptions which seems to be a feature in Winston. The line you referenced seems to be for logging errors, e.g. with logger.error(...) not for https://github.com/winstonjs/winston#handling-uncaught-exceptions-with-winston

I've experienced this myself using this transport where an uncaught exception was logged to the console (since the transport is configured with handleExceptions: true) but did not see the exception in Sentry.

I tried to wrap this transport in winston.exceptions.handle() as well, but it threw an error saying there was no log method.

ianwalter avatar Feb 21 '20 14:02 ianwalter

I think I have the same problem. While the console and file transports are working, sentry isn't. Strangely, if I change the sentry level from error to debug it works. Maybe something related to the pathGlobal not being enabled and the module not catching exceptions?

import { AppModule } from "@/app.module"
import { EnvConfig } from "@/env.config"
import { ValidationPipe } from "@nestjs/common"
import { NestFactory } from "@nestjs/core"
import { NestExpressApplication } from "@nestjs/platform-express"
import * as bodyParser from "body-parser"
import { utilities as winstonModuleUtilities, WinstonModule } from "nest-winston"
import { join } from "path"
import * as winston from "winston"
import winstonSentry from "winston-transport-sentry-node"

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule, {
    logger: WinstonModule.createLogger({
      level: "debug",
      transports: [
        new winston.transports.Console({
          level: "debug",
          format: winston.format.combine(winston.format.timestamp(), winstonModuleUtilities.format.nestLike()),
        }),
        new winstonSentry({
          level: "error",
          sentry: {
            // debug: EnvConfig.NODE_ENV === "production" ? false : true,
            dsn: EnvConfig.SENTRY_DNS,
            debug: true,
            environment: EnvConfig.NODE_ENV,
          },
        }),
        new winston.transports.File({
          level: "error",
          filename: "application.log",
          dirname: "logs",
        }),
      ],
    }),
  })

  app.enableCors()

  app.use(bodyParser.json({ limit: "10mb" }))
  app.use(bodyParser.urlencoded({ limit: "10mb", extended: true }))

  app.useStaticAssets(join(__dirname, "static"), { prefix: "/static" })

  app.setBaseViewsDir(join(__dirname, "views"))
  app.setViewEngine("hbs")

  app.useGlobalPipes(
    new ValidationPipe({
      transform: true,
    }),
  )

  await app.listen(EnvConfig.PORT)
}

bootstrap()

incompletude avatar Dec 30 '20 04:12 incompletude

@aandrewww This doesn't seem to catch uncaught exceptions which seems to be a feature in Winston. The line you referenced seems to be for logging errors, e.g. with logger.error(...) not for https://github.com/winstonjs/winston#handling-uncaught-exceptions-with-winston

I've experienced this myself using this transport where an uncaught exception was logged to the console (since the transport is configured with handleExceptions: true) but did not see the exception in Sentry.

I tried to wrap this transport in winston.exceptions.handle() as well, but it threw an error saying there was no log method.

How have you solved?

incompletude avatar Dec 30 '20 05:12 incompletude