opensource-portal/middleware/logger.ts

39 строки
1.5 KiB
TypeScript
Исходник Обычный вид История

2015-11-13 23:38:55 +03:00
//
// Copyright (c) Microsoft.
2015-11-13 23:38:55 +03:00
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
2016-07-14 10:38:47 +03:00
const logger = require('morgan');
2015-11-13 23:38:55 +03:00
const encryptionMetadataKey = '_ClientEncryptionMetadata2';
const piiFormat = ':id :method :scrubbedUrl :status :response-time ms - :res[content-length] :encryptedSession :correlationId';
const format = ':method :scrubbedUrl :status :response-time ms - :res[content-length] :encryptedSession :correlationId';
logger.token('encryptedSession', function getUserId(req) {
const config = req.app.settings.runtimeConfig;
if (req.session && req.session.passport && req.session.passport.user) {
const userType = config.authentication.scheme === 'aad' ? 'azure' : 'github';
return req.session.passport.user[userType] && req.session.passport.user[userType][encryptionMetadataKey] !== undefined ? 'encrypted' : 'plain';
}
});
logger.token('id', function getUserId(req) {
const config = req.app.settings.runtimeConfig;
if (config) {
const userType = config.authentication.scheme === 'aad' ? 'azure' : 'github';
return req.user && req.user[userType] && req.user[userType].username ? req.user[userType].username : undefined;
}
2015-11-13 23:38:55 +03:00
});
logger.token('correlationId', function getCorrelationId(req) {
2016-07-14 21:00:31 +03:00
return req.correlationId;
2015-11-13 23:38:55 +03:00
});
logger.token('scrubbedUrl', function getScrubbedUrl(req) {
2016-07-14 10:38:47 +03:00
return req.scrubbedUrl || req.originalUrl || req.url;
2015-11-13 23:38:55 +03:00
});
module.exports = function createLogger(config) {
2017-06-07 22:40:38 +03:00
return logger(config && config.debug && config.debug.showUsers === true ? piiFormat : format);
};