Skip to content

Commit 625acac

Browse files
authored
Merge pull request #255 from SAP/impr-custom-fields-docs
Update documentation
2 parents b2bc10d + a14add6 commit 625acac

File tree

4 files changed

+69
-33
lines changed

4 files changed

+69
-33
lines changed

docs/configuration/custom-fields-format.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ nav_order: 4
66
permalink: /configuration/custom-fields-format
77
---
88

9-
# Custom fields format
9+
# Custom fields format and type conversion
1010

11-
As described in [Custom Fields](/cf-nodejs-logging-support/general-usage/custom-fields), the library will automatically detect which logging service your app is bound to and set the logging format accordingly.
12-
However, it is also possible to force the logging format by setting the property `customFieldsFormat`.
11+
12+
13+
As described in [Custom Fields](/cf-nodejs-logging-support/general-usage/custom-fields), the library supports different formats for logging custom fields to ensure compatibility with SAP logging services.
14+
When using user-provided services or running in environments without service bindings, you might want to explicitly set the custom fields format.
15+
Besides programmatic configuration, this can also be achieved via by adding the following settings to your configuration file.
1316

1417
Example:
1518

docs/configuration/fields.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Each field, i.e., its source, format and behavior is defined by a set required a
8787

8888
Read more on each available property below.
8989

90-
### Property: name (required)
90+
### Property: `name` (required)
9191

9292
* **Type:** `string`
9393
* **Description:** Assign field name.

docs/general-usage/04-custom-fields.md

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,70 +8,101 @@ permalink: /general-usage/custom-fields
88

99
# Custom Fields
1010

11-
You can use the custom field feature to add custom fields to your logs.
12-
11+
Custom fields allow you to enrich your log messages with additional context by attaching key-value pairs.
12+
This is particularly useful for adding metadata that can help with filtering, searching, and analyzing logs.
1313

1414
## Format and type transformation for SAP logging services
1515

16-
The library will automatically detect which logging service is bound to your app and will set the custom field format accordingly.
16+
While the library can be used independently of SAP logging services, it provides built-in support for logging custom fields compatible with [SAP Application Logging Service](https://help.sap.com/docs/application-logging-service) and [SAP Cloud Logging](https://help.sap.com/docs/cloud-logging).
17+
This includes proper formatting and type conversion of custom fields to ensure compatibility with the respective logging service:
18+
19+
- **SAP Application Logging**: Custom fields are formatted as key-value pairs within a special `#cf` field in the log message. The values are converted to strings to ensure compatibility with the service's field type requirements.
20+
- **SAP Cloud Logging**: Custom fields are added as top-level fields in the log message. By default, values are also converted to strings to prevent type mismatches during indexing. However, you can configure the library to retain the original types of custom field values if needed.
21+
22+
When deploying your application to SAP BTP Cloud Foundry, the library automatically detects logging service bindings and sets the custom field format accordingly.
23+
24+
### Overriding the custom fields format
25+
26+
There are cases where you might want to override the default behavior:
27+
- When using the library in an environment without a logging service binding, e.g., on Kubernetes.
28+
- When using a user-provided service where the logging service type cannot be detected correctly.
29+
- When you want to disable custom fields logging entirely.
1730

18-
For local testing purposes you can enforce a specific format like this:
31+
In such cases, you can explicitly set the desired custom fields format programmatically:
1932

2033
```js
34+
import { CustomFieldsFormat } from "@sap/cf-nodejs-logging-support";
35+
36+
log.setCustomFieldsFormat(CustomFieldsFormat.ApplicationLogging);
37+
// or
2138
log.setCustomFieldsFormat("application-logging");
22-
// possible values: "disabled", "all", "application-logging", "cloud-logging", "default"
2339
```
2440

25-
Alternatively, you can force the logging format by setting a configuration file as explained in [Custom Fields Format](/cf-nodejs-logging-support/configuration/custom-fields-format).
41+
Available formats are:
2642

27-
### SAP Application Logging
43+
| Format Constant | String Value | Description |
44+
|-----------------|--------------|-------------|
45+
| `CustomFieldsFormat.ApplicationLogging` | `application-logging` | Use this format to log custom fields compatible with SAP Application Logging Service. |
46+
| `CustomFieldsFormat.CloudLogging` | `cloud-logging` | Use this format to log custom fields compatible with SAP Cloud Logging. |
47+
| `CustomFieldsFormat.All` | `all` | Use this format to log custom fields compatible with both SAP Application Logging Service and SAP Cloud Logging. |
48+
| `CustomFieldsFormat.Disabled` | `disabled` | Disable custom fields logging. |
49+
| `CustomFieldsFormat.Default` | `default` | Same as `cloud-logging` format. |
2850

29-
In case you want to use this library with SAP Application Logging Service you need to register your custom fields.
30-
Please make sure to call the registration exactly once globally before logging any custom fields.
31-
Registered fields will be indexed based on the order given by the provided field array.
51+
Alternatively, you can force the logging format setting using a configuration file as explained in [Advanced Configuration](/cf-nodejs-logging-support/configuration).
3252

33-
Custom fields are converted and printed as string values independent of their original type.
53+
### Overriding the custom fields type conversion
54+
55+
By default, custom field values are converted to strings to avoid type mismatches during indexing.
56+
While this behavior cannot be changed for the custom fields format used for SAP Application Logging, it can be adjusted for SAP Cloud Logging as follows:
3457

3558
```js
36-
log.registerCustomFields(["field"]);
37-
info("Test data", {"field": 42});
38-
// ... "msg":"Test data"
39-
// ... "#cf": {"string": [{"k":"field","v":"42","i":0}]}...
59+
import { CustomFieldsTypeConversion } from "@sap/cf-nodejs-logging-support";
60+
61+
log.setCustomFieldsTypeConversion(CustomFieldsTypeConversion.Retain);
4062
```
4163

42-
### SAP Cloud Logging
64+
Available type conversion options are:
65+
| Type Conversion Constant | Description |
66+
|--------------------------|-------------|
67+
| `CustomFieldsTypeConversion.Stringify` | Convert all custom field values to strings. This is the default behavior. |
68+
| `CustomFieldsTypeConversion.Retain` | Retain the original types of custom field values. |
69+
70+
Alternatively, you can force the field type conversion setting using a configuration file as explained in [Advanced Configuration](/cf-nodejs-logging-support/configuration).
4371

44-
When using the library with SAP Cloud Logging, custom fields get added as top-level fields.
45-
Registering custom fields is not required in this case.
72+
Keep in mind that retaining original types may lead to indexing issues if the same custom field is logged with different types across log entries.
4673

47-
By default all custom field values get stringified, which can help to prevent field type mismatches when indexing to SAP Cloud Logging.
48-
However, if you want to retain the original type of your custom fields, you can change the type conversion configuration accordingly:
74+
### Registering custom fields for SAP Application Logging
75+
76+
When using SAP Application Logging Service, it is necessary to register custom fields before logging them.
77+
Please make sure to do the registration exactly once globally before logging any custom fields.
78+
Registered fields will be indexed based on the order given by the provided field array.
4979

5080
```js
51-
log.setCustomFieldsTypeConversion(CustomFieldsTypeConversion.Retain)
81+
log.registerCustomFields(["field"]);
5282
info("Test data", {"field": 42});
5383
// ... "msg":"Test data"
54-
// ... "field": 42
84+
// ... "#cf": {"string": [{"k":"field","v":"42","i":0}]}...
5585
```
5686

57-
## Adding custom fields
87+
## Logging custom fields
5888

59-
In addition to logging messages as described in [Message Logs](/cf-nodejs-logging-support/general-usage/message-logs) you can attach custom fields as an object of key-value pairs as last parameter:
89+
In addition to logging messages as described in [Message Logs](/cf-nodejs-logging-support/general-usage/message-logs), you can attach custom fields by passing an object of key-value pairs as the last parameter.
90+
This allows enriching log messages with additional context information.
6091

6192
```js
6293
logger.info("My log message", {"field-a" :"value"});
6394
```
6495

65-
Another way of adding custom fields to messages is to set them for (child) loggers in *global* or *request* context.
66-
In result messages will contain the custom fields specified to the logger instance in use.
96+
Another way to add custom fields is by setting them on (child) logger instances, either globally or within a request context.
97+
When you do this, all messages logged by that logger will automatically include the specified custom fields, providing consistent context information across your logs.
6798

6899
```js
69100
logger.setCustomFields({"field-a": "value"})
70101
logger.info("My log message");
71102
```
72103

73-
You can also set custom fields globally by calling the same function on the global `log` instance.
74-
All logs, including request logs and logs by child loggers, will now contain the specified custom fields and values.
104+
You can also define custom fields globally by invoking the same method on the global `log` instance.
105+
This ensures that all logs, including request logs and those emitted by child loggers, will automatically include the specified custom fields and their values.
75106

76107
```js
77108
log.setCustomFields({"field-b": "test"});

docs/index.markdown

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ nav_order: 1
99

1010
# CF Node.js Logging Support
1111

12-
This library provides a bundle of targeted logging services for node.js applications running on Cloud Foundry which serves two main purposes:
12+
This library provides a bundle of targeted logging services for Node.js applications running on Cloud Foundry which serves two main purposes:
1313
It provides means to emit *structured application log messages* and instrument parts of your application stack to *collect request metrics*.
1414

15+
The library is optimized for seamless integration with SAP BTP Cloud Foundry environment and its logging services [SAP Application Logging Service](https://help.sap.com/docs/application-logging-service) and [SAP Cloud Logging](https://help.sap.com/docs/cloud-logging).
16+
1517
To get started follow our [Getting Started](/cf-nodejs-logging-support/getting-started/) chapters to install the library and learn how to integrate it with supported server frameworks.
1618

1719
Head over to the [General Usage](/cf-nodejs-logging-support/general-usage) articles to find information about the main logging functions.

0 commit comments

Comments
 (0)