Format
2 minute read
The Format converter formats a string with specified variables.
Syntax: Format(formatString, values)
- formatString: the base string with placeholders for variables.
- values: an array of values to fill into the placeholders. The placeholders in the format string are mapped to the values in the array based on their order. This means that the first placeholder corresponds to the first element in the values array.
You can use the following format Specifiers in the formatString:
- %s: This specifier is used for strings. It replaces %s in the format string with the corresponding string value provided in the values array.
- %d: This specifier is used for integers. It replaces %d in the format string with the integer value provided in the values array, formatted as a decimal number.
- %f: Used for floating-point numbers. You can specify the precision (number of decimal places) as well, for example, %.2f for two decimal places.
- %x and %X: Used for hexadecimal representation of numbers. %x produces lowercase hex letters (e.g., a to f), and %X produces uppercase hex letters (e.g., A to F).
- %o: Used for octal representation of numbers.
- %%: Used to insert a literal % character.
Match the type of the variable with the appropriate specifier to avoid runtime errors or incorrect formatting.
Input
{
"_type": "log",
"attributes": {
"bytes_sent": 152305,
"device_name": "Router-X9000",
"session_duration": 2735,
"timestamp": "\"2024-12-09T10:26:47.487742422Z\""
},
"body": "High throughput observed. Device performance logged.",
"resource": {
"ed.conf.id": "123456789",
"ed.domain": "pipeline",
"ed.org.id": "987654321",
"ed.source.name": "__ed_dummy_test_input",
"ed.source.type": "memory_input",
"ed.tag": "loggen",
"host.ip": "10.0.0.1",
"host.name": "ED_TEST",
"location": "DataCenter-5",
"operator": "NetworkOperations",
"service.name": "ed-tester",
"src_type": "memory_input"
},
"timestamp": 1733740007487
}
Statement
set(attributes["formatted_device_report"], Format("Report - Device: %s; Operator: %s; Location: %s; Timestamp: %s; Details: Sent %d bytes over a session lasting %d seconds.", [attributes["device_name"], resource["operator"], resource["location"], attributes["timestamp"], attributes["bytes_sent"], attributes["session_duration"]]))
Output
{
"_type": "log",
"attributes": {
"bytes_sent": 152305,
"device_name": "Router-X9000",
"formatted_device_report": "Report - Device: Router-X9000; Operator: NetworkOperations; Location: DataCenter-5; Timestamp: \"2024-12-09T10:27:35.283863127Z\"; Details: Sent 152305 bytes over a session lasting 2735 seconds.",
"session_duration": 2735,
"timestamp": "\"2024-12-09T10:27:35.283863127Z\""
},
"body": "High throughput observed. Device performance logged.",
"resource": {
"ed.conf.id": "123456789",
"ed.domain": "pipeline",
"ed.org.id": "987654321",
"ed.source.name": "__ed_dummy_test_input",
"ed.source.type": "memory_input",
"ed.tag": "loggen",
"host.ip": "10.0.0.1",
"host.name": "ED_TEST",
"location": "DataCenter-5",
"operator": "NetworkOperations",
"service.name": "ed-tester",
"src_type": "memory_input"
},
"timestamp": 1733740055283
}
The statement created a formatted_device_report attribute that collates and formats various other fields into a new message.