Skip to content

Commit c8eb4e3

Browse files
committed
added convenience description to readme
1 parent d3242ca commit c8eb4e3

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ app.use(log.logNetwork);
4444

4545
app.get('/', function (req, res) {
4646
// Context bound custom message
47-
req.logMessage("info", "Hello World will be sent");
47+
req.logger.info("Hello World will be sent");
4848

4949
res.send('Hello World');
5050
});
5151
app.listen(3000);
5252

5353
// Formatted log message free of request context
54-
log.logMessage("info", "Server is listening on port %d", 3000);
54+
log.info("Server is listening on port %d", 3000);
5555
```
5656

5757
### Other Server Libraries
@@ -83,14 +83,14 @@ const server = http.createServer((req, res) => {
8383
log.logNetwork(req, res);
8484

8585
// Context bound custom message
86-
req.logMessage("info", "request bound information:", {
86+
req.logger.info("request bound information:", {
8787
"some": "info"
8888
});
8989
res.end('ok');
9090
});
9191
server.listen(3000);
9292
// Formatted log message free of request context
93-
log.logMessage("info", "Server is listening on port %d", 3000);
93+
log.info("Server is listening on port %d", 3000);
9494
```
9595

9696
### Custom Messages
@@ -132,6 +132,24 @@ logMessage("info", "Test data %j", {"field" :"value"}, {});
132132
// ... "msg":"Test data {\"field\": \"value\"}" ...
133133
```
134134

135+
### Convenience Methods
136+
137+
Instead of using logMessage(...), you could also use:
138+
139+
| Standard | Convenient |
140+
|------------------------------------------------|-----------------------------------|
141+
| ```log.logMessage("info",...)``` | ```log.info(...)``` |
142+
| ```req.logMessage("info",...)``` | ```req.logger.info(...)``` |
143+
| ```correlationObject.logMessage("info",...)``` | ```correlationObject.info(...)``` |
144+
145+
The convenience methods are currently only available for the common node.js logging levels:
146+
- error
147+
- warn
148+
- info
149+
- verbose
150+
- debug
151+
- silly
152+
135153
### Sensitive data redaction
136154
Version 3.0.0 and above implements a sensitive data redaction system, which deactivates the logging of sensitive fields. The field will contain 'redacted' instead of the original content.
137155

@@ -145,6 +163,7 @@ In order to activate normal logging for all or some of these fields, you have to
145163
| ```LOG_REMOTE_USER: true``` | activates the field remote_user |
146164
| ```LOG_REFERER: true``` | activates the field referer |
147165

166+
148167
This behavior matches with the corresponding mechanism in the [CF Java Logging Support](https://github.com/SAP/cf-java-logging-support/wiki/Overview#logging-sensitive-user-data) library.
149168

150169
### Dynamic log levels
@@ -266,7 +285,13 @@ Possibility to tailor logs to your needs, you can for example change the msg fie
266285
```js
267286
log.overrideNetworkField("msg", YOUR_CUSTOM_MSG);
268287
```
269-
This will replace the value of the previously empty msg field for network logs with YOUR_CUSTOM_MSG.
288+
This will replace the value of the previously not existing msg field for network logs with YOUR_CUSTOM_MSG.
289+
If the overridden field is already existing, it will be overridden by YOUR_CUSTOM_MSG for ALL subsequent network logs, until you
290+
remove the override with:
291+
```js
292+
log.overrideNetworkField("msg", null);
293+
```
294+
If you use this override feature in conjunction with a log parser, make sure you will not violate any parsing rules.
270295

271296

272297
## Sample Apps

cf-nodejs-logging-support-core/log-core.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ var logMessage = function () {
343343
logObject.type = logType;
344344

345345
if (customFields != null) {
346-
logObject.custom_fields = customFields.constructor == Array ? [] : {};
346+
logObject.custom_fields = {};
347347
for (var key in customFields) {
348348
if ((typeof customFields[key]) == "string") {
349349
logObject.custom_fields[key] = customFields[key];
@@ -450,7 +450,7 @@ var writeStaticFields = function (logObject) {
450450
var overrideField = function (field, value) {
451451
if (field != null && typeof field == "string") {
452452
if (value == undefined || value == null) {
453-
fixedValues[field] = null;
453+
fixedValues[field] = undefined;
454454
return true;
455455
} else {
456456
fixedValues[field] = value;

test/test-complete.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('Test Complete', function () {
4242
process.stdout.write = process.stdout.write.override(
4343
function (obj) {
4444
store = obj;
45-
this.superFunction(obj);
45+
//this.superFunction(obj);
4646
}
4747
);
4848
before(function () {
@@ -69,7 +69,6 @@ describe('Test Complete', function () {
6969
log.overrideNetworkField("msg","testmessage");
7070
log.logNetwork(req, res, () => {});
7171
res.end("ok");
72-
console.log(store);
7372
assert.deepEqual(JSON.parse(store),results.getLogMessage());
7473
});
7574
});

test/test-core.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,13 +729,13 @@ describe('Test log-core', function () {
729729
}));
730730
});
731731

732-
it("Test custom fields log output (array)", function () {
732+
it("Test custom fields log output (convert array to object)", function () {
733733
log("info", "Test", [
734734
1, "123", { "field": "values" }
735735
]);
736736

737737
logObject.msg.should.equal('Test');
738-
JSON.stringify(logObject.custom_fields).should.equal('["1","123","{\\"field\\":\\"values\\"}"]');
738+
JSON.stringify(logObject.custom_fields).should.equal('{"0":"1","1":"123","2":"{\\"field\\":\\"values\\"}"}');
739739
});
740740

741741
it("Test custom fields log type consistency (objects)", function () {

0 commit comments

Comments
 (0)