Skip to content

Commit 2e08f8b

Browse files
authored
Merge pull request #93 from CyberSource/support-for-external-logger-configuration
External Logger support changes
2 parents 26f153a + e20618d commit 2e08f8b

File tree

7 files changed

+83
-11
lines changed

7 files changed

+83
-11
lines changed

generator/cybersource-javascript-template/index.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,6 @@
9898
exports.LogConfiguration = require('./authentication/logging/LogConfiguration.js');
9999
exports.SensitiveDataTags = require('./authentication/logging/SensitiveDataTags.js');
100100
exports.SensitiveDataMasker = require('./authentication/logging/SensitiveDataMasker.js');
101-
101+
exports.ExternalLoggerWrapper = require('./authentication/logging/ExternalLoggerWrapper.js');
102102
return exports;<={{ }}=>
103103
}));

src/authentication/core/MerchantConfig.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,9 @@ MerchantConfig.prototype.defaultPropValues = function defaultPropValues() {
526526

527527
return merchantMap.toObject();
528528
}
529-
logger.clear();
529+
if(!this.logConfiguration.isExternalLoggerSet){
530+
logger.clear();
531+
}
530532
}
531533

532534
module.exports = MerchantConfig;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
class ExternalLoggerWrapper{
4+
5+
extLogger;
6+
7+
constructor(externalLogger) {
8+
this.extLogger = externalLogger;
9+
}
10+
11+
getLogger(){
12+
return this.extLogger;
13+
}
14+
15+
isLoggerEmpty(){
16+
if(this.extLogger === undefined)
17+
return false;
18+
return true;
19+
}
20+
}
21+
22+
module.exports = ExternalLoggerWrapper;

src/authentication/logging/LogConfiguration.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22

33
var Constants = require('../util/Constants');
4+
const ExternalLoggerWrapper = require('../logging/ExternalLoggerWrapper');
5+
const ApiException = require('../util/ApiException');
46

57
class LogConfiguration {
68
enableLog;
@@ -10,6 +12,8 @@ class LogConfiguration {
1012
loggingLevel;
1113
maxLogFiles;
1214
enableMasking;
15+
hasExternalLogger;
16+
externalLogger;
1317

1418
constructor(logConfig) {
1519
this.setLogEnable(logConfig.enableLog);
@@ -19,6 +23,8 @@ class LogConfiguration {
1923
this.setLoggingLevel(logConfig.loggingLevel);
2024
this.setMaxLogFiles(logConfig.maxLogFiles);
2125
this.setMaskingEnabled(logConfig.enableMasking);
26+
this.setHasExternalLogger(logConfig.hasExternalLogger);
27+
this.setExternalLogger(logConfig.externalLogger);
2228
}
2329

2430
isLogEnabled() {
@@ -43,6 +49,22 @@ class LogConfiguration {
4349
this.enableMasking = enableMaskingValue;
4450
}
4551

52+
setHasExternalLogger(hasExternalLogger){
53+
this.hasExternalLogger = hasExternalLogger;
54+
}
55+
56+
isExternalLoggerSet(){
57+
return this.hasExternalLogger;
58+
}
59+
60+
setExternalLogger(externalLogger){
61+
this.externalLogger = externalLogger;
62+
}
63+
64+
getExternalLogger(){
65+
return this.externalLogger;
66+
}
67+
4668
getLogDirectory () {
4769
return this.logDirectory;
4870
}
@@ -64,7 +86,7 @@ class LogConfiguration {
6486
setLogFileName (logFileNameValue) {
6587
this.logFileName = logFileNameValue;
6688
}
67-
89+
6890
getLogFileMaxSize () {
6991
return this.logFileMaxSize;
7092
}
@@ -90,7 +112,7 @@ class LogConfiguration {
90112
getMaxLogFiles () {
91113
return this.maxLogFiles;
92114
}
93-
115+
94116
/**
95117
* @param {any} maxLogFilesValue
96118
*/
@@ -99,6 +121,19 @@ class LogConfiguration {
99121
}
100122

101123
getDefaultLoggingProperties(warningMessage) {
124+
125+
if(typeof (this.hasExternalLogger) === "boolean" && this.hasExternalLogger === true){
126+
this.hasExternalLogger = true;
127+
} else {
128+
this.hasExternalLogger = false;
129+
}
130+
131+
if((typeof (this.externalLogger) === "object" && !(this.externalLogger instanceof ExternalLoggerWrapper))
132+
|| this.externalLogger === undefined || !(this.externalLogger.isLoggerEmpty())){
133+
ApiException.LoggerException("No valid external logger object found. Turning off external logger flag.")
134+
this.hasExternalLogger = false;
135+
}
136+
102137
if (typeof (this.enableLog) === "boolean" && this.enableLog === true) {
103138
this.enableLog = true;
104139
} else {

src/authentication/logging/Logger.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { format } = require('winston');
33
const DataMasker = require('./SensitiveDataMasker');
44
const { combine, timestamp, label, printf } = format;
55
require('winston-daily-rotate-file');
6+
const ExternalLoggerWrapper = require('../logging/ExternalLoggerWrapper');
67

78
const maskedLoggingFormat = printf(({ level, message, label, timestamp }) => {
89
return `[${timestamp}] [${level.toUpperCase()}] [${label}] : ${DataMasker.maskSensitiveData(message)}`;
@@ -13,6 +14,13 @@ const unmaskedLoggingFormat = printf(({ level, message, label, timestamp }) => {
1314
});
1415

1516
exports.getLogger = function (merchantConfig, loggerCategory = 'UnknownCategoryLogger') {
17+
18+
if(merchantConfig.getLogConfiguration().isExternalLoggerSet() && merchantConfig.getLogConfiguration().getExternalLogger()
19+
&& merchantConfig.getLogConfiguration().getExternalLogger().getLogger()
20+
&& merchantConfig.getLogConfiguration().getExternalLogger() instanceof ExternalLoggerWrapper){
21+
let logger = merchantConfig.getLogConfiguration().getExternalLogger().getLogger();
22+
return logger;
23+
}
1624
var enableLog = merchantConfig.getLogConfiguration().isLogEnabled();
1725
var enableMasking = merchantConfig.getLogConfiguration().isMaskingEnabled();
1826
var loggerCategoryRandomiser = Math.floor((Math.random() * 1000) + 1);
@@ -26,19 +34,19 @@ exports.getLogger = function (merchantConfig, loggerCategory = 'UnknownCategoryL
2634
newLogger = winston.loggers.get(loggerCategory + loggerCategoryRandomiser, {
2735
level: loggingLevel,
2836
format: combine(
29-
label({ label: loggerCategory }),
30-
timestamp(),
31-
enableMasking ? maskedLoggingFormat : unmaskedLoggingFormat
37+
label({ label: loggerCategory }),
38+
timestamp(),
39+
enableMasking ? maskedLoggingFormat : unmaskedLoggingFormat
3240
),
3341
transports: appTransports
3442
});
3543
} else {
3644
newLogger = winston.loggers.get(loggerCategory + loggerCategoryRandomiser, {
3745
level: loggingLevel,
3846
format: combine(
39-
label({ label: loggerCategory }),
40-
timestamp(),
41-
enableMasking ? maskedLoggingFormat : unmaskedLoggingFormat
47+
label({ label: loggerCategory }),
48+
timestamp(),
49+
enableMasking ? maskedLoggingFormat : unmaskedLoggingFormat
4250
),
4351
transports: [new winston.transports.Console({
4452
silent: !enableLog

src/authentication/util/ApiException.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ exports.ApiException = function (message, logger) {
1212
exports.AuthException = function (message) {
1313
var err = new Error(message);
1414
throw err;
15+
}
16+
17+
exports.LoggerException = function (message) {
18+
var err = new Error(message);
19+
console.log(err);
1520
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3972,6 +3972,6 @@
39723972
exports.LogConfiguration = require('./authentication/logging/LogConfiguration.js');
39733973
exports.SensitiveDataTags = require('./authentication/logging/SensitiveDataTags.js');
39743974
exports.SensitiveDataMasker = require('./authentication/logging/SensitiveDataMasker.js');
3975-
3975+
exports.ExternalLoggerWrapper = require('./authentication/logging/ExternalLoggerWrapper.js');
39763976
return exports;
39773977
}));

0 commit comments

Comments
 (0)