cf-nodejs-logging-support
Advanced tools
Comparing version 3.0.14 to 4.0.0
@@ -37,6 +37,6 @@ var util = require('util'); | ||
// Initializes the core logger, including setup of environment var defined settings | ||
var init = function() { | ||
var init = function () { | ||
// Read dyn. log level header name from environment var | ||
var headerName = process.env[envDynLogHeader]; | ||
if(headerName != null && headerName != "null" && headerName != "") { | ||
if (headerName != null && headerName != "null" && headerName != "") { | ||
dynLogLevelHeader = headerName; | ||
@@ -64,7 +64,6 @@ } else { | ||
// Check if config field needs a set env var to be enabled. If specified env var does not exist, the resulting log field will be replaced by reductedPlaceholder | ||
if(obj.envVarSwitch != null) { | ||
if (obj.envVarSwitch != null) { | ||
var val = process.env[obj.envVarSwitch]; | ||
var pass = (val == "true" || val == "True" || val == "TRUE"); | ||
if(!pass) { | ||
// Uncomment to enable log field reduction (currently breaking test cases) | ||
var pass = (val == "true" || val == "True" || val == "TRUE"); | ||
if (!pass) { | ||
obj.reduce = true; | ||
@@ -77,4 +76,11 @@ } | ||
} else if (obj.source.type == "time") { | ||
preLogConfig.push(obj); | ||
postLogConfig.push(obj); | ||
if (obj.source.pre) | ||
preLogConfig.push(obj); | ||
if (obj.source.post) | ||
postLogConfig.push(obj); | ||
if (!obj.source.pre && !obj.source.post) { | ||
obj.source.type = "static"; | ||
obj.source.value = -1; | ||
preLogConfig.push(obj); | ||
} | ||
} else if ((obj.source.parent != null && obj.source.parent == "res")) { | ||
@@ -110,10 +116,10 @@ postLogConfig.push(obj); | ||
// Replace all fields, which are marked to be reduced and do not equal to their default value, empty or "-", to reductedPlaceholder. | ||
var reduceFields = function(config, logObject) { | ||
for(var i = 0; i < config.length; i++) { | ||
var reduceFields = function (config, logObject) { | ||
for (var i = 0; i < config.length; i++) { | ||
var configEntry = config[i]; | ||
if(configEntry.reduce) { | ||
if (configEntry.reduce) { | ||
var value = logObject[configEntry.name]; | ||
var defaultValue = configEntry.default != null ? configEntry.default : "-"; | ||
if(value == null || value == "" || value == defaultValue) { | ||
if (value == null || value == "" || value == defaultValue) { | ||
continue; | ||
@@ -229,3 +235,3 @@ } | ||
// return, if path is empty. | ||
if(path == null || path.length == 0) { | ||
if (path == null || path.length == 0) { | ||
return null; | ||
@@ -252,3 +258,3 @@ } | ||
// if the path is not empty, recursively resolve the remaining waypoints. | ||
if(path.length >= 1) { | ||
if (path.length >= 1) { | ||
return resolveNestedVariable(value, path); | ||
@@ -261,10 +267,10 @@ } | ||
var checkLoggingLevel = function(level, dynamicLogLevel) { | ||
var checkLoggingLevel = function (level, dynamicLogLevel) { | ||
var threshold; | ||
if(dynamicLogLevel != null) { | ||
if (dynamicLogLevel != null) { | ||
threshold = dynamicLogLevel; // use dynamic log level | ||
} else { | ||
threshold = logLevelInt; // use global log level | ||
} | ||
} | ||
return (threshold >= loggingLevels[level]); | ||
@@ -302,6 +308,6 @@ } | ||
var dynamicLogLevel = this.dynamicLogLevel; | ||
var level = args[0]; | ||
if (!checkLoggingLevel(level,this.dynamicLogLevel)) { | ||
return false; | ||
if (!checkLoggingLevel(level, this.dynamicLogLevel)) { | ||
return false; | ||
} else { | ||
@@ -409,3 +415,3 @@ var logObject = initLog(); | ||
// Sets the dynamic log level for the request to the given level | ||
var setDynamicLoggingLevel = function(levelName) { | ||
var setDynamicLoggingLevel = function (levelName) { | ||
var context = this; | ||
@@ -436,28 +442,26 @@ context.dynamicLogLevel = getLogLevelFromName(levelName); | ||
// Get the name of the dynamic log level header | ||
var getDynLogLevelHeaderName = function() { | ||
var getDynLogLevelHeaderName = function () { | ||
return dynLogLevelHeader; | ||
} | ||
// Get the dynamic logging level from the given JWT. | ||
var getLogLevelFromJWT = function(token) { | ||
// Gets the log level number from a given level name | ||
var getLogLevelFromName = function (levelName) { | ||
if (levelName == null) return null; | ||
return (loggingLevels[levelName.toLowerCase()] != undefined) ? loggingLevels[levelName.toLowerCase()] : null; | ||
} | ||
// Binds the Loglevel extracted from JWT token to the given req | ||
var bindDynLogLevel = function (token, req) { | ||
var payload = verifyAndDecodeJWT(token, dynLogLevelKey); | ||
if(payload == null) { | ||
return null; | ||
if (payload) { | ||
req.dynamicLogLevel = getLogLevelFromName(payload.level); | ||
} | ||
var levelName = payload.level; | ||
return getLogLevelFromName(levelName); | ||
} | ||
// Gets the log level number from a given level name | ||
var getLogLevelFromName = function(levelName) { | ||
if(levelName == null) return null; | ||
return loggingLevels[levelName.toLowerCase()]; | ||
} | ||
// Verifies the given JWT and returns its payload. | ||
var verifyAndDecodeJWT = function(token, key) { | ||
if(key == null || token == null) { | ||
var verifyAndDecodeJWT = function (token, key) { | ||
if (key == null || !token) { | ||
return null; // no public key or jwt provided | ||
@@ -467,4 +471,7 @@ } | ||
try { | ||
return jwt.verify(token, "-----BEGIN PUBLIC KEY-----\n" + key + "\n-----END PUBLIC KEY-----", {algorithms: ["RS256", "RS384", "RS512"]}); | ||
} catch(err) { | ||
if (key.match(/BEGIN PUBLIC KEY/)) | ||
return jwt.verify(token, key, { algorithms: ["RS256", "RS384", "RS512"] }); | ||
else | ||
return jwt.verify(token, "-----BEGIN PUBLIC KEY-----\n" + key + "\n-----END PUBLIC KEY-----", { algorithms: ["RS256", "RS384", "RS512"] }); | ||
} catch (err) { | ||
return null; // token expired or invalid | ||
@@ -494,2 +501,2 @@ } | ||
exports.getDynLogLevelHeaderName = getDynLogLevelHeaderName; | ||
exports.getLogLevelFromJWT = getLogLevelFromJWT; | ||
exports.bindDynLogLevel = bindDynLogLevel; |
@@ -48,10 +48,6 @@ // Log network activity for express applications | ||
var token = req.header(core.getDynLogLevelHeaderName()); | ||
core.bindDynLogLevel(token, req); | ||
if(token != null) { | ||
req.dynamicLogLevel = core.getLogLevelFromJWT(token); | ||
} else { | ||
req.dynamicLogLevel = null; | ||
} | ||
var fallbacks = []; | ||
@@ -78,6 +74,3 @@ var selfReferences = []; | ||
case "time": | ||
if (configEntry.source.pre != null) | ||
logObject[configEntry.name] = configEntry.source.pre(req, res, logObject); | ||
else | ||
logObject[configEntry.name] = -1 //defaulting for time fields | ||
logObject[configEntry.name] = configEntry.source.pre(req, res, logObject); | ||
break; | ||
@@ -88,3 +81,3 @@ case "special": | ||
} | ||
core.handleConfigDefaults(configEntry, logObject, fallbacks); | ||
@@ -96,3 +89,3 @@ } | ||
} | ||
for (var key in selfReferences) { | ||
@@ -137,4 +130,3 @@ logObject[key] = logObject[selfReferences[key]]; | ||
case "time": | ||
if (configEntry.source.post != null) | ||
logObject[configEntry.name] = configEntry.source.post(req, res, logObject); | ||
logObject[configEntry.name] = configEntry.source.post(req, res, logObject); | ||
break; | ||
@@ -162,3 +154,3 @@ case "special": | ||
core.reduceFields(postConfig, logObject); | ||
if(core.checkLoggingLevel(logObject.level,req.dynamicLogLevel)) | ||
if (core.checkLoggingLevel(logObject.level, req.dynamicLogLevel)) | ||
core.sendLog(logObject); | ||
@@ -165,0 +157,0 @@ logSent = true; |
@@ -35,9 +35,4 @@ // Log network activity for express applications | ||
var token = req.headers[core.getDynLogLevelHeaderName()]; | ||
core.bindDynLogLevel(token, req); | ||
if (token != null) { | ||
req.dynamicLogLevel = core.getLogLevelFromJWT(token); | ||
} else { | ||
req.dynamicLogLevel = null; | ||
} | ||
var fallbacks = []; | ||
@@ -64,6 +59,3 @@ var selfReferences = []; | ||
case "time": | ||
if (configEntry.source.pre != null) | ||
logObject[configEntry.name] = configEntry.source.pre(req, res, logObject); | ||
else | ||
logObject[configEntry.name] = -1 //defaulting for time fields | ||
logObject[configEntry.name] = configEntry.source.pre(req, res, logObject); | ||
break; | ||
@@ -114,3 +106,3 @@ case "special": | ||
case "header": | ||
if(res._headers) | ||
if (res._headers) | ||
logObject[configEntry.name] = res._headers[configEntry.source.name]; | ||
@@ -125,4 +117,3 @@ break; | ||
case "time": | ||
if (configEntry.source.post != null) | ||
logObject[configEntry.name] = configEntry.source.post(req, res, logObject); | ||
logObject[configEntry.name] = configEntry.source.post(req, res, logObject); | ||
break; | ||
@@ -150,3 +141,3 @@ case "special": | ||
if(core.checkLoggingLevel(logObject.level,req.dynamicLogLevel)) | ||
if (core.checkLoggingLevel(logObject.level, req.dynamicLogLevel)) | ||
core.sendLog(logObject); | ||
@@ -153,0 +144,0 @@ logSent = true; |
@@ -1,3 +0,1 @@ | ||
/*jshint node:true */ | ||
// Log network activity for restify applications | ||
@@ -47,9 +45,4 @@ | ||
var token = req.header(core.getDynLogLevelHeaderName()); | ||
core.bindDynLogLevel(token, req); | ||
if (token != null) { | ||
req.dynamicLogLevel = core.getLogLevelFromJWT(token); | ||
} else { | ||
req.dynamicLogLevel = null; | ||
} | ||
var fallbacks = []; | ||
@@ -76,6 +69,3 @@ var selfReferences = []; | ||
case "time": | ||
if (configEntry.source.pre != null) | ||
logObject[configEntry.name] = configEntry.source.pre(req, res, logObject); | ||
else | ||
logObject[configEntry.name] = -1 //defaulting for time fields | ||
logObject[configEntry.name] = configEntry.source.pre(req, res, logObject); | ||
break; | ||
@@ -126,4 +116,3 @@ case "special": | ||
case "time": | ||
if (configEntry.source.post != null) | ||
logObject[configEntry.name] = configEntry.source.post(req, res, logObject); | ||
logObject[configEntry.name] = configEntry.source.post(req, res, logObject); | ||
break; | ||
@@ -152,3 +141,3 @@ case "special": | ||
if(core.checkLoggingLevel(logObject.level,req.dynamicLogLevel)) | ||
if (core.checkLoggingLevel(logObject.level, req.dynamicLogLevel)) | ||
core.sendLog(logObject); | ||
@@ -155,0 +144,0 @@ }); |
@@ -1,40 +0,23 @@ | ||
var core; | ||
const Transport = require('winston-transport'); | ||
const { SPLAT } = require('triple-beam'); | ||
var setCoreLogger = function (coreLogger) { | ||
core = coreLogger; | ||
}; | ||
var getWinstonTransport = function () { | ||
var winston; | ||
try { | ||
winston = require("winston"); | ||
} catch (e) { | ||
return null | ||
const CfNodejsLoggingSupportLogger = class CfNodejsLoggingSupportLogger extends Transport { | ||
constructor(options) { | ||
super(options); | ||
this.name = 'CfNodejsLoggingSupportLogger'; | ||
this.level = options.level || 'info'; | ||
this.logMessage = options.logMessage; | ||
} | ||
var winstonTransport = new(winston.transports.Console)({ | ||
timestamp: function () { | ||
return Date.now(); | ||
}, | ||
level: "info", | ||
formatter: function (options) { | ||
// Return string will be passed to winston logger. | ||
var logObject = core.initLog(); | ||
if (options != null) { | ||
if (options.level != null) { | ||
logObject.level = options.level; | ||
} | ||
logObject.msg = (undefined !== options.message ? options.message : ''); | ||
logObject.type = "log"; | ||
if (core.validObject(options.meta)) { | ||
logObject.custom_fields = options.meta; | ||
} | ||
} | ||
return JSON.stringify(logObject); | ||
log(info) { | ||
if (!!info[SPLAT]) { | ||
this.logMessage.apply(this, [info.level, info.message, ...info[SPLAT]]); | ||
} else { | ||
this.logMessage(info.level, info.message); | ||
} | ||
}); | ||
return winstonTransport; | ||
} | ||
} | ||
exports.setCoreLogger = setCoreLogger; | ||
exports.getWinstonTransport = getWinstonTransport; | ||
exports.createTransport = function (options) { | ||
return new CfNodejsLoggingSupportLogger(options); | ||
} |
18
index.js
@@ -42,10 +42,12 @@ //loading core logger functionality | ||
exports.winstonTransport = function () { | ||
var transport = require("./cf-nodejs-logging-support-winston/winston-transport"); | ||
exports.createWinstonTransport = function (options) { | ||
if (!options) { | ||
options = { | ||
level: 'info' | ||
}; | ||
} | ||
options.logMessage = effectiveLogger.logMessage; | ||
return require("./cf-nodejs-logging-support-winston/winston-transport").createTransport(options); | ||
}; | ||
transport.setCoreLogger(coreLogger); | ||
return transport.getWinstonTransport(); | ||
}(); | ||
exports.getCorrelationObject = function () { | ||
@@ -55,3 +57,3 @@ return effectiveLogger.getCorrelationObject(); | ||
exports.setLogPattern = function(args) { | ||
exports.setLogPattern = function (args) { | ||
effectiveLogger.setLogPattern.apply(this, arguments); | ||
@@ -58,0 +60,0 @@ }; |
{ | ||
"name": "cf-nodejs-logging-support", | ||
"version": "3.0.14", | ||
"version": "4.0.0", | ||
"description": "Logging tool for Cloud Foundry", | ||
@@ -28,3 +28,4 @@ "keywords": [ | ||
"chai": "^4.1.2", | ||
"eslint": "^5.4.0", | ||
"codecov": "^3.1.0", | ||
"eslint": "^5.12.1", | ||
"import-fresh": "^2.0.0", | ||
@@ -37,7 +38,9 @@ "istanbul": "^0.4.4", | ||
"sinon": "^6.1.5", | ||
"winston": "^2.4.4" | ||
"winston": "^3.2.1", | ||
"winston-transport": "^4.3.0" | ||
}, | ||
"scripts": { | ||
"test": "istanbul cover node_modules/mocha/bin/_mocha -- --timeout 10000" | ||
"test": "istanbul cover node_modules/mocha/bin/_mocha -- --timeout 10000", | ||
"coverage": "codecov" | ||
} | ||
} |
# Node.js Logging Support for Cloud Foundry | ||
[![Version npm](https://img.shields.io/npm/v/cf-nodejs-logging-support.svg?style=flat-square)](https://www.npmjs.com/package/cf-nodejs-logging-support)[![npm Downloads](https://img.shields.io/npm/dm/cf-nodejs-logging-support.svg?style=flat-square)](https://www.npmjs.com/package/cf-nodejs-logging-support)[![Build Status](https://img.shields.io/travis/SAP/cf-nodejs-logging-support/master.svg?style=flat-square)](https://travis-ci.org/SAP/cf-nodejs-logging-support) | ||
[![Version npm](https://img.shields.io/npm/v/cf-nodejs-logging-support.svg?style=flat-square)](https://www.npmjs.com/package/cf-nodejs-logging-support)[![npm Downloads](https://img.shields.io/npm/dm/cf-nodejs-logging-support.svg?style=flat-square)](https://www.npmjs.com/package/cf-nodejs-logging-support)[![Build Status](https://img.shields.io/travis/SAP/cf-nodejs-logging-support/v4.0.0.svg?style=flat-square)](https://travis-ci.org/SAP/cf-nodejs-logging-support)[![Code Coverage](https://img.shields.io/codecov/c/github/SAP/cf-nodejs-logging-support.svg?style=flat-square)](https://codecov.io/gh/SAP/cf-nodejs-logging-support) | ||
@@ -13,2 +13,3 @@ ## Summary | ||
#### Version 3.0 introduced dynamic log levels, sensitive data reduction and a redesigned field configuration system | ||
#### Version 4.0 changed winston transport api | ||
@@ -110,3 +111,3 @@ ## Features | ||
With addtional numeric value | ||
With additional numeric value | ||
```js | ||
@@ -207,5 +208,5 @@ logMessage("info", "Listening on port %d", 5000); | ||
var logger = new(winston.Logger)({ | ||
var logger = winston.createLogger({ | ||
// Bind transport to winston | ||
transports: [log.winstonTransport] | ||
transports: [log.createWinstonTransport()] | ||
}); | ||
@@ -276,3 +277,3 @@ | ||
* [Express Sample](https://github.com/SAP/cf-nodejs-logging-support/blob/master/sample/cf-nodejs-shoutboard-express) | ||
* [Restify Sample](https://github.com/SAP/cf-nodejs-logging-support/blob/master/sample/cf-nodejs-shoutboard-restify) | ||
* [Express & Restify Sample](https://github.com/SAP/cf-nodejs-logging-support/blob/master/sample/cf-nodejs-shoutboard) | ||
* [Winston Sample](https://github.com/SAP/cf-nodejs-logging-support/blob/master/sample/winston) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
276
85957
12
1282