Node.js Logging Support for Cloud Foundry
Features
- Network logging (http requests) for CloudFoundry
- Custom message logging
- Logging levels
- Can be bound to Winston as transport
Installation
Add the following dependency to your package.json.
"dependencies": {
"cf-nodejs-logging-support": "git+https://github.com/SAP/cf-nodejs-logging-support"
}
Usage
Minimal Example
var express = require('express');
var log = require('cf-nodejs-logging-support');
var app = express();
log.setLoggingLevel("info");
app.use(log.logNetwork);
app.get('/', function (req, res) {
req.logMessage("info", "Hello World will be send");
res.send('Hello World');
});
app.listen(3000);
log.logMessage("info", "Server is listening on port %d", 3000);
Custom Messages
Use the logMessage(...) function to log custom messages with a given logging level. It is also possible to use the standard format placeholders equivalent to the util.format method.
Custom messages may be called on two objects:
- If you want to keep track of a request with custom messages, you want to call req.logMessage(...) so the correct correlation_id and request_id will be added to this log.
- If you want to write request independent custom messages, you want to call log.logMessage(...) so no further context tracking will be added.
Simple message
logMessage("info", "Hello World");
With addtional numeric value
logMessage("info", "Listening on port %d", 5000);
With additional string values
logMessage("info", "This %s a %s", "is", "test");
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.
logMessage("info", "Test data %j", {"field" :"value"});
With json object forced to be embedded in to the message (nothing will be added to custom_fields).
logMessage("info", "Test data %j", {"field" :"value"}, {});
Winston Transport
This logging tool can be used in conjunction with Winston. Example:
var express = require('express');
var log = require('cf-nodejs-logging-support');
var winston = require('winston');
var app = express();
var logger = new(winston.Logger)({
transports: [log.winstonTransport]
});
app.get('/', function (req, res) {
res.send('Hello World');
});
app.listen(3000);
logger.log("info", "Server is listening on port %d", 3000);
Sample Apps