From 1882e25d00d8240246b5c14b3eb7f9e460a50596 Mon Sep 17 00:00:00 2001 From: Hannu Lyytikainen Date: Tue, 26 Sep 2017 15:09:22 +0300 Subject: [PATCH] Equalize logger setup in server and browser Set up logger in the server side with the same parameters as in the browser and clean the environment variables out of the server/log.js file. Code commenting improved. --- server/index.js | 14 ++++++++++---- server/log.js | 15 +++++++++------ src/config.js | 1 + src/index.js | 3 ++- src/util/log.js | 4 ++-- 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/server/index.js b/server/index.js index b076b3eb..6130c324 100644 --- a/server/index.js +++ b/server/index.js @@ -31,19 +31,24 @@ const fs = require('fs'); const log = require('./log'); const buildPath = path.resolve(__dirname, '..', 'build'); -const dev = process.env.NODE_ENV !== 'production'; +const ENV = process.env.NODE_ENV; const PORT = process.env.PORT || 4000; const CLIENT_ID = process.env.REACT_APP_SHARETRIBE_SDK_CLIENT_ID || '08ec69f6-d37e-414d-83eb-324e94afddf0'; const BASE_URL = process.env.REACT_APP_SHARETRIBE_SDK_BASE_URL || 'http://localhost:8088'; const ENFORCE_SSL = process.env.SERVER_SHARETRIBE_ENFORCE_SSL === 'true'; const TRUST_PROXY = process.env.SERVER_SHARETRIBE_TRUST_PROXY || null; +const SENTRY_DSN = process.env.SERVER_SENTRY_DSN; +const dev = ENV !== 'production'; const app = express(); const errorPage = fs.readFileSync(path.join(buildPath, '500.html'), 'utf-8'); -// Error request handler -log.setup(); +// Setup error logger +log.setup(SENTRY_DSN, ENV); +// Add logger request handler. In case Sentry is set up +// request information is added to error context when sent +// to Sentry. app.use(log.requestHandler()); // The helmet middleware sets various HTTP headers to improve security. @@ -156,7 +161,8 @@ app.get('*', (req, res) => { }); }); -// Error handler that logs error responses +// Set error handler. If Sentry is set up, all error responses +// (500 and up) will be logged there. app.use(log.errorHandler()); app.listen(PORT, () => { diff --git a/server/log.js b/server/log.js index 14cf2cee..c50e2217 100644 --- a/server/log.js +++ b/server/log.js @@ -5,21 +5,24 @@ */ const Raven = require('raven'); -const SENTRY_DSN = process.env.SERVER_SENTRY_DSN; -const ENV = process.env.NODE_ENV; + +// Sentry client key +let SENTRY_DSN; /** * Set up error loggin. If a Sentry DSN is defined * Sentry client is configured. */ -exports.setup = () => { - if (SENTRY_DSN) { - Raven.config(SENTRY_DSN, { - environment: ENV, +exports.setup = (sentryDsn, env) => { + if (sentryDsn) { + Raven.config(sentryDsn, { + environment: env, autoBreadcrumbs: { http: true, }, }).install(); + + SENTRY_DSN = sentryDsn; } }; diff --git a/src/config.js b/src/config.js index 186d8cab..a7918a3f 100644 --- a/src/config.js +++ b/src/config.js @@ -11,6 +11,7 @@ const sdkBaseUrl = process.env.REACT_APP_SHARETRIBE_SDK_BASE_URL || 'http://loca const currency = process.env.REACT_APP_SHARETRIBE_MARKETPLACE_CURRENCY || 'USD'; +// Sentry DSN (Data Source Name), a client key for authenticating calls to Sentry const sentryDsn = process.env.REACT_APP_PUBLIC_SENTRY_DSN; // Currency formatting options. diff --git a/src/index.js b/src/index.js index 01c4d8f6..8f0ec527 100644 --- a/src/index.js +++ b/src/index.js @@ -74,7 +74,8 @@ if (typeof window !== 'undefined') { setupStripe(); render(store); - log.setup(config.sentryDsn); + // set up logger with Sentry DSN client key and environment + log.setup(config.sentryDsn, config.env); if (config.dev) { // Expose stuff for the browser REPL diff --git a/src/util/log.js b/src/util/log.js index b4e706a4..ed1c1caf 100644 --- a/src/util/log.js +++ b/src/util/log.js @@ -14,9 +14,9 @@ import Raven from 'raven-js'; * * @param {String} sentryDsn A public Sentry DSN */ -export const setup = sentryDsn => { +export const setup = (sentryDsn, env) => { if (sentryDsn !== undefined) { - Raven.config(sentryDsn).install(); + Raven.config(sentryDsn, { environment: env }).install(); } };