You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/configuration/custom-fields-format.md
+6-3Lines changed: 6 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,10 +6,13 @@ nav_order: 4
6
6
permalink: /configuration/custom-fields-format
7
7
---
8
8
9
-
# Custom fields format
9
+
# Custom fields format and type conversion
10
10
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.
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.
13
13
14
14
## Format and type transformation for SAP logging services
15
15
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.
17
30
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:
// possible values: "disabled", "all", "application-logging", "cloud-logging", "default"
23
39
```
24
40
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:
26
42
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. |
|`CustomFieldsFormat.Default`|`default`| Same as `cloud-logging` format. |
28
50
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).
32
52
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:
|`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).
43
71
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.
46
73
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.
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.
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.
67
98
68
99
```js
69
100
logger.setCustomFields({"field-a":"value"})
70
101
logger.info("My log message");
71
102
```
72
103
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.
Copy file name to clipboardExpand all lines: docs/index.markdown
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,9 +9,11 @@ nav_order: 1
9
9
10
10
# CF Node.js Logging Support
11
11
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:
13
13
It provides means to emit *structured application log messages* and instrument parts of your application stack to *collect request metrics*.
14
14
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
+
15
17
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.
16
18
17
19
Head over to the [General Usage](/cf-nodejs-logging-support/general-usage) articles to find information about the main logging functions.
0 commit comments