This was designed as a complete and feature-packed drop-in replacement for 15+ unmaintained and poorly documented packages on NPM for sentry and raven winston loggers... such as winston-sentry, winston-raven, sentry-logger, etc.
Do you want to log your user objects along with every log automatically?
If so, we can simply use custom middleware to bind a logger method on the ctx object in Koa, or the req object in Express.
This example implementation assumes that you're using the standard Passport implementation and that the logged-in user object is set to ctx.state.user for Koa or req.user for Express. This is the standard usage, so you should have nothing to worry about!
If you need to whitelist only certain fields from ctx.state.user or req.user (e.g. don't send your passwords to it) then you need to specify the option parseUser through options.config.parseUser as documented here https://docs.sentry.io/clients/node/config/. The default fields whitelisted are [ 'id', 'username', 'email' ]. If you specify options.config.parseUser: true then all keys will be collected. If you specify false then none will be collected.
app.use(asyncfunction(ctx,next){try{constpost=awaitPost.create({message: 'hello world'});logger.info('post created',{extra: post});ctx.body=post;}catch(err){ctx.throw(err);// or you could also do `logger.error(err);`,// but it's redundant since `app.emit('error')`// will get invoked when `ctx.throw` occurs in the app}});
Express Example
constwinston=require('winston');constSentry=require('winston-raven-sentry');constexpress=require('express');constpassport=require('passport');const_=require('lodash');constapp=newexpress();constlogger=newwinston.Logger();logger.add(Sentry,{// ...});// define this first before all elseapp.use(logger.transports.sentry.raven.requestHandler());app.use(passport.initialize());app.get('/',function(req,res,next){thrownewError('oops!');});// keep this before all other error handlersapp.use(logger.transports.sentry.raven.errorHandler());app.listen(3000);
install (Boolean) - automatically catches uncaught exceptions through Raven.install if set to true (defaults to false)
errorHandler (Function) - a callback function to use for logging Raven errors (e.g. an invalid DSN key). This defaults to logging the err.message, see Default Error Handler below... but if you wish to disable this just pass errorHandler: false. If there is already an error listener then this function will not get bound.
raven (Object) - an optional instance of Raven that is already configured via Raven.config (if provided this will be used instead of the config option
Transport related options:
name (String) - transport's name (defaults to sentry)
silent (Boolean) - suppress logging (defaults to false)
level (String) - transport's level of messages to log (defaults to info)
levelsMap (Object) - log level mapping to Sentry (see Log Level Mapping below)
Default Raven Options (options.config)
logger (String) - defaults to winston-raven-sentry
captureUnhandledRejections (Boolean) - defaults to false
culprit (String) - defaults to the module or function name
server_name (String) - defaults to process.env.SENTRY_NAME or os.hostname()
release (String) - defaults to process.env.SENTRY_RELEASE (see #343 if you'd like to have the git hash or package version as the default)
tags (Array or Object) - no default value
environment (String) - defaults to process.env.SENTRY_ENVIRONMENT (see #345 if you'd like to have this default to process.env.NODE_ENV instead)
modules (Object) - defaults to package.json dependencies
By default, if you provide an Error instance to either the msg or meta arguments to logger[level](msg, meta), then this package will automatically set meta.extra.err for you as follows:
This ensures that your stack trace and error message are visible and saved to Sentry.
Furthermore, if msg was an Error object, then we will automatically set msg = msg.message.
This will prevent you from receiving the following message in Sentry:
Recommended Logging Approach
Compared to packages such as winston-sentry and winston-raven, we log messages to Sentry more accurately using captureMessage and captureException (and take into consideration Error instances). There was a core bug in all other similar packages on NPM that did not pass along the log level properly, therefore it was refactored and also built to the standards of raven itself (e.g. we utilize the defaults that they also do, see above options).
Here are a few examples provided below for how we recommend logging:
Log an error with stack trace (uses Raven.captureException):
logger.error(newError('something happened'));
Note that this will automatically set extra.err.message = "something happened" and provide the stack trace as extra.err.stack.
Log an error message (uses Raven.captureException - don't worry as this method automatically turns the message below into an Error instance for us):
logger.error('something happened');
Note that this will automatically set extra.err.message = "something happened" and provide the stack trace as extra.err.stack.
Log an error with stack trace and extra data (uses Raven.captureException):
请发表评论