Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch all uncaught errors #4

Open
moisesrodriguez opened this issue Nov 7, 2019 · 5 comments
Open

Catch all uncaught errors #4

moisesrodriguez opened this issue Nov 7, 2019 · 5 comments

Comments

@moisesrodriguez
Copy link

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

@aandrewww
Copy link
Owner

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

@moisesrodriguez
Copy link
Author

@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.

@ianwalter
Copy link

ianwalter commented Feb 21, 2020

@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.

@incompletude
Copy link

incompletude commented Dec 30, 2020

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
Copy link

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants