2015-11-13 23:38:55 +03:00
|
|
|
//
|
2019-10-03 04:41:16 +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
|
|
|
|
2016-07-26 23:12:51 +03:00
|
|
|
const encryptionMetadataKey = '_ClientEncryptionMetadata2';
|
2016-07-26 23:25:22 +03:00
|
|
|
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';
|
2016-07-26 23:12:51 +03:00
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-07-20 09:47:41 +03:00
|
|
|
logger.token('id', function getUserId(req) {
|
2016-07-26 23:12:51 +03:00
|
|
|
const config = req.app.settings.runtimeConfig;
|
2016-07-20 09:47:41 +03:00
|
|
|
if (config) {
|
2016-07-26 23:12:51 +03:00
|
|
|
const userType = config.authentication.scheme === 'aad' ? 'azure' : 'github';
|
2016-07-20 09:47:41 +03:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2016-07-26 23:25:22 +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);
|
2016-07-26 23:25:22 +03:00
|
|
|
};
|