cf-nodejs-logging-support
Advanced tools
Comparing version 1.1.2 to 2.0.0
@@ -1,76 +0,86 @@ | ||
var winston = require("winston"); | ||
var util = require('util'); | ||
var os = require('os'); | ||
const nsPerSec = 1e9; | ||
const logType = "log"; | ||
const loggingLevels = { 'error': 0, 'warn': 1, 'info': 2, 'verbose': 3, 'debug': 4, 'silly': 5 }; | ||
var initDummy = null; | ||
var logLevelInt = 2; | ||
var pattern = null; | ||
var stdout = process.stdout; | ||
// Customized transport, which specifies the correct logging format and logs to stdout | ||
var consoleTransport = new(winston.transports.Console)({ | ||
timestamp: function () { | ||
return Date.now(); | ||
}, | ||
level: "info", | ||
formatter: function (options) { | ||
// Return string will be passed to winston logger. | ||
if (null !== pattern) { | ||
if (undefined !== options.meta) { | ||
var output = pattern; | ||
for (var key in options.meta) { | ||
if (typeof (options.meta[key]) === "object" && validObject(options.meta[key])) { | ||
output = output.replace('{{' + key + '}}', JSON.stringify(options.meta[key])); | ||
} else { | ||
output = output.replace('{{' + key + '}}', options.meta[key]); | ||
} | ||
// Stringify and log given object to console. If a custom pattern is set, the referenced object fields are used to replace the references. | ||
var writeLogToConsole = function(logObject) { | ||
var output = ""; | ||
if (null != pattern) { | ||
if (undefined !== logObject && logObject != null) { | ||
output = pattern; | ||
for (var key in logObject) { | ||
if (typeof (logObject[key]) === "object" && validObject(logObject[key])) { | ||
output = output.replace('{{' + key + '}}', JSON.stringify(logObject[key])); | ||
} else { | ||
output = output.replace('{{' + key + '}}', logObject[key]); | ||
} | ||
return output; | ||
} | ||
return ""; | ||
} else { | ||
return (undefined !== options.meta && validObject(options.meta)) ? JSON.stringify(options.meta) : ''; | ||
} | ||
} else { | ||
output = (undefined !== logObject && validObject(logObject)) ? JSON.stringify(logObject) : ''; | ||
} | ||
}); | ||
stdout.write(output + os.EOL); | ||
} | ||
var winstonLogger = new(winston.Logger)({ | ||
transports: [consoleTransport] | ||
}); | ||
// Sets the minimum logging level. Messages with a lower level, will not be forwarded. (Levels: error, warn, info, verbose, debug, silly) | ||
// Sets the minimum logging level. Messages with a lower level will not be forwarded. (Levels: error, warn, info, verbose, debug, silly) | ||
var setLoggingLevel = function (level) { | ||
consoleTransport.level = level; | ||
}; | ||
logLevelInt = loggingLevels[level]; | ||
} | ||
// Gets the minimum logging level. (Levels: error, warn, info, verbose, debug, silly) | ||
var getLoggingLevel = function () { | ||
return consoleTransport.level; | ||
for(var key in loggingLevels) { | ||
if(loggingLevels[key] == logLevelInt) { | ||
return key; | ||
} | ||
} | ||
}; | ||
// Initializes an empty log object | ||
var initLog = function (logObject, time) { | ||
var vcapEnvironment = ("VCAP_APPLICATION" in process.env) ? JSON.parse(process.env.VCAP_APPLICATION) : {}; | ||
var initLog = function () { | ||
if (time == null) { | ||
time = [0, 0]; | ||
var time = process.hrtime(); | ||
var logObject = {}; | ||
if (initDummy == null) { | ||
logObject = prepareInitDummy(); | ||
initDummy = JSON.stringify(logObject); | ||
} else { | ||
logObject = JSON.parse(initDummy); | ||
} | ||
logObject.written_at = (new Date()).toJSON(); | ||
logObject.written_ts = time[0] * 1e9 + time[1]; | ||
logObject.written_ts = time[0] * nsPerSec + time[1]; | ||
logObject.component_type = "application"; | ||
logObject.component_id = !("application_id" in vcapEnvironment) ? "-" : vcapEnvironment.application_id; | ||
logObject.component_name = !("application_name" in vcapEnvironment) ? "-" : vcapEnvironment.application_name; | ||
logObject.component_instance = !("instance_index" in vcapEnvironment) ? "0" : vcapEnvironment.instance_index.toString(); | ||
logObject.layer = "[NODEJS]"; | ||
return logObject; | ||
logObject.space_name = !("space_name" in vcapEnvironment) ? "-" : vcapEnvironment.space_name; | ||
logObject.space_id = !("space_id" in vcapEnvironment) ? "-" : vcapEnvironment.space_id; | ||
}; | ||
logObject.source_instance = logObject.component_instance; | ||
var prepareInitDummy = function () { | ||
var obj = {}; | ||
var vcapEnvironment = ("VCAP_APPLICATION" in process.env) ? JSON.parse(process.env.VCAP_APPLICATION) : {}; | ||
logObject.container_id = !("CF_INSTANCE_IP" in process.env) ? "-" : process.env.CF_INSTANCE_IP; | ||
obj.component_type = "application"; | ||
obj.component_id = !("application_id" in vcapEnvironment) ? "-" : vcapEnvironment.application_id; | ||
obj.component_name = !("application_name" in vcapEnvironment) ? "-" : vcapEnvironment.application_name; | ||
obj.component_instance = !("instance_index" in vcapEnvironment) ? "0" : vcapEnvironment.instance_index.toString(); | ||
obj.source_instance = obj.component_instance; | ||
logObject.logger = "nodejs-logger"; | ||
obj.layer = "[NODEJS]"; | ||
obj.space_name = !("space_name" in vcapEnvironment) ? "-" : vcapEnvironment.space_name; | ||
obj.space_id = !("space_id" in vcapEnvironment) ? "-" : vcapEnvironment.space_id; | ||
obj.container_id = !("CF_INSTANCE_IP" in process.env) ? "-" : process.env.CF_INSTANCE_IP; | ||
obj.logger = "nodejs-logger"; | ||
return obj; | ||
}; | ||
@@ -83,3 +93,7 @@ | ||
// Write log to console to be parsed by logstash | ||
winstonLogger.log(level, '', logObject); | ||
//winstonLogger.log(level, '', logObject); | ||
if (logLevelInt >= loggingLevels[level]) { | ||
writeLogToConsole(logObject); | ||
} | ||
}; | ||
@@ -111,2 +125,5 @@ | ||
var level = args[0]; | ||
if (logLevelInt < loggingLevels[level]) { | ||
return false; | ||
} | ||
var customFields = null; | ||
@@ -125,3 +142,3 @@ | ||
var logObject = {}; | ||
var logObject = initLog(); | ||
@@ -134,11 +151,12 @@ var req = this; | ||
var time = process.hrtime(); | ||
initLog(logObject, time); | ||
logObject.msg = msg; | ||
logObject.type = "log"; | ||
logObject.type = logType; | ||
if (customFields != null) { | ||
for (var key in customFields) { | ||
if(!((typeof customFields[key]) == "string")) { | ||
customFields[key] = JSON.stringify(customFields[key]); | ||
} | ||
} | ||
logObject.custom_fields = customFields; | ||
@@ -148,2 +166,3 @@ } | ||
sendLog(level, logObject); | ||
return true; | ||
}; | ||
@@ -150,0 +169,0 @@ |
@@ -17,7 +17,5 @@ // Log network activity for express applications | ||
var logNetwork = function (req, res, next) { | ||
var logObject = {}; | ||
var logSent = false; | ||
var time = process.hrtime(); | ||
core.initLog(logObject, time); | ||
var logObject = core.initLog(); | ||
@@ -24,0 +22,0 @@ //rendering the given arguments failsave against missing fields |
@@ -17,7 +17,5 @@ // Log network activity for express applications | ||
var logNetwork = function (req, res) { | ||
var logObject = {}; | ||
var logSent = false; | ||
var time = process.hrtime(); | ||
core.initLog(logObject, time); | ||
var logObject = core.initLog(); | ||
@@ -24,0 +22,0 @@ //rendering the given arguments failsave against missing fields |
@@ -19,6 +19,5 @@ /*jshint node:true */ | ||
var logNetwork = function (req, res, next) { | ||
var logObject = {}; | ||
var time = process.hrtime(); | ||
var logSent = false; | ||
core.initLog(logObject, time); | ||
var logObject = core.initLog(); | ||
@@ -25,0 +24,0 @@ //rendering the given arguments failsave against missing fields |
@@ -17,6 +17,3 @@ var winston = require("winston"); | ||
// Return string will be passed to winston logger. | ||
var logObject = {}; | ||
var time = process.hrtime(); | ||
core.initLog(logObject, time); | ||
var logObject = core.initLog(); | ||
if (options != null) { | ||
@@ -23,0 +20,0 @@ if (options.level != null) { |
{ | ||
"name": "cf-nodejs-logging-support", | ||
"version": "1.1.2", | ||
"description": "Logging tool for Cloud Foundry", | ||
"keywords": [ | ||
"name": "cf-nodejs-logging-support", | ||
"version": "2.0.0", | ||
"description": "Logging tool for Cloud Foundry", | ||
"keywords": [ | ||
"logging", | ||
@@ -10,40 +10,42 @@ "cf", | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/SAP/cf-nodejs-logging-support" | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/SAP/cf-nodejs-logging-support" | ||
}, | ||
"license": "Apache-2.0", | ||
"author": { | ||
"name": "Christian Dinse, Nicklas Dohrn" | ||
}, | ||
"main": "./index.js", | ||
"engines": { | ||
"node": ">=4.0.0" | ||
}, | ||
"dependencies": { | ||
"request": "^2.81.0", | ||
"uuid": "3.0.1" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"eslint": "latest", | ||
"istanbul": "^0.4.4", | ||
"mocha": "^2.5.3", | ||
"mock-require": "^1.3.0", | ||
"node-mocks-http": "^1.6.4", | ||
"rewire": "^2.5.2", | ||
"sinon": "latest", | ||
"winston": "^2.3.1" | ||
}, | ||
"maintainers": [ | ||
{ | ||
"name": "nicklas dohrn", | ||
"email": "nicklas.dohrn@sap.com" | ||
}, | ||
"license": "Apache-2.0", | ||
"author": { | ||
"name": "Christian Dinse, Nicklas Dohrn" | ||
}, | ||
"main": "./index.js", | ||
"engines": { | ||
"node": ">=4.0.0" | ||
}, | ||
"dependencies": { | ||
"uuid": "3.0.1", | ||
"winston": "2.3.1" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"eslint": "latest", | ||
"istanbul": "^0.4.4", | ||
"mocha": "^2.5.3", | ||
"mock-require": "^1.3.0", | ||
"rewire": "^2.5.2", | ||
"sinon": "latest" | ||
}, | ||
"maintainers": [ | ||
{ | ||
"name": "nicklas dohrn", | ||
"email": "nicklas.dohrn@sap.com" | ||
}, | ||
{ | ||
"name": "christian dinse", | ||
"email": "christian.dinse@sap.com" | ||
{ | ||
"name": "christian dinse", | ||
"email": "christian.dinse@sap.com" | ||
} | ||
], | ||
"scripts": { | ||
"test": "istanbul cover node_modules/mocha/bin/_mocha" | ||
} | ||
"scripts": { | ||
"test": "istanbul cover node_modules/mocha/bin/_mocha" | ||
} | ||
} |
@@ -11,2 +11,4 @@ # Node.js Logging Support for Cloud Foundry | ||
#### Version 2.0 introduced loging without Winston and changed custom fields to be parsed and reported as strings regardless of original type. | ||
## Features | ||
@@ -55,3 +57,3 @@ | ||
#### With restify: | ||
``` | ||
```js | ||
var restify = require('restify'); | ||
@@ -69,3 +71,3 @@ var log = require('cf-nodejs-logging-support'); | ||
#### With nodejs http: | ||
``` | ||
```js | ||
var log = require("cf-nodejs-logging-support"); | ||
@@ -117,3 +119,3 @@ const http = require('http'); | ||
With custom fields added to custom_fields field. Keep in mind, that the last argument is handled as custom fields object, if it is an object. | ||
With custom fields added to custom_fields field. Keep in mind, that the last argument is handled as custom_fields object, if it is an object. | ||
```js | ||
@@ -132,3 +134,3 @@ logMessage("info", "Test data %j", {"field" :"value"}); | ||
### Winston Transport | ||
This logging tool can be used in conjunction with Winston. Example: | ||
This logging tool can be used in conjunction with Winston. Logging via Winston transport is limited to custom logs. Network activity can not be tracked automatically. Example: | ||
```js | ||
@@ -135,0 +137,0 @@ var express = require('express'); |
Sorry, the diff of this file is not supported yet
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
39470
13
497
0
175
9
+ Addedrequest@^2.81.0
+ Addedajv@6.12.6(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@1.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.1.5(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.15.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@6.5.3(transitive)
+ Addedrequest@2.88.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)
- Removedwinston@2.3.1
- Removedasync@1.0.0(transitive)
- Removedcolors@1.0.3(transitive)
- Removedcycle@1.0.3(transitive)
- Removedeyes@0.1.8(transitive)
- Removedstack-trace@0.0.10(transitive)
- Removedwinston@2.3.1(transitive)