Do not use global logger in middlewares, use the one initialized in res.locals instead

This commit is contained in:
Daniel García Aubert 2020-06-02 09:00:45 +02:00
parent e644201756
commit 62d66f2dbc
11 changed files with 20 additions and 26 deletions

View File

@ -86,8 +86,7 @@ module.exports = class ApiRouter {
const { rendererCache, tileBackend, attributesBackend, previewBackend, mapBackend, mapStore } = windshaftFactory({
rendererOptions: serverOptions,
redisPool,
onTileErrorStrategy: getOnTileErrorStrategy({ enabled: environmentOptions.enabledFeatures.onTileErrorStrategy }),
logger: global.logger
onTileErrorStrategy: getOnTileErrorStrategy({ enabled: environmentOptions.enabledFeatures.onTileErrorStrategy })
});
const rendererStatsReporter = new RendererStatsReporter(rendererCache, serverOptions.renderCache.statsInterval);

View File

@ -95,7 +95,6 @@ module.exports = class AnonymousMapController {
metrics({
enabled: this.config.pubSubMetrics.enabled,
metricsBackend: this.metricsBackend,
logger: global.logger,
tags: metricsTags
}),
credentials(),

View File

@ -70,7 +70,6 @@ module.exports = class PreviewTemplateController {
metrics({
enabled: this.config.pubSubMetrics.enabled,
metricsBackend: this.metricsBackend,
logger: global.logger,
tags: metricsTags
}),
credentials(),
@ -337,17 +336,13 @@ function setContentTypeHeader () {
};
}
function incrementMapViewsError (ctx) {
return `ERROR: failed to increment mapview count for user '${ctx.user}': ${ctx.err}`;
}
function incrementMapViews ({ metadataBackend }) {
return function incrementMapViewsMiddleware (req, res, next) {
const { user, mapConfigProvider } = res.locals;
const { user, mapConfigProvider, logger } = res.locals;
mapConfigProvider.getMapConfig((err, mapConfig) => {
if (err) {
global.logger.info(incrementMapViewsError({ user, err }));
logger.warn(err, `ERROR: failed to increment mapview count for user '${user}'`);
return next();
}
@ -359,7 +354,7 @@ function incrementMapViews ({ metadataBackend }) {
metadataBackend.incMapviewCount(user, statTag, (err) => {
if (err) {
global.logger.info(incrementMapViewsError({ user, err }));
logger.warn(err, `ERROR: failed to increment mapview count for user '${user}'`);
}
next();

View File

@ -6,11 +6,11 @@ module.exports = function setCacheChannelHeader () {
return next();
}
const { mapConfigProvider } = res.locals;
const { mapConfigProvider, logger } = res.locals;
mapConfigProvider.getAffectedTables((err, affectedTables) => {
if (err) {
global.logger.warn(err, 'ERROR generating Cache Channel Header');
logger.warn(err, 'ERROR generating Cache Channel Header');
return next();
}

View File

@ -40,11 +40,11 @@ module.exports = function setCacheControlHeader ({
return next();
}
const { mapConfigProvider = { getAffectedTables: callback => callback() } } = res.locals;
const { mapConfigProvider = { getAffectedTables: callback => callback() }, logger } = res.locals;
mapConfigProvider.getAffectedTables((err, affectedTables) => {
if (err) {
global.logger.warn(err, 'ERROR generating Cache Control Header');
logger.warn(err, 'ERROR generating Cache Control Header');
return next();
}

View File

@ -2,7 +2,7 @@
module.exports = function incrementMapViewCount (metadataBackend) {
return function incrementMapViewCountMiddleware (req, res, next) {
const { mapConfig, user } = res.locals;
const { mapConfig, user, logger } = res.locals;
const statTag = mapConfig.obj().stat_tag;
if (statTag) {
@ -14,7 +14,7 @@ module.exports = function incrementMapViewCount (metadataBackend) {
req.profiler.done('incMapviewCount');
if (err) {
global.logger.warn(err, `ERROR: failed to increment mapview count for user '${user}'`);
logger.warn(err, `ERROR: failed to increment mapview count for user '${user}'`);
}
next();

View File

@ -6,7 +6,7 @@ module.exports = function setLastModifiedHeader () {
return next();
}
const { mapConfigProvider, cache_buster: cacheBuster } = res.locals;
const { mapConfigProvider, cache_buster: cacheBuster, logger } = res.locals;
if (cacheBuster) {
const cacheBusterTimestamp = parseInt(cacheBuster, 10);
@ -21,7 +21,7 @@ module.exports = function setLastModifiedHeader () {
mapConfigProvider.getAffectedTables((err, affectedTables) => {
if (err) {
global.logger.warn(err, 'ERROR generating Last Modified Header');
logger.warn(err, 'ERROR generating Last Modified Header');
return next();
}

View File

@ -5,10 +5,10 @@ const uuid = require('uuid');
module.exports = function logger () {
return function loggerMiddleware (req, res, next) {
const id = req.get('X-Request-Id') || uuid.v4();
res.locals.logger = global.logger.child({ id });
const logger = res.locals.logger = global.logger.child({ id });
res.locals.logger.info(req);
res.on('finish', () => res.locals.logger.info(res));
logger.info(req);
res.on('finish', () => logger.info(res));
next();
};

View File

@ -3,7 +3,7 @@
const EVENT_VERSION = '1';
const MAX_LENGTH = 100;
module.exports = function metrics ({ enabled, tags, metricsBackend, logger }) {
module.exports = function metrics ({ enabled, tags, metricsBackend }) {
if (!enabled) {
return function metricsDisabledMiddleware (req, res, next) {
next();
@ -15,6 +15,8 @@ module.exports = function metrics ({ enabled, tags, metricsBackend, logger }) {
}
return function metricsMiddleware (req, res, next) {
const { logger } = res.locals;
res.on('finish', () => {
const { event, attributes } = getEventData(req, res, tags);

View File

@ -5,7 +5,7 @@ const NamedMapMapConfigProvider = require('../../models/mapconfig/provider/named
module.exports = function setSurrogateKeyHeader ({ surrogateKeysCache }) {
return function setSurrogateKeyHeaderMiddleware (req, res, next) {
const { user, mapConfigProvider } = res.locals;
const { user, mapConfigProvider, logger } = res.locals;
if (mapConfigProvider instanceof NamedMapMapConfigProvider) {
surrogateKeysCache.tag(res, new NamedMapsCacheEntry(user, mapConfigProvider.getTemplateName()));
@ -17,7 +17,7 @@ module.exports = function setSurrogateKeyHeader ({ surrogateKeysCache }) {
mapConfigProvider.getAffectedTables((err, affectedTables) => {
if (err) {
global.logger.warn(err, 'ERROR generating Surrogate Key Header');
logger.warn(err, 'ERROR generating Surrogate Key Header');
return next();
}

View File

@ -93,7 +93,6 @@ module.exports = class NamedMapController {
metrics({
enabled: this.config.pubSubMetrics.enabled,
metricsBackend: this.metricsBackend,
logger: global.logger,
tags: metricsTags
}),
credentials(),