escaped_utf8 decoder not work
Bug Report
Describe the bug I want to use fluent-bit to archive the service logs in my kubernetes to AWS S3 bucket, but the content of the log file I downloaded from s3 has spaces and garbled symbols, and it still doesn’t work after adding parse decoder effect.
To Reproduce
- Rubular link if applicable:
- Example log message if applicable:
{"date":"2021-12-21T02:23:04.393564Z","log":"\u001b[90m\u001b[4mquery:\u001b[24m\u001b[39m \u001b[94mSELECT\u001b[39m \"UserJob\".\"member_position_id\" \u001b[94mAS\u001b[39m \"UserJob_member_position_id\", \"UserJob\".\"member_id\" \u001b[94mAS\u001b[39m \"UserJob_member_id\", \"UserJob\".\"pos_id\" \u001b[94mAS\u001b[39m \"UserJob_pos_id\" \u001b[94mFROM\u001b[39m \"TestINetAdmin\".\"dbo\".\"member_position\" \"UserJob\" \u001b[94mWHERE\u001b[39m \"UserJob\".\"member_id\" = \u001b[32m182\u001b[39m\r\n","stream":"stdout","time":"2021-12-21T02:23:04.393564472Z"}
- Steps to reproduce the problem:
Expected behavior
The log I want should be like this:
query: SELECT "TaxItem"."id" AS "TaxItem_id", "TaxItem"."name" AS "TaxItem_name", "TaxItem"."code" AS "TaxItem_code" FROM "dbo"."tax_item" "TaxItem" WHERE "TaxItem"."code" = 'shipping' ORDER BY (SELECT NULL) OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
Screenshots
My configmap

Your Environment
- Version used: Fluent Bit v1.8.11
- Environment name and version (e.g. Kubernetes? What version?): aws-eks V1.18
Additional context
It would help to simplify this a bit and include the config as text to make it easier to reproduce - otherwise we have to type it all out from the screenshot which might introduce a typo.
I think what you're saying is that you have a log like this:
{"date":"2021-12-21T02:23:04.393564Z","log":"\u001b[90m\u001b[4mquery:\u001b[24m\u001b[39m \u001b[94mSELECT\u001b[39m \"UserJob\".\"member_position_id\" \u001b[94mAS\u001b[39m \"UserJob_member_position_id\", \"UserJob\".\"member_id\" \u001b[94mAS\u001b[39m \"UserJob_member_id\", \"UserJob\".\"pos_id\" \u001b[94mAS\u001b[39m \"UserJob_pos_id\" \u001b[94mFROM\u001b[39m \"TestINetAdmin\".\"dbo\".\"member_position\" \"UserJob\" \u001b[94mWHERE\u001b[39m \"UserJob\".\"member_id\" = \u001b[32m182\u001b[39m\r\n","stream":"stdout","time":"2021-12-21T02:23:04.393564472Z"}
You have a tail input with the default docker parser (I think - it looks like it but want to confirm?) plus some decoding defined like this:
Name docker
Format json
Time_Key time
Time_Format %Y-%m-%dT%H:%M:%S.%L
Time_Keep On
Decode_Field_As_Escaped_UTF8 log
I'll add a stdout output but what output are you seeing? I would add a stdout output to see what the output is from Fluent Bit directly without Elasticsearch in the way.
Sorry, I didn’t think well, this is my configmap content. ` apiVersion: v1 kind: ConfigMap metadata: name: fluent-bit-config namespace: logging labels: k8s-app: fluent-bit data:
Configuration files: server, input, filters and output
======================================================
fluent-bit.conf: | [SERVICE] Flush 1 Log_Level info Daemon off Parsers_File parsers.conf HTTP_Server On HTTP_Listen 0.0.0.0 HTTP_Port 2020
@INCLUDE input-kubernetes.conf
@INCLUDE filter-kubernetes.conf
@INCLUDE output-elasticsearch.conf
input-kubernetes.conf: |
[INPUT]
Name tail
Tag kube.<namespace_name>.<pod_name>.<container_name>
Tag_Regex (?<pod_name>a-z0-9?(.a-z0-9?))(?<namespace_name>[^]+)_(?<container_name>.+)-
Path /var/log/containers/export-staging.log,/var/log/containers/export-dev.log,/var/log/containers/export-production*.log
Parser docker
DB /var/log/flb_kube.db
Mem_Buf_Limit 5MB
Skip_Long_Lines On
Refresh_Interval 10
filter-kubernetes.conf: | [FILTER] Name kubernetes Match kube.<namespace_name>.<pod_name>.<container_name> Kube_URL https://kubernetes.default.svc:443 Kube_CA_File /var/run/secrets/kubernetes.io/serviceaccount/ca.crt Kube_Token_File /var/run/secrets/kubernetes.io/serviceaccount/token Kube_Tag_Prefix (?<pod_name>a-z0-9?(.a-z0-9?)*)(?<namespace_name>[^]+)_(?<container_name>.+)- Merge_Log On Merge_Log_Key log_processed K8S-Logging.Parser On K8S-Logging.Exclude Off
output-elasticsearch.conf: |
[OUTPUT]
Name s3
Match *
bucket ftl-logs-eks-oregon
region us-west-2
store_dir /home
total_file_size 1M
upload_timeout 1m
s3_key_format /$TAG[1]/%Y/%m/%d/$TAG[2]/$TAG[3]
parsers.conf: |
[PARSER]
Name apache
Format regex
Regex ^(?[^ ]) (?
[PARSER]
Name apache2
Format regex
Regex ^(?<host>[^ ]*) [^ ]* (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)")?$
Time_Key time
Time_Format %d/%b/%Y:%H:%M:%S %z
[PARSER]
Name apache_error
Format regex
Regex ^\[[^ ]* (?<time>[^\]]*)\] \[(?<level>[^\]]*)\](?: \[pid (?<pid>[^\]]*)\])?( \[client (?<client>[^\]]*)\])? (?<message>.*)$
[PARSER]
Name nginx
Format regex
Regex ^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^\"]*?)(?: +\S*)?)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)")?$
Time_Key time
Time_Format %d/%b/%Y:%H:%M:%S %z
[PARSER]
Name json
Format json
Time_Key time
Time_Format %d/%b/%Y:%H:%M:%S %z
[PARSER]
Name docker
Format json
Time_Key time
Time_Format %Y-%m-%dT%H:%M:%S.%L
Time_Keep On
Decode_Field_as escaped_utf8 log
[PARSER]
# http://rubular.com/r/tjUt3Awgg4
Name cri
Format regex
Regex ^(?<time>[^ ]+) (?<stream>stdout|stderr) (?<logtag>[^ ]*) (?<message>.*)$
Time_Key time
Time_Format %Y-%m-%dT%H:%M:%S.%L%z
[PARSER]
Name syslog
Format regex
Regex ^\<(?<pri>[0-9]+)\>(?<time>[^ ]* {1,2}[^ ]* [^ ]*) (?<host>[^ ]*) (?<ident>[a-zA-Z0-9_\/\.\-]*)(?:\[(?<pid>[0-9]+)\])?(?:[^\:]*\:)? *(?<message>.*)$
Time_Key time
Time_Format %b %d %H:%M:%S
`
这将有助于简化这一点并将配置包含为文本以使其更容易重现 - 否则我们必须从屏幕截图中全部输入,这可能会引入错字。
我想你说的是你有一个这样的日志:
{"date":"2021-12-21T02:23:04.393564Z","log":"\u001b[90m\u001b[4mquery:\u001b[24m\u001b[39m \u001b[94mSELECT\u001b[39m \"UserJob\".\"member_position_id\" \u001b[94mAS\u001b[39m \"UserJob_member_position_id\", \"UserJob\".\"member_id\" \u001b[94mAS\u001b[39m \"UserJob_member_id\", \"UserJob\".\"pos_id\" \u001b[94mAS\u001b[39m \"UserJob_pos_id\" \u001b[94mFROM\u001b[39m \"TestINetAdmin\".\"dbo\".\"member_position\" \"UserJob\" \u001b[94mWHERE\u001b[39m \"UserJob\".\"member_id\" = \u001b[32m182\u001b[39m\r\n","stream":"stdout","time":"2021-12-21T02:23:04.393564472Z"}您有一个
tail默认输入dockerparser(我认为 - 它看起来像但想确认?)加上一些这样定义的解码:Name docker Format json Time_Key time Time_Format %Y-%m-%dT%H:%M:%S.%L Time_Keep On Decode_Field_As_Escaped_UTF8 log我会添加一个
stdout输出,但你看到了什么输出?我会添加一个stdout输出,以直接查看 Fluent Bit 的输出,而无需 Elasticsearch。 I did two tests in the local environment according to the method you said.
First,
echo test log to /root/test.log
while true; do echo '{"date":"2021-12-22T02:48:25.529276Z","log":"\u001b[90m\u001b[4mquery:\u001b[24m\u001b[39m \u001b[94mSELECT\u001b[39m \"TABLE_CATALOG\", \"TABLE_SCHEMA\", \"COLUMN_NAME\", \"TABLE_NAME\" \u001b[94mFROM\u001b[39m \"casbin\".\"INFORMATION_SCHEMA\".\"COLUMNS\" \u001b[94mWHERE\u001b[39m COLUMNPROPERTY(object_id(\"TABLE_CATALOG\" + \u001b[37m'.'\u001b[39m + \"TABLE_SCHEMA\" + \u001b[37m'.'\u001b[39m + \"TABLE_NAME\"), \"COLUMN_NAME\", \u001b[37m'IsIdentity'\u001b[39m) = \u001b[32m1\u001b[39m \u001b[94mAND\u001b[39m \"TABLE_SCHEMA\" \u001b[94mIN\u001b[39m (\u001b[37m'dbo'\u001b[39m, \u001b[37m'dbo'\u001b[39m)\r\n","stream":"stdout","time":"2021-12-22T02:48:25.529276271Z"}' >> /root/test.log; done
docker parser config
[PARSER]
Name docker
Format json
Time_Key time
Time_Format %Y-%m-%dT%H:%M:%S.%L
Time_Keep On
# --
# Since Fluent Bit v1.2, if you are parsing Docker logs and using
# the Kubernetes filter, it's not longer required to decode the
# 'log' key.
#
# Command | Decoder | Field | Optional Action
# =============|==================|=================
Decode_Field_As json log
input and output
[INPUT]
name tail
path /root/test.log
parser docker
# Read interval (sec) Default: 1
[OUTPUT]
name stdout
match *
result: [2528] tail.0: [1640141305.529276270, {"date"=>"2021-12-22T02:48:25.529276Z", "log"=>"query: SELECT "TABLE_CATALOG", "TABLE_SCHEMA", "COLUMN_NAME", "TABLE_NAME" FROM "casbin"."INFORMATION_SCHEMA"."COLUMNS" WHERE COLUMNPROPERTY(object_id("TABLE_CATALOG" + . + "TABLE_SCHEMA" + . + "TABLE_NAME"), "COLUMN_NAME", IsIdentity) = 1 AND "TABLE_SCHEMA" IN (dbo, dbo) ", "stream"=>"stdout", "time"=>"2021-12-22T02:48:25.529276271Z"}]
Test here everything is normal
Second Use the same analysis as before, only change the output and send it to s3
[INPUT]
name tail
path /root/test.log
parser docker
# Read interval (sec) Default: 1
[OUTPUT]
Name s3
Match *
bucket ftl-logs-eks-oregon
region us-west-2
total_file_size 1M
upload_timeout 1m
result: {"date":"2021-12-22T02:48:25.529276Z","log":"\u001b[90m\u001b[4mquery:\u001b[24m\u001b[39m \u001b[94mSELECT\u001b[39m "TABLE_CATALOG", "TABLE_SCHEMA", "COLUMN_NAME", "TABLE_NAME" \u001b[94mFROM\u001b[39m "casbin"."INFORMATION_SCHEMA"."COLUMNS" \u001b[94mWHERE\u001b[39m COLUMNPROPERTY(object_id("TABLE_CATALOG" + \u001b[37m.\u001b[39m + "TABLE_SCHEMA" + \u001b[37m.\u001b[39m + "TABLE_NAME"), "COLUMN_NAME", \u001b[37mIsIdentity\u001b[39m) = \u001b[32m1\u001b[39m \u001b[94mAND\u001b[39m "TABLE_SCHEMA" \u001b[94mIN\u001b[39m (\u001b[37mdbo\u001b[39m, \u001b[37mdbo\u001b[39m)\r\n","stream":"stdout","time":"2021-12-22T02:48:25.529276271Z"}
I don't know how to solve this problem
@chenFe-1 Hi, have you found a solution for this problem?
@chenFe-1 Hi, have you found a solution for this problem?
The fluentbit official docker parser package parses logs according to the docker log output style, overwriting the previously garbled format, resulting in ansi style encoding in the log stream.Abandoning the use of docker parser solves this problem
I also have this garbled format, do you mind to share your parser?
I can't get rid of these:
[1672121500.727733915, {"log"=>"{"log":"---MULTI-LINE-MESSAGE--- \u001b[33;1m \u001b[0m depositOptionId: \u001b[32m'deposit.option.special.non-churn.2-ab1'\u001b[39m,\n","stream":"stdout","time":"2022-12-27T06:11:40.727720165Z"}"}]
I also have this garbled format, do you mind to share your parser? I can't get rid of these:
[1672121500.727733915, {"log"=>"{"log":"---MULTI-LINE-MESSAGE--- \u001b[33;1m \u001b[0m depositOptionId: \u001b[32m'deposit.option.special.non-churn.2-ab1'\u001b[39m,\n","stream":"stdout","time":"2022-12-27T06:11:40.727720165Z"}"}]
`apiVersion: v1 kind: ConfigMap metadata: name: fluent-bit-config namespace: logging labels: k8s-app: fluent-bit data:
Configuration files: server, input, filters and output
======================================================
fluent-bit.conf: | [SERVICE] Flush 1 Log_Level info Daemon off Parsers_File parsers.conf HTTP_Server On HTTP_Listen 0.0.0.0 HTTP_Port 2020
@INCLUDE input-kubernetes.conf
@INCLUDE filter-kubernetes.conf
@INCLUDE output-s3.conf
ansi_filter.lua: | function log_split(tag,timestamp,record) new_record = {} new_log = record["log"]:gsub('%a%d*%a*%[%d*%a','') new_log = new_log:gsub('\','') new_record["log"] = new_log return 2,timestamp,new_record end
input-kubernetes.conf: |
[INPUT]
Name tail
Tag kube.<namespace_name>.<pod_name>.<container_name>
Tag_Regex (?<pod_name>a-z0-9?(.a-z0-9?))(?<namespace_name>[^]+)_(?<container_name>.+)-
Path /var/log/containers/.log
DB /var/log/flb_kube.db
Mem_Buf_Limit 5MB
Skip_Long_Lines On
Refresh_Interval 10
filter-kubernetes.conf: |
[FILTER]
Name kubernetes
Match kube.<namespace_name>.<pod_name>.<container_name>
Kube_URL https://kubernetes.default.svc:443
Kube_CA_File /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
Kube_Token_File /var/run/secrets/kubernetes.io/serviceaccount/token
Kube_Tag_Prefix (?<pod_name>a-z0-9?(.a-z0-9?))(?<namespace_name>[^]+)_(?<container_name>.+)-
Merge_Log On
Merge_Log_Key log_processed
K8S-Logging.Parser On
K8S-Logging.Exclude Off
[FILTER]
Name lua
Match *
script /fluent-bit/etc/ansi_filter.lua
call log_split
[FILTER]
Name grep
Match *
Exclude log \u[0-9][a-z][[0-9][a-z]
output-s3.conf: |
[OUTPUT]
Name s3
Match *
bucket
region
total_file_size 10M
upload_timeout 1m
content_type text/plain; charset=utf-8
use_put_object On
s3_key_format /$TAG[1]/%Y/%m/%d/$TAG[2]/$TAG[3]-$UUID.log
parsers.conf: |
[PARSER]
Name apache
Format regex
Regex ^(?[^ ]) (?
[PARSER]
Name apache2
Format regex
Regex ^(?<host>[^ ]*) [^ ]* (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)")?$
Time_Key time
Time_Format %d/%b/%Y:%H:%M:%S %z
[PARSER]
Name apache_error
Format regex
Regex ^\[[^ ]* (?<time>[^\]]*)\] \[(?<level>[^\]]*)\](?: \[pid (?<pid>[^\]]*)\])?( \[client (?<client>[^\]]*)\])? (?<message>.*)$
[PARSER]
Name nginx
Format regex
Regex ^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^\"]*?)(?: +\S*)?)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)")?$
Time_Key time
Time_Format %d/%b/%Y:%H:%M:%S %z
[PARSER]
Name json
Format json
Time_Key time
Time_Format %d/%b/%Y:%H:%M:%S %z
[PARSER]
# http://rubular.com/r/tjUt3Awgg4
Name cri
Format regex
Regex ^(?<time>[^ ]+) (?<stream>stdout|stderr) (?<logtag>[^ ]*) (?<message>.*)$
Time_Key time
Time_Format %Y-%m-%dT%H:%M:%S.%L%z
[PARSER]
Name syslog
Format regex
Regex ^\<(?<pri>[0-9]+)\>(?<time>[^ ]* {1,2}[^ ]* [^ ]*) (?<host>[^ ]*) (?<ident>[a-zA-Z0-9_\/\.\-]*)(?:\[(?<pid>[0-9]+)\])?(?:[^\:]*\:)? *(?<message>.*)$
Time_Key time
Time_Format %b %d %H:%M:%S`
@chenFe-1 You sir are awesome! Thank you very much
For those who need the Lua filter to remove ANSI shell color code, you can apply this filter
function strip_ansi_color_code(tag, timestamp, record)
if type(record["log"]) == "string" then
record["log"] = record["log"]:gsub('[\27\155][][()#;?%d]*[A-PRZcf-ntqry=><~]', '')
return 2, 0, record
end
return 0, 0, 0
end
Syntax to put it to Fluentbit K8s yaml config
[FILTER]
Name lua
Match *
code function strip_ansi_color_code(tag, timestamp, record) if type(record["log"]) == "string" then record["log"] = record["log"]:gsub('[\27\155][][()#;?%d]*[A-PRZcf-ntqry=><~]', '') return 2, 0, record end return 0, 0, 0 end
call strip_ansi_color_code
Source:
[32m[Nest] 1 - [39m03/08/2024, 10:03:37 AM [32m LOG[39m [38;5;3m[InstanceLoader] [39m[32mConfigModule dependencies initialized[39m[38;5;3m +1ms[39m
Output:
[Nest] 1 - 03/08/2024, 10:03:37 AM LOG [InstanceLoader] ConfigModule dependencies initialized +1ms 10
Ref: https://github.com/fluent/fluent-bit/discussions/6151 https://stackoverflow.com/a/49209650/368691