Field processing plugins add, delete, modify, pack, expand, and extract fields.
Field processing example
This table details the data structure of a raw log stored in SLS. It compares using the field extraction plugin in anchor mode to not using a plugin. Using the field extraction plugin structures your data, simplifying subsequent queries.
|
Raw log |
Without plugin |
Anchor mode |
|
Content: "time:2022.09.12 20:55:36\t json:{\"key1\" : \"xx\", \"key2\": false, \"key3\":123.456, \"key4\" : { \"inner1\" : 1, \"inner2\" : false}}" |
Use anchor mode to extract field values, and set the field names to time, val_key1, val_key2, val_key3, value_key4_inner1, and value_key4_inner2.
|
Field processing plugins
SLS provides the following types of field processing plugins. Select one that meets your needs.
|
Plugin name |
Type |
Description |
|
Extract fields |
Extended |
Supports the following modes:
|
|
Add fields |
Extended |
Adds new fields to a log. |
|
Drop fields |
Extended |
Removes specified fields. |
|
Rename fields |
Extended |
Renames fields. |
|
Pack fields |
Extended |
Packs multiple fields into a single JSON object. |
|
Expand JSON fields |
Extended |
Expands a JSON string field into separate fields. |
|
Map field values |
Extended |
Replaces or transforms field values based on a mapping table. |
|
Replace strings |
Extended |
Performs full-text replacement, regular expression-based replacement, or escape character removal in text logs. |
Entry point
To use a Logtail plugin for log processing, add it when you create or modify a Logtail configuration. For more information, see Overview.
Limitations
-
Text logs and container standard output support only form-based configuration, while all other input sources support only JSON-based configuration.
-
The following limitations apply when extracting fields in regex mode.
The Go regular expression engine, based on RE2, has the following limitations compared to the PCRE engine:
-
Differences in named group syntax
Go uses the
(?P<name>...)syntax instead of the PCRE syntax(?<name>...). -
Unsupported regular expression patterns
-
Assertion:
(?=...),(?!...),(?<=...), and(?<!...). -
Conditional expression:
(?(condition)true|false). -
Recursive matching:
(?R)and(?0). -
Subroutine reference:
(?&name)and(?P>name). -
Atomic group:
(?>...).
-
When debugging regular expressions with tools such as Regex101, avoid the unsupported patterns listed above, as the plugin cannot process logs that use them.
-
Field extraction plugin
Extracts a log field using regex mode, anchor mode, CSV mode, single-character delimiter mode, multi-character delimiter mode, key-value pair mode, or Grok mode.
Regex mode
Anchor mode
CSV mode
Single-character delimiter mode
Multi-character delimiter mode
Key-value pair mode
Grok mode
Add fields plugin
Use the processor_add_fields plugin to add log fields. This topic describes the plugin's parameters and provides configuration examples.
Configuration
The processor_add_fields plugin is supported in Logtail 0.16.28 and later.
Form
-
Parameters
Set Processor Type to Add Field. The following table describes the parameters.
Parameter
Description
Add Field
The key-value pairs to add as new log fields.
Ignore New Fields with Same Name
If enabled, prevents a new field from overwriting an existing field with the same name.
-
Configuration example
This example shows how to add the
aaa2and aaa3 fields.-
Raw log
"aaa1":"value1" -
Logtail plugin configuration: Add two fields:
aaa2with the valuevalue2, andaaa3with the valuevalue3. -
Result
"aaa1":"value1" "aaa2":"value2" "aaa3":"value3"
-
JSON
-
Parameters
Set
typetoprocessor_add_fields. The following table describes the parameters in thedetailobject.Parameter
Type
Required
Description
Fields
Map
Yes
The log fields to add, specified as a map of key-value pairs.
IgnoreIfExist
Boolean
No
Specifies the behavior when a field to be added has the same name as an existing field.
-
true: The new field is ignored.
-
false (default): The existing field is overwritten by the new field.
-
-
Configuration example
This example shows how to add the aaa2 and aaa3 fields.
-
Raw log
"aaa1":"value1" -
Logtail plugin configuration
{ "processors":[ { "type":"processor_add_fields", "detail": { "Fields": { "aaa2": "value2", "aaa3": "value3" } } } ] } -
Result
"aaa1":"value1" "aaa2":"value2" "aaa3":"value3"
-
Drop fields plugin
The processor_drop plugin drops specified log fields. This topic covers the plugin's parameters and provides configuration examples.
Configuration
The processor_drop plugin is supported in Logtail 0.16.28 and later.
Form
-
Parameters
Set Processor Type to Drop Field.
Parameter
Description
Drop Field
The log fields to drop. You can specify multiple fields.
-
Configuration example
To drop the
aaa1andaaa2fields from a log, configure the plugin as follows:-
Raw log
"aaa1":"value1" "aaa2":"value2" "aaa3":"value3" -
Logtail plugin configuration: Drop the
aaa1andaaa2fields. -
Result
"aaa3":"value3"
-
JSON
-
Parameters
Set type to processor_drop. The following table describes the detail parameter.
Parameter
Type
Required
Description
DropKeys
string array
Yes
The keys of the log fields to drop. You can specify multiple keys.
-
Configuration example
To drop the aaa1 and aaa2 fields from a log, configure the plugin as follows:
-
Raw log
"aaa1":"value1" "aaa2":"value2" "aaa3":"value3" -
Logtail plugin configuration
{ "processors":[ { "type":"processor_drop", "detail": { "DropKeys": ["aaa1","aaa2"] } } ] } -
Result
"aaa3":"value3"
-
Rename fields plugin
You can use the processor_rename plugin to rename fields. This topic describes the parameters and configuration examples of the processor_rename plugin.
Configuration
The processor_rename plugin requires Logtail 0.16.28 or later.
Console
-
Parameters
Set Processor Type to Rename Fields and configure the parameters described in the following table.
Parameter
Description
Original Field
The original field to rename.
New Field
The new name for the field.
Report Original Field Missing Error
Specifies whether to report an error if the specified original field is not found in the log.
-
Example
This example renames the
aaa1field to bbb1 and the aaa2 field to bbb2.-
Raw log
"aaa1":"value1" "aaa2":"value2" "aaa3":"value3" -
Logtail plugin configuration: Rename
aaa1tobbb1andaaa2tobbb2. -
Result
"bbb1":"value1" "bbb2":"value2" "aaa3":"value3"
-
JSON
-
Parameters
Set
typetoprocessor_renameand configure the parameters for thedetailobject as described in the following table.Parameter
Type
Required
Description
NoKeyErrorBoolean
No
Specifies whether to report an error if the specified original field is not found in the log.
-
true: Reports an error.
-
false (default): Does not report an error.
SourceKeysString array
Yes
The original fields to rename.
DestKeysString array
Yes
The new names for the fields.
-
-
Example
This example renames the
aaa1field tobbb1and theaaa2field tobbb2.-
Raw log
"aaa1":"value1" "aaa2":"value2" "aaa3":"value3" -
Logtail plugin configuration
{ "processors":[ { "type":"processor_rename", "detail": { "SourceKeys": ["aaa1","aaa2"], "DestKeys": ["bbb1","bbb2"], "NoKeyError": true } } ] } -
Result
"bbb1":"value1" "bbb2":"value2" "aaa3":"value3"
-
Pack fields plugin
Use the processor_packjson plugin to pack one or more source fields into a destination field as a JSON object. This topic describes the parameters of the processor_packjson plugin and provides configuration examples.
Configuration
The processor_packjson plugin is supported in Logtail 0.16.28 and later.
Console
-
Parameters
Set Processor Type to Encapsulate Fields. The following table describes the parameters.
Parameter
Description
Original Field
The source fields to pack.
New Field
The destination field for the packed JSON object.
Retain Original Field
If you select this option, the source fields are retained in the parsed log.
Report Original Field Missing Error
If you select this option, the plugin reports an error if a specified source field is not found in the raw log.
-
Example
This example shows how to pack the a and b fields into a destination field named d_key.
-
Raw log
"a":"1" "b":"2" -
Logtail plugin configuration: Set Source Field to
aandb, and set Destination Field tod_key. -
Result
"a":"1" "b":"2" "d_key":"{\"a\":\"1\",\"b\":\"2\"}"
-
JSON
-
Parameters
Set type to processor_packjson. The following table describes the parameters in the detail object.
Parameter
Type
Required
Description
SourceKeys
String array
Yes
The source fields to pack.
DestKey
String
No
The destination field for the packed JSON object.
KeepSource
Boolean
No
Specifies whether to retain the source fields in the parsed log.
-
true (default): The source fields are retained.
-
false: The source fields are removed after packing.
AlarmIfIncomplete
Boolean
No
Specifies whether to report an error if a specified source field is not found in the raw log.
-
true (default): An error is reported.
-
false: No error is reported.
-
-
Example
This example shows how to pack the a and b fields into a destination field named d_key.
-
Raw log
"a":"1" "b":"2" -
Logtail plugin configuration
{ "processors":[ { "type":"processor_packjson", "detail": { "SourceKeys": ["a","b"], "DestKey":"d_key", "KeepSource":true, "AlarmIfIncomplete":true } } ] } -
Result
"a":"1" "b":"2" "d_key":"{\"a\":\"1\",\"b\":\"2\"}"
-
Expand JSON field plugin
Use the processor_json plugin to expand a JSON field. This topic describes the parameters for the processor_json plugin and provides configuration examples.
Configuration
The processor_json plugin is supported in Logtail 0.16.28 and later.
Console
-
Parameters
Set Processor Type to Expand JSON Field. The following table describes the parameters.
Parameter
Description
Original Field
The name of the source field to expand.
JSON Expansion Depth
The maximum depth to expand the JSON object. The default value is 0, which means there is no depth limit. A value of 1 expands only the top-level fields.
Character to Concatenate Expanded Keys
The character used to connect nested keys. The default value is an underscore (_).
Name Prefix of Expanded Keys
A prefix for the names of expanded fields.
Expand Array
Specifies whether to expand arrays. This parameter is supported in Logtail 1.8.0 and later.
Retain Original Field
Specifies whether to keep the source field in the log after parsing.
Report Original Field Missing Error
Specifies whether to report an error if the specified source field is not found in the raw log.
Use Name of Original Field as Name Prefix of Expanded Keys
Specifies whether to use the name of the source field as a prefix for all expanded field names.
Retain Raw Logs If Parsing Fails
Specifies whether to keep the raw log if parsing fails.
-
Example
This example expands the
s_keyfield, usingjas a custom prefix and the source field names_keyas an additional prefix for the expanded field names. The following is an example configuration:-
Raw log (file path read by Logtail)
{"s_key":"{\"k1\":{\"k2\":{\"k3\":{\"k4\":{\"k51\":\"51\",\"k52\":\"52\"},\"k41\":\"41\"}}}}"} -
Logtail plugin configuration
Set SourceKey to
s_key, enable NoKeyError, set ExpandDepth to0(unlimited), set ExpandConnector to a hyphen (-), set Prefix toj, enable UseSourceKeyAsPrefix, and disable KeepSource. -
Result
The expanded log contains the following fields:
j_s_key-k1-k2-k3-k4-k51with the value51,j_s_key-k1-k2-k3-k4-k52with the value52, andj_s_key-k1-k2-k3-k41with the value41. The sources_keyfield is removed.
-
JSON
-
Parameters
Set
typetoprocessor_json. The following table describes the parameters in thedetailobject.Parameter
Type
Required
Description
SourceKey
String
Yes
The name of the source field to expand.
NoKeyError
Boolean
No
Specifies whether to report an error if the source field is not found in the raw log.
-
true (default): An error is reported.
-
false: No error is reported.
ExpandDepth
Int
No
The maximum depth to expand the JSON object. The default value is 0, which means there is no depth limit. A value of 1 expands only the top-level fields.
ExpandConnector
String
No
The character used to connect nested keys. The default value is an underscore (_).
Prefix
String
No
The prefix to add to the names of the new fields after expansion.
KeepSource
Boolean
No
Specifies whether to keep the source field in the log after parsing.
-
true (default): The source field is kept.
-
false: The source field is not kept.
UseSourceKeyAsPrefix
Boolean
No
Specifies whether to use the source field name as a prefix for all expanded field names.
KeepSourceIfParseError
Boolean
No
Specifies whether to keep the raw log if parsing fails.
-
true (default): The raw log is kept.
-
false: The raw log is not kept.
ExpandArray
Boolean
No
Specifies whether to expand arrays. This parameter is supported in Logtail 1.8.0 and later.
-
false (default): Arrays are not expanded.
-
true: Expand arrays. For example,
{"k":["1","2"]}is expanded to{"k[0]":"1","k[1]":"2"}.
-
-
Example
This example expands the
s_keyfield, usingjas a custom prefix and the source field names_keyas an additional prefix for the expanded field names. The following is an example configuration:-
Raw log (file path read by Logtail)
{"s_key":"{\"k1\":{\"k2\":{\"k3\":{\"k4\":{\"k51\":\"51\",\"k52\":\"52\"},\"k41\":\"41\"}}}}"} -
Logtail plugin configuration
{ "processors":[ { "type":"processor_json", "detail": { "SourceKey": "s_key", "NoKeyError":true, "ExpandDepth":0, "ExpandConnector":"-", "Prefix":"j", "KeepSource": false, "UseSourceKeyAsPrefix": true } } ] } -
Result
The expanded log contains the following fields:
j_s_key-k1-k2-k3-k4-k51with the value51,j_s_key-k1-k2-k3-k4-k52with the value52, andj_s_key-k1-k2-k3-k41with the value41. The sources_keyfield is removed.
-
Field value mapping plugin
The processor_dict_map plugin maps field values. This topic describes the plugin's parameters and provides configuration examples.
Configuration
Form
Set processor type to Field Value Mapping. The following table describes the parameters.
|
Parameter |
Description |
|
Original Field |
The name of the source field. |
|
New Field |
The name of the mapped field. |
|
Mapping Dictionary |
A dictionary that maps source values to target values. For small dictionaries, you can configure mappings directly in this field instead of using a local dictionary file. Important
If you set Local Dictionary, the Mapping Dictionary configuration is ignored. |
|
Local Dictionary |
A CSV file that contains the mapping dictionary. In the file, fields must be enclosed in double quotation marks (") and separated by commas (,). |
|
advanced parameters>process missing source field |
When selected, if a raw log is missing the source field, the plugin fills the mapped field with the value from Value to Fill New Field. |
|
advanced parameters>Maximum Mapping Dictionary Size |
The maximum number of rules allowed in the mapping dictionary. The default is 1,000. Decrease this value to limit the plugin's server memory usage. |
|
advanced parameters>Method to Process Raw Log |
Specifies what to do when the mapped field already exists in a raw log.
|
JSON
Set type to processor_dict_map. The following table describes the parameters of detail.
|
Parameter |
Type |
Required |
Description |
|
SourceKey |
String |
Yes |
The name of the source field. |
|
MapDict |
Map |
No |
A dictionary that maps source values to target values. For small dictionaries, you can configure mappings directly here instead of using the DictFilePath parameter. Important
If you set the DictFilePath parameter, the MapDict parameter is ignored. |
|
DictFilePath |
String |
No |
The path to a CSV file that contains the mapping dictionary. In the file, fields must be enclosed in double quotation marks (") and separated by commas (,). |
|
DestKey |
String |
No |
The name of the mapped field. |
|
HandleMissing |
Boolean |
No |
Specifies whether to take action if the source field is missing from a raw log.
|
|
Missing |
String |
No |
The value for the mapped field when the source field is missing. The default is This parameter takes effect when HandleMissing is set to true. |
|
MaxDictSize |
Int |
No |
The maximum number of rules allowed in the mapping dictionary. The default is 1,000. Decrease this value to limit the plugin's server memory usage. |
|
Mode |
String |
No |
Specifies what to do when the mapped field (specified by DestKey) already exists in a raw log.
|
String replacement
Use the processor_string_replace plugin to perform full-text or regex-based replacement, or to remove escape characters from raw logs.
Configuration
The processor_string_replace plugin is supported in Logtail 1.6.0 and later.
Form
Set Processor Type to String Replacement. The following table describes the parameters.
|
Parameter |
Description |
|
Original Field |
The name of the source field. |
|
Match Mode |
The method used to find content for replacement. Valid values:
|
|
Matched Content |
The pattern or string to match.
|
|
Replaced By |
The string to use for replacement.
|
|
New Field |
The field to store the result. If not specified, the source field is overwritten. |
JSON
Set type to processor_string_replace. The following table describes the parameters within the detail object.
|
Parameter |
Type |
Required |
Description |
|
|
String |
Yes |
The name of the source field. |
|
|
String |
Yes |
The replacement method. Valid values:
|
|
|
String |
No |
The pattern or string to match.
|
|
|
String |
No |
The string to use for replacement. Defaults to an empty string ("").
|
|
|
String |
No |
The field to store the result. If this parameter is not specified, the source field is overwritten. |
Configuration examples
Replacing content using a string match
This example shows how to use a string match to replace Error: in the content field with an empty string.
Form
-
Raw log:
"content": "2023-05-20 10:01:23 Error: Unable to connect to database." -
Logtail plugin configuration: Set match mode to String Match, set source field to
content, set match content toError:, and leave replacement content empty. -
Result:
"content": "2023-05-20 10:01:23 Unable to connect to database."
JSON
-
Raw log:
"content": "2023-05-20 10:01:23 Error: Unable to connect to database." -
Logtail plugin configuration:
{ "processors":[ { "type":"processor_string_replace", "detail": { "SourceKey": "content", "Method": "const", "Match": "Error: ", "ReplaceString": "" } } ] } -
Result:
"content": "2023-05-20 10:01:23 Unable to connect to database."
Replacing content using a regular expression
This example shows how to use a regular expression to replace strings in the content field that match \\u\w+\[\d{1,3};*\d{1,3}m|N/A with an empty string.
Form
-
Raw log:
"content": "2022-09-16 09:03:31.013 \u001b[32mINFO \u001b[0;39m \u001b[34m[TID: N/A]\u001b[0;39m [\u001b[35mThread-30\u001b[0;39m] \u001b[36mc.s.govern.polygonsync.job.BlockTask\u001b[0;39m : Block collection------end------\r" -
Logtail plugin configuration: Set match mode to Regex Match, set source field to
content, set match content to\\u\w+\[\d{1,3};*\d{1,3}m|N/A, and leave replacement content empty. -
Result:
"content": "2022-09-16 09:03:31.013 INFO [TID: ] [Thread-30] c.s.govern.polygonsync.job.BlockTask : Block collection------end------\r"
JSON
-
Raw log:
"content": "2022-09-16 09:03:31.013 \u001b[32mINFO \u001b[0;39m \u001b[34m[TID: N/A]\u001b[0;39m [\u001b[35mThread-30\u001b[0;39m] \u001b[36mc.s.govern.polygonsync.job.BlockTask\u001b[0;39m : Block collection------end------\r" -
Logtail plugin configuration:
{ "processors":[ { "type":"processor_string_replace", "detail": { "SourceKey": "content", "Method": "regex", "Match": "\\\\u\\w+\\[\\d{1,3};*\\d{1,3}m|N/A", "ReplaceString": "" } } ] } -
Result:
"content": "2022-09-16 09:03:31.013 INFO [TID: ] [Thread-30] c.s.govern.polygonsync.job.BlockTask : Block collection------end------\r"
Replacing content using capture groups
This example uses a regular expression capture group to replace 16 in the content field with */24 and store the result in a new field named new_ip.
When using capture groups for replacement, the replacement string cannot contain {}. You must use formats such as $1 and $2.
Form
-
Raw log:
"content": "10.10.239.16" -
Logtail plugin configuration: Set match mode to Regex Match, set source field to
content, set match content to(\d.*\.)\d+, set replacement content to$1*/24, and set destination field tonew_ip. -
Result:
"content": "10.10.239.16", "new_ip": "10.10.239.*/24"
JSON
-
Raw log:
"content": "10.10.239.16" -
Logtail plugin configuration:
{ "processors":[ { "type":"processor_string_replace", "detail": { "SourceKey": "content", "Method": "regex", "Match": "(\\d.*\\.)\\d+", "ReplaceString": "$1*/24", "DestKey": "new_ip" } } ] } -
Result:
"content": "10.10.239.16", "new_ip": "10.10.239.*/24"
Removing escape characters
Form
-
Raw log:
"content": "{\\x22UNAME\\x22:\\x22\\x22,\\x22GID\\x22:\\x22\\x22,\\x22PAID\\x22:\\x22\\x22,\\x22UUID\\x22:\\x22\\x22,\\x22STARTTIME\\x22:\\x22\\x22,\\x22ENDTIME\\x22:\\x22\\x22,\\x22UID\\x22:\\x222154212790\\x22,\\x22page_num\\x22:1,\\x22page_size\\x22:10}" -
Logtail plugin configuration: Set match mode to Remove Escape Characters, and set source field to
content. -
Result:
"content": "{\"UNAME\":\"\",\"GID\":\"\",\"PAID\":\"\",\"UUID\":\"\",\"STARTTIME\":\"\",\"ENDTIME\":\"\",\"UID\":\"2154212790\",\"page_num\":1,\"page_size\":10}"
JSON
-
Raw log:
"content": "{\\x22UNAME\\x22:\\x22\\x22,\\x22GID\\x22:\\x22\\x22,\\x22PAID\\x22:\\x22\\x22,\\x22UUID\\x22:\\x22\\x22,\\x22STARTTIME\\x22:\\x22\\x22,\\x22ENDTIME\\x22:\\x22\\x22,\\x22UID\\x22:\\x222154212790\\x22,\\x22page_num\\x22:1,\\x22page_size\\x22:10}" -
Logtail plugin configuration:
{ "processors":[ { "type":"processor_string_replace", "detail": { "SourceKey": "content", "Method": "unquote" } } ] } -
Result:
"content": "{\"UNAME\":\"\",\"GID\":\"\",\"PAID\":\"\",\"UUID\":\"\",\"STARTTIME\":\"\",\"ENDTIME\":\"\",\"UID\":\"2154212790\",\"page_num\":1,\"page_size\":10}"
Related documentation
-
Configure Logtail pipelines using the API:
-
Configure processing plugins in the console:
-
Collect cluster container logs (standard output and files) using a Kubernetes CRD